blob: 29ec2dd5398c038f9e657db3637d70159fe6393f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# 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)
|