-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
readme_util.py
28 lines (24 loc) · 1013 Bytes
/
readme_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
'''Extremely crunchy parser intended to yell at me if I forget to update references to string constants in the readme'''
import re
import ao3downloader.strings as strings
def get_text() -> str:
with open('README.md') as f:
return f.read()
def check_value(text: str, pos: int) -> str:
start = re.search('<!--', text[pos:])
value = text[pos:pos+start.start()]
pos = pos + start.end()
end = re.search('-->', text[pos:])
variable = text[pos:pos+end.start()]
try:
realval = str(getattr(strings, variable)).replace('{', '').replace('}', '')
except AttributeError:
return f'Bad variable name. Variable: {variable} Value: {value}'
if value not in realval:
return f'Bad value. Variable: {variable} Expected: {realval} Actual: {value}'
return None
text = get_text()
errors = list(filter(lambda e: e is not None, [check_value(text, x.end()) for x in re.finditer('<!--CHECK-->', text)]))
if errors:
output = '; '.join(errors)
print(output)