-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzeDescriptions.py
79 lines (65 loc) · 2.9 KB
/
analyzeDescriptions.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import json
import sys
import re
def check_descriptions(description, path, issues):
rfc2119_words = {"MUST", "SHALL", "REQUIRED", "SHOULD", "MAY", "MUST NOT", "SHALL NOT", "SHOULD NOT", "MAY NOT"}
if isinstance(description, str):
# Check for RFC-2119 words
for word in rfc2119_words:
if re.search(rf'\b{word}\b', description):
issues.append(f'Description at path {path} contains RFC-2119 word: {word}')
# Check if description starts with an uppercase letter
if not description[0].isupper():
issues.append(f'Description at path {path} does not start with an uppercase letter.')
# Check if description ends with a single period
if not description.endswith('.'):
issues.append(f'Description at path {path} does not end with a single period.')
# Check if description contains multiple consecutive periods
if '..' in description:
issues.append(f'Description at path {path} contains multiple consecutive periods.')
# Split sentences by period followed by a space
sentences = re.split(r'\.\s', description)
# Check for exactly one space between sentences for descriptions with more than one sentence
if len(sentences) > 1:
matches = re.findall(r'\.\s{2,}', description)
if matches:
issues.append(f'Description at path {path} has more than one space after a period before the next sentence.')
matches = re.findall(r'\.[^\s]', description)
if matches:
issues.append(f'Description at path {path} does not have one space after a period before the next sentence.')
def traverse_schema(schema, path, issues):
if isinstance(schema, dict):
for key, value in schema.items():
if key == 'description':
check_descriptions(value, path + 'description', issues)
else:
traverse_schema(value, path + key + '.', issues)
elif isinstance(schema, list):
for index, item in enumerate(schema):
traverse_schema(item, path + f'{index}.', issues)
def main():
if len(sys.argv) != 2:
print("Usage: python analyzeDescriptions.py <path_to_schema>")
sys.exit(1)
schema_file_path = sys.argv[1]
try:
with open(schema_file_path, 'r') as file:
schema = json.load(file)
except FileNotFoundError:
print(f"Error: File not found: {schema_file_path}")
sys.exit(1)
except json.JSONDecodeError:
print(f"Error: Failed to parse JSON from file: {schema_file_path}")
sys.exit(1)
issues = []
traverse_schema(schema, '', issues)
if issues:
print("Issues found:")
for issue in issues:
print(issue)
sys.exit(1)
else:
print("All descriptions are correctly formatted.")
sys.exit(0)
if __name__ == "__main__":
main()