-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_formats.py
executable file
·115 lines (92 loc) · 3 KB
/
parse_formats.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
#!/usr/bin/python3
"""Parse FORMATS from manual/ into text files."""
import dataclasses
import pathlib
import re
import textwrap
import typing as t
cwd = pathlib.Path(__file__).parent
manual_dir = cwd / "manual"
# Since tmux 1.6 manual pages used this before formats
needle = "The following variables are available, where appropriate:"
@dataclasses.dataclass
class Format:
variable_name: str
alias: str | None
replaced_with: str
@dataclasses.dataclass
class Manual:
version: str
machine_version: float
path: pathlib.Path
formats: list[Format] = dataclasses.field(default_factory=list)
format_re = re.compile(
r"""
^\s{5}
(?P<variable_name>[a-z_]+){1}
(?:[ \t]){5,15} # skip spaces
(?P<alias>\#[a-zA-Z]{1})?
(?:[ \t]){5,15} # skip spaces
(?P<replaced_with>[a-zA-Z ]+){1}$
""",
re.VERBOSE | re.MULTILINE,
)
def run() -> int:
if not manual_dir.is_dir():
raise Exception("manual/ dir mustexist in CWD in relation to this script")
# Fetch files
version_map: dict[str, Manual] = {}
for file in sorted(manual_dir.glob("*.txt")):
version_raw = str(file.stem).rsplit(".txt")[0]
machine_version = version_raw
if not machine_version[-1].isdigit():
machine_version = machine_version[:-1] + str(ord(machine_version[-1]) - 96)
version = float(machine_version)
version_map[version_raw] = Manual(
version=version_raw, machine_version=version, path=file
)
# Parse formats from files
for manual in version_map.values():
if manual.machine_version < 1.6:
continue
contents = open(manual.path).read()
for line in format_re.finditer(contents):
if line is not None:
match = line.groupdict()
assert match is not None
manual.formats.append(Format(**match))
for manual in version_map.values():
print(f"tmux {manual.version}")
# Array
formats = ", ".join([f'"{format.variable_name}"' for format in manual.formats])
print(f" Formats: [{formats}]")
# Dataclass
print(
textwrap.dedent(
f"""
@dataclasses.dataclass
class TmuxObject:
"""
+ "\n ".join(
[
f"{format.variable_name}: str | None = None"
for format in manual.formats
]
)
)
)
raw_formats = [format.variable_name for format in manual.formats]
if manual.version == "3.0a":
try:
assert "pane_title" in raw_formats
except AssertionError:
print("Should have pane title")
raise
elif manual.version == "3.3":
try:
assert "pane_title" not in raw_formats
except AssertionError:
print("Should not have pane_title")
return 0
if __name__ == "__main__":
run()