-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.py
199 lines (157 loc) · 7.79 KB
/
util.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import hashlib
import re
import sys
from typing import Optional
import urllib.request
''' Utility functions for RFC annotations tools '''
_running_in_test = False
verbose_output = False
def debug(s: str, end='\n'):
if verbose_output:
print(s, end=end)
def info(s: str, end='\n'):
print(s, end=end)
def warn(s: str):
print(f"\n\n Warning: {s}\n", file=sys.stderr)
def error(s: str):
print(f"\n\n ERROR: {s}\n", file=sys.stderr)
def is_valid_date_string(s: str) -> bool:
return len(s) == 10 and re.match(r"^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])", s) is not None
def correct_path(directory: str) -> str:
if directory is None or len(directory) == 0:
directory = "."
if not directory.endswith("/"):
directory += "/"
return directory
def get_from_environment(name: str, default: Optional[str] = None) -> Optional[str]:
if not name.startswith("RFC_"):
name = "RFC_" + name
return os.environ[name] if name in os.environ else default
def filtered_files(directory: str, prefix: str = "", suffix: str = "") -> list:
ret = []
if os.path.exists(directory):
for file in os.listdir(directory):
if file.startswith(prefix) and file.endswith(suffix):
ret.append(file)
return ret
def create_checksum(d: dict) -> str:
s: str = ""
for key in sorted(d.keys()):
val = d[key]
if len(s) > 0:
s += "\n"
s += f"{key}={val}"
return hashlib.md5(bytes(s, "utf-8")).hexdigest()
def create_anchor(href: str, text: str, suffix: str = "", prefix: str = "", attributes: Optional[dict] = None) \
-> str:
extra = "" if href.startswith("#") else "target='_blank' "
if attributes is not None:
for name, val in attributes.items():
if name != "target":
extra += f"{name}='{val}' "
return f"{prefix}<a {extra}href='{href}'>{text}</a>{suffix}"
def replace_links_in_text(line: str, replace_special_chars: bool) -> str:
start = "<"
end = ">"
if replace_special_chars:
start = "<"
end = ">"
line = line.replace("&", "&").replace("<", start).replace(">", end)
stop = False
while not stop:
candidates = re.finditer(start + "http(s*)://([^/?#]*)?([^?#]*)(\\?([^#]*))?(#(.*))?" + end, line)
stop = True
if candidates is not None:
for search_result in candidates:
groups = search_result.groups()
if groups is not None and len(groups) >= 7 and groups[0] is not None:
href = search_result.group()[len(start):-len(end)]
if end in href:
# if there are multiple links in the same line we have to split the result
href = href[0:href.index(end)]
stop = False
search_fragment = f"{start}{href}{end}"
line = line.replace(search_fragment, create_anchor(href=href, text=href))
return line
def get_rfc_target(rfc: str, rfc_list: Optional[list] = None, target_id: Optional[str] = None) -> str:
if rfc_list is not None and rfc in rfc_list:
if target_id is None:
return f"./rfc{rfc}.html"
else:
return f"./rfc{rfc}.html#{target_id}"
else:
if target_id is None:
return f"https://datatracker.ietf.org/doc/rfc{rfc}/"
else:
return f"https://datatracker.ietf.org/doc/html/rfc{rfc}.html#{target_id}"
def rewrite_rfc_anchor(line: str, rfc_list: Optional[list]) -> str:
def get_target_id(entity: str, number: str) -> str:
reftype = entity.lower()
if reftype == "section" and len(number) > 1 and number[:1].isalpha():
reftype = "appendix"
return reftype + "-" + number.upper()
if "@@" in line:
start = line.index("@@")
split = line[start + 2:]
if "@@" in split:
end = split.index("@@")
target_text = split[0:end].strip()
# find RFC number (and line or section reference) inside {target_text}
fmt1 = r"^(?P<sectionstring>(?P<sectiontype>Section|Appendix|Line)\s*(?P<sectionno>[0-9A-Z\.]+))"\
"(?P<fill1>\s*(of|in)\s*\[?)(?P<docstring>RFC\s*(?P<docno>[0-9]+))(?P<fill2>\]?)$"
fmt2 = r"^(?P<fill1>\[?)(?P<docstring>RFC\s*(?P<docno>[0-9]+))(?P<fill2>\]?,?\s*)(?P<sectionstring>"\
"(?P<sectiontype>Section|Appendix|Line)\s*(?P<sectionno>[0-9A-Z\.]+))$"
fmt3 = r"^(?P<fill1>\[?)(?P<docstring>RFC\s*(?P<docno>[0-9]+))(?P<fill2>]?)$"
fmt4 = r"^(?P<sectionstring>(?P<sectiontype>Section|Appendix|Line)\s*(?P<sectionno>[0-9A-Z\.]+))$"
replacement = None
match = re.search(fmt1, target_text, flags=re.IGNORECASE)
if match is not None:
target_rfc = match.group("docno")
target_section = get_target_id(match.group("sectiontype"), match.group("sectionno"))
a1 = create_anchor(href=get_rfc_target(target_rfc, rfc_list, target_section),
text=match.group("sectionstring"), suffix=match.group("fill1"))
a2 = create_anchor(href=get_rfc_target(target_rfc, rfc_list), text=match.group("docstring"),
suffix=match.group("fill2"))
replacement = f"{a1}{a2}"
else:
match = re.search(fmt2, target_text, flags=re.IGNORECASE)
if match is not None:
target_rfc = match.group("docno")
target_section = get_target_id(match.group("sectiontype"), match.group("sectionno"))
a1 = create_anchor(href=get_rfc_target(target_rfc, rfc_list, target_section),
text=match.group("sectionstring"))
a2 = create_anchor(prefix=match.group("fill1"), href=get_rfc_target(target_rfc, rfc_list),
text=match.group("docstring"), suffix=match.group("fill2"))
replacement = f"{a2}{a1}"
else:
match = re.search(fmt3, target_text, flags=re.IGNORECASE)
if match is not None:
target_rfc = match.group("docno")
replacement = create_anchor(prefix=match.group("fill1"),
href=get_rfc_target(target_rfc, rfc_list),
text=match.group("docstring"), suffix=match.group("fill2"))
else:
match = re.search(fmt4, target_text, flags=re.IGNORECASE)
if match is not None:
target_section = get_target_id(match.group("sectiontype"), match.group("sectionno"))
contents = match.group("sectionstring")
replacement = create_anchor(href="#" + target_section, text=contents)
if replacement is not None:
line = line.replace(f"@@{target_text}@@", replacement)
return rewrite_rfc_anchor(line, rfc_list)
return line[0:start + 2] + rewrite_rfc_anchor(split, rfc_list)
return line
def rewrite_rfc_anchors(lines: [str], rfc_list: Optional[list]) -> [str]:
ret = []
for line in lines:
ret.append(rewrite_rfc_anchor(line, rfc_list))
return ret
def means_false(s: str) -> bool:
return s.lower() in ["0", "no", "false", "off", "disabled", "never"]
def means_true(s: str) -> bool:
return not means_false(s)
def config_directories() -> [str]:
return ["default-config"] if _running_in_test else ["local-config", "default-config"]
def urlopen(url):
return urllib.request.urlopen(urllib.request.Request(url, headers ={'User-Agent': 'Mozilla/5.0'}))