|
| 1 | +import pprint |
| 2 | +import yaml |
| 3 | +import jsonref |
| 4 | +import json |
| 5 | + |
| 6 | + |
| 7 | +with open("models/myos/interfaces/myos_interfaces.yml", 'r') as stream: |
| 8 | + try: |
| 9 | + data = yaml.load(stream) |
| 10 | + except yaml.YAMLError as exc: |
| 11 | + print(exc) |
| 12 | + |
| 13 | + |
| 14 | +data = jsonref.loads(json.dumps(data)) |
| 15 | + |
| 16 | + |
| 17 | +# |
| 18 | +# data = pluck(data) |
| 19 | +# |
| 20 | +# pprint(yaml.dump(data)) |
| 21 | +# |
| 22 | +# |
| 23 | +# yaml.Dumper.ignore_aliases = lambda *args : True |
| 24 | +# print(yaml.dump(data, default_flow_style=False)) |
| 25 | +# |
| 26 | + |
| 27 | + |
| 28 | +def dive(obj, required=False): |
| 29 | + # print('------------------------------') |
| 30 | + # print(obj) |
| 31 | + if not 'description' in obj: |
| 32 | + print('description missing in obj') |
| 33 | + result = {'description': obj['description']} |
| 34 | + if not 'type' in obj: |
| 35 | + print('type missing in obj') |
| 36 | + if obj['type'] == 'object': |
| 37 | + result['suboptions'] = {} |
| 38 | + if not 'properties' in obj: |
| 39 | + print('properties missing in obj') |
| 40 | + for propkey, propval in obj['properties'].items(): |
| 41 | + required = bool('required' in obj and propkey in obj['required']) |
| 42 | + result['suboptions'][propkey] = dive(propval, required) |
| 43 | + elif obj['type'] == 'array': |
| 44 | + result['suboptions'] = {} |
| 45 | + if not 'items' in obj: |
| 46 | + print('items missing in obj') |
| 47 | + if not 'properties' in obj: |
| 48 | + print('items/properties missing in obj') |
| 49 | + for propkey, propval in obj['items']['properties'].items(): |
| 50 | + required = bool('required' in obj['items'] and propkey in obj['items']['required']) |
| 51 | + result['suboptions'][propkey] = dive(propval, required) |
| 52 | + elif obj['type'] in ['str', 'bool', 'int']: |
| 53 | + if 'default' in obj: |
| 54 | + result['default'] = obj['default'] |
| 55 | + if 'enum' in obj: |
| 56 | + result['choices'] = obj['enum'] |
| 57 | + if 'version_added' in obj: |
| 58 | + result['version_added'] = obj['version_added'] |
| 59 | + if required: |
| 60 | + result['required'] = required |
| 61 | + result['type'] = obj['type'] |
| 62 | + |
| 63 | + |
| 64 | + return result |
| 65 | + |
| 66 | +def u_to_str(object, context, maxlevels, level): |
| 67 | + typ = pprint._type(object) |
| 68 | + if typ is unicode: |
| 69 | + object = str(object) |
| 70 | + return pprint._safe_repr(object, context, maxlevels, level) |
| 71 | + |
| 72 | + |
| 73 | +result = dive(data['schema']) |
| 74 | +printer = pprint.PrettyPrinter() |
| 75 | +printer.format = u_to_str |
| 76 | +print(printer.pformat(result)) |
0 commit comments