michaelmior commited on
Commit
3ed4d56
·
verified ·
1 Parent(s): e642135

Add script to extract descriptions

Browse files
Files changed (1) hide show
  1. get_descriptions.py +57 -0
get_descriptions.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import glob
2
+ import json
3
+ import os
4
+ import sys
5
+
6
+ KEYWORDS = [
7
+ "allOf",
8
+ "anyOf",
9
+ "oneOf",
10
+ ]
11
+
12
+ REMOVE_KEYS = [
13
+ "$comment",
14
+ "description",
15
+ "title",
16
+ ]
17
+
18
+ dict_values = type({}.values())
19
+
20
+
21
+ def clean_object(obj):
22
+ if isinstance(obj, dict):
23
+ return {k: clean_object(v) for k, v in obj.items() if k not in REMOVE_KEYS}
24
+ elif isinstance(obj, list):
25
+ return [clean_object(v) for v in obj]
26
+ else:
27
+ return obj
28
+
29
+
30
+ def get_descriptions(obj, key=""):
31
+ if isinstance(obj, dict):
32
+ if "description" in obj and isinstance(obj["description"], str):
33
+ yield key, obj["description"], clean_object(obj)
34
+ for k, v in obj.items():
35
+ if k in KEYWORDS:
36
+ k = key
37
+ yield from get_descriptions(v, k)
38
+ elif isinstance(obj, (list, dict_values)):
39
+ for v in obj:
40
+ yield from get_descriptions(v, key)
41
+
42
+
43
+ for line in sys.stdin:
44
+ obj = json.loads(line)
45
+ schema = obj["name"]
46
+ obj = json.loads(obj["content"])
47
+ for key, desc, sub_obj in get_descriptions(obj):
48
+ json.dump(
49
+ {
50
+ "schema": os.path.basename(schema),
51
+ "key": key,
52
+ "description": desc,
53
+ "object": json.dumps(sub_obj),
54
+ },
55
+ sys.stdout,
56
+ )
57
+ sys.stdout.write("\n")