diff options
author | vg <vg@devys.org> | 2018-12-07 12:05:14 +0100 |
---|---|---|
committer | vg <vg@devys.org> | 2018-12-07 12:05:14 +0100 |
commit | 8fe0b2c7754873ec33bccb7952683b96113491bc (patch) | |
tree | 4c006a97b194b720b8630395eb8315495f856aad /contacts_validation/contacts_validation/command_line.py | |
download | devys-pim-8fe0b2c7754873ec33bccb7952683b96113491bc.tar.gz devys-pim-8fe0b2c7754873ec33bccb7952683b96113491bc.tar.bz2 devys-pim-8fe0b2c7754873ec33bccb7952683b96113491bc.zip |
first commit; add contacts_validation
Diffstat (limited to 'contacts_validation/contacts_validation/command_line.py')
-rw-r--r-- | contacts_validation/contacts_validation/command_line.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/contacts_validation/contacts_validation/command_line.py b/contacts_validation/contacts_validation/command_line.py new file mode 100644 index 0000000..0310c78 --- /dev/null +++ b/contacts_validation/contacts_validation/command_line.py @@ -0,0 +1,51 @@ +# Copyright 2018 vg@devys.org +# SPDX-License-Identifier: MIT + +''' +Validate my contacts. + +if no FILENAME or one filename is -, stdin will be used. + +Usage: contacts-validation [options] [--] [FILENAME...] + contacts-validation -h|--help|--help-format + +Options: + -h, --help Display this help message + --help-format display schema fields name/title/descriptions +''' + +import os +import sys + +import docopt +import yaml + +from . import validate_yaml_data + + +def gen_streams(filenames): + for filename in filenames: + if filename == '-': + yield sys.stdin + else: + with open(filename, 'r', encoding='utf8') as stream: + yield stream + + +def main(): + 'function called only when script invoked directly on command line' + args = docopt.docopt(__doc__) + + if args['--help-format']: + print(f'for now see the schema yaml file for description') + raise SystemExit(0) + + with open(f'{os.path.dirname(__file__)}/data/contacts_schema.yaml') as schema_fh: + schema_obj = yaml.safe_load(schema_fh.read()) + assert schema_obj + + for i, stream in enumerate(gen_streams(args['FILENAME'] or ['-'])): + print('#'*60, f'# Valdating stream {i}', '#'*60, sep='\n') + yaml_data = yaml.safe_load(stream.read()) + assert yaml_data + validate_yaml_data(yaml_data, schema_obj) |