-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelationscheck.py
91 lines (70 loc) · 3.69 KB
/
relationscheck.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
80
81
82
83
84
85
86
87
88
89
90
91
import os
import re
import pygraphviz as pgv
def parse_patterns_dot(file_path):
graph = pgv.AGraph(file_path)
patterns = {}
for edge in graph.edges():
src, dest = edge
if src not in patterns:
patterns[src] = {'supports': set(), 'supported_by': set()}
if dest not in patterns:
patterns[dest] = {'supports': set(), 'supported_by': set()}
patterns[src]['supports'].add(dest)
patterns[dest]['supported_by'].add(src)
print(f"patterns: {patterns}")
return patterns
def parse_md_file(file_path):
with open(file_path, 'r') as file:
content = file.read()
# Regex to capture supports and supported by sections
supports_section = re.findall(r'### Supports\s*:\s*([\s\S]*?)(?=\n###|\Z)', content)
supported_by_section = re.findall(r'### Supported By\s*:\s*([\s\S]*?)(?=\n###|\Z)', content)
# Extracting pattern names from the sections
supports_patterns = set(re.findall(r'\[([^\]]+)\]\(./[^\)]+\)', supports_section[0])) if supports_section else set()
supported_by_patterns = set(re.findall(r'\[([^\]]+)\]\(./[^\)]+\)', supported_by_section[0])) if supported_by_section else set()
return supports_patterns, supported_by_patterns
def extract_pattern_name(file_path):
with open(file_path, 'r') as file:
content = file.read()
match = re.search(r'^##\s+([A-Z]{2,4})\s*-\s*(.+)$', content, re.MULTILINE)
if match:
return match.group(2).strip()
else:
return os.path.basename(file_path).replace('.md', '')
def validate_patterns(directory, patterns_dot_path):
patterns = parse_patterns_dot(patterns_dot_path)
missing_links = {}
for filename in os.listdir(directory):
if filename.endswith('.md'):
file_path = os.path.join(directory, filename)
pattern_name = extract_pattern_name(file_path)
print(f"{pattern_name}:")
file_path = os.path.join(directory, filename)
supports, supported_by = parse_md_file(file_path)
expected_supports = patterns.get(pattern_name, {}).get('supports', set())
expected_supported_by = patterns.get(pattern_name, {}).get('supported_by', set())
#print(f"expected supports: {expected_supports}:")
#print(f" supports: {supports}:")
#print(f"expected supports bu: {expected_supported_by}:")
#print(f"supported by: {supported_by}:")
missing_supports = expected_supports - supports
missing_supported_by = expected_supported_by - supported_by
if missing_supports or missing_supported_by:
missing_links[filename] = []
for pattern in missing_supports:
remotefilename = pattern.replace(' ', '_').replace('/', '_').replace('-', '_').lower()
print(f"Supports Missing: {filename}:")
print(f"* [{pattern}](./{remotefilename}.md)")
missing_links[filename].append(f"* [{pattern}](./{remotefilename}.md)")
for pattern in missing_supported_by:
remotefilename = pattern.replace(' ', '_').replace('/', '_').replace('-', '_').lower()
print(f"Supported By Missing: {filename}:")
print(f"* [{pattern}](./{remotefilename}.md)")
missing_links[filename].append(f"* [{pattern}](./{remotefilename}.md)")
return missing_links
if __name__ == "__main__":
directory = './content/patterns/'
patterns_dot_path = './patterns.dot'
missing_links = validate_patterns(directory, patterns_dot_path)
#print_missing_links(missing_links)