annotate validateyaml/main.py @ 0:c00f27542710

initial commit
author Jeff Hammel <k0scist@gmail.com>
date Tue, 01 Nov 2016 16:06:13 -0700
parents
children 99c252285817
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 validate yaml
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # imports
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import argparse
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import yaml
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 class ValidateYAMLParser(argparse.ArgumentParser):
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 """CLI option parser"""
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def __init__(self, **kwargs):
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 kwargs.setdefault('description', __doc__)
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 argparse.ArgumentParser.__init__(self, **kwargs)
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 self.add_argument('yaml_file', type=argparse.filetype('r'))
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 self.options = None
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 def parse_args(self, *args, **kw):
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 options = argparse.ArgumentParser.parse_args(self, *args, **kw)
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 self.validate(options)
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 self.options = options
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 return options
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 def validate(self, options):
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 """validate options"""
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 def main(args=sys.argv[1:]):
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 """CLI"""
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 # parse command line options
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 parser = ValidateYAMLParser()
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 options = parser.parse_args(args)
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 yaml.load(options.yaml_file)
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 if __name__ == '__main__':
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 main()
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
c00f27542710 initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43