# 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 error_occured = False 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 if not validate_yaml_data(yaml_data, schema_obj): error_occured = True if error_occured: raise SystemExit(1)