-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclevis.py
127 lines (91 loc) · 3.21 KB
/
clevis.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Link things together.
> The clevis is a U-shaped piece that has holes at the end of the
prongs to accept the clevis pin.
_____
/ \
/ \
,' `.
| |
| |
| |
===[]====o====[]===
|___________|
`---'
"""
import json
import os
from difflib import unified_diff
import tomllib
def _get_linked_value(link: list[dict]) -> str:
completed_operations = [_operate(step) for step in link]
return "".join(completed_operations)
def _operate(link: dict) -> str:
match link["type"]:
case "path":
path = link["value"]
if os.path.exists(path):
return path
raise ValueError(f"path {path} doesn't exist")
case "toml":
file_path = link["file"]
key = link["key"]
with open(file_path, "rb") as f:
config = tomllib.load(f)
# Traverse the config using the dot-separated key
keys = key.split(".")
value = config
for k in keys:
value = value[k]
return value
case "span":
file_path = link["file"]
key = link["key"]
# Parse the key to extract line number and character range
line_raw, char_raw = key.split(":")
line_idx = int(line_raw) - 1 # Convert to zero-based index
char_start, char_end = map(int, char_raw.split("-"))
with open(file_path, "r") as f:
lines = f.readlines()
# Ensure the line index is within bounds
if line_idx < 0 or line_idx >= len(lines):
raise ValueError(f"line index {line_idx + 1} is out of bounds")
line = lines[line_idx]
# Ensure character ranges are within bounds
if char_start < 1 or char_end > len(line):
raise ValueError(
f"character range {char_start}-{char_end} is out of bounds for line {line_idx + 1}"
)
span = line[char_start - 1 : char_end]
return span
case _:
raise ValueError(f"unknown type: {link['type']}")
def _get_state():
with open("clevis.json", "r") as f:
return json.load(f)
if __name__ == "__main__":
state = _get_state()
status: int = 0
for key, value in state.items():
print(f"checking {key}")
expected_str = value["value"]
a_str = _get_linked_value(value["a"]["link"])
b_str = _get_linked_value(value["b"]["link"])
diff_a = list(
unified_diff(a_str, expected_str, fromfile="actual", tofile="expected")
)
if diff_a:
print("differences found in A")
print("".join(diff_a))
status = 1
else:
print("no differences found in A")
diff_b = list(
unified_diff(b_str, expected_str, fromfile="actual", tofile="expected")
)
if diff_b:
print("differences found in B")
print("".join(diff_b))
status = 1
else:
print("no differences found in B")
exit(status)