-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmetadata.py
More file actions
215 lines (165 loc) · 7.71 KB
/
Copy pathmetadata.py
File metadata and controls
215 lines (165 loc) · 7.71 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import functools
import re
from string import Template
from typing import Any, Callable, Iterable, Iterator, NamedTuple, Optional, TypeVar, Union
from modules.utilities import first, second
class MetadataError(Exception):
def __init__(self,*args: Any,**kwargs: Any) -> None:
Exception.__init__(self,*args,**kwargs)
# TYPES
T = TypeVar("T")
Predicate = Callable[[T], bool]
TagValue = Union[str, bool]
MetadataItem = tuple[str, TagValue]
Metadata = list[MetadataItem]
class Tag_string(NamedTuple):
name: str
unique: bool
default: Optional[str]
required: bool
predicate: Optional[Predicate[str]] = None
class Tag_boolean(NamedTuple):
name: str
unique: bool
default: Optional[bool]
required: bool
predicate: Optional[Predicate[bool]] = None
Tag = Union[Tag_string, Tag_boolean]
PREFIX_COMMENT: str = "//"
PREFIX_TAG: str = "@"
BLOCK_START: str = "==UserScript=="
BLOCK_END: str = "==/UserScript=="
def tag(name: str) -> str:
return PREFIX_TAG + name
REGEXGROUP_CONTENT: str = "content"
REGEX_METADATA_BLOCK: re.Pattern = re.compile(
PREFIX_COMMENT + r"\s*" + BLOCK_START + r"\n"
+ r"(?P<" + REGEXGROUP_CONTENT + r">.*)"
+ PREFIX_COMMENT + r"\s*" + BLOCK_END,
re.DOTALL
)
REGEXGROUP_TAGNAME: str = "tagname"
REGEXGROUP_TAGVALUE: str = "tagvalue"
REGEX_METADATA_LINE: re.Pattern = re.compile(
r"^\s*" + PREFIX_COMMENT
+ r"\s*" + PREFIX_TAG
+ r"(?P<" + REGEXGROUP_TAGNAME + r">[^\s]+)"
+ r"(?:\s+?(?P<" + REGEXGROUP_TAGVALUE + r">\S.*)?)?$"
)
STRING_ERROR_MISSING_BLOCK: str = f"""No metadata block found. The metadata block must follow this format:
{PREFIX_COMMENT} {BLOCK_START}
{PREFIX_COMMENT} {tag("key1")} value1
{PREFIX_COMMENT} {tag("key2")} value2
{PREFIX_COMMENT} ...
{PREFIX_COMMENT} {tag("keyN")} valueN
{PREFIX_COMMENT} {BLOCK_END}
It must start with `{PREFIX_COMMENT} {BLOCK_START}` and end with `{PREFIX_COMMENT} {BLOCK_END}`, and every line must be a line comment starting with an {PREFIX_TAG}-prefixed tag name, then whitespace, then a tag value (with the exception of boolean directives such as {tag("noframes")}, which are automatically true if present).
"""
STRING_ERROR_INVALID_BLOCK: Template = Template(f"""Invalid metadata block. Only comments are allowed, and each line should follow this format:
{PREFIX_COMMENT} {tag("key")} value
This line does not:
$line
""")
STRING_ERROR_MISSING_TAG: Template = Template(f"""The {tag("$tagName")} metadata directive is required, but was not found.""")
STRING_ERROR_MISSING_VALUE: Template = Template(f"""The {tag("$tagName")} metadata directive requires a value, like so:
{PREFIX_COMMENT} {tag("$tagName")} something
""")
STRING_ERROR_PREDICATE_FAILED: Template = Template(f"""Detected a {tag("$tagName")} metadata directive with an invalid value, namely:
$tagValue
""")
def isWhitespaceLine(s: str) -> bool:
return re.compile(r"^\s*$").match(s) is not None
def isCommentLine(s: str) -> bool:
return re.compile(r"^\s*" + PREFIX_COMMENT + r".*$").match(s) is not None
def extract(userscriptContent: str) -> str: # raises MetadataError
match_metadataBlock: Optional[re.Match] = REGEX_METADATA_BLOCK.search(userscriptContent)
if (match_metadataBlock is None):
raise MetadataError(STRING_ERROR_MISSING_BLOCK)
block: str = match_metadataBlock.group(REGEXGROUP_CONTENT)
for line in block.splitlines():
if not isWhitespaceLine(line) and not isCommentLine(line) and not REGEX_METADATA_LINE.match(line):
raise MetadataError(STRING_ERROR_INVALID_BLOCK.substitute(line=line))
return block
def parse(metadataContent: str) -> Metadata:
def parseLine(line: str) -> Optional[MetadataItem]:
match: Optional[re.Match] = REGEX_METADATA_LINE.search(line)
if match is None:
return None
else:
tagName: str = match.group(REGEXGROUP_TAGNAME)
tagValue: Optional[str] = match.group(REGEXGROUP_TAGVALUE)
# Boolean tags have no explicit value; if they are present, they are true:
if tagValue is None:
return (tagName, True)
else:
return (tagName, tagValue)
# filter did not play well with mypy:
parsedItems: Metadata = []
for item in map(parseLine, metadataContent.splitlines()):
if item is not None:
parsedItems.append(item)
return parsedItems
def tagByName(tags: list[Tag], tagName: str) -> Optional[Tag]:
return next((x for x in tags if x.name == tagName), None)
def validatePair(tags: list[Tag], pair: MetadataItem) -> MetadataItem:
(tagName, tagValue) = pair
tag: Optional[Tag] = tagByName(tags, tagName)
if tag is None:
# Unrecognized key.
return (tagName, tagValue)
else:
# Recognized key! Check if it has the correct type.
tagPredicate: Optional[Predicate] = tag.predicate
if type(tag) is Tag_string and type(tagValue) is not str:
raise MetadataError(STRING_ERROR_MISSING_VALUE.substitute(tagName=tagName))
if type(tag) is Tag_boolean:
tagValue = tagValue is not False # This handles cases like `@noframes blabla`; a boolean directive is true no matter what comes after it.
if tagPredicate is not None:
if not tagPredicate(tagValue):
raise MetadataError(STRING_ERROR_PREDICATE_FAILED.substitute(tagName=tagName, tagValue=str(tagValue)))
return (tagName, tagValue)
def validate(tags: list[Tag], metadata: Metadata) -> Metadata: # raises MetadataError
def handleDuplicate(acc: Iterable[MetadataItem], pair: MetadataItem) -> Iterable[MetadataItem]:
name: str = first(pair)
tag: Optional[Tag] = tagByName(tags, name)
seenTagNames: Iterator[str] = map(first, acc)
# Throw away pair if it has the same tag name as some already seen, known, unique directive:
return acc if tag is not None and tag.unique and name in seenTagNames else list(acc) + [pair]
def withoutDuplicates(metadata: Metadata) -> Metadata:
empty: Iterable[tuple[str, Union[str, bool]]] = [] # to satisfy mypy
return list(functools.reduce(handleDuplicate, metadata, empty))
# Awkwardly written to satisfy mypy:
def withDefaults(metadata: Metadata) -> Metadata:
tagNamesThatWeHave: list[str] = list(map(first, metadata))
unseenItems = map(
lambda tag: (tag.name, tag.default),
filter(
lambda tag: tag.name not in tagNamesThatWeHave,
tags
)
)
neededDefaults: Metadata = []
for (tagName, default) in unseenItems:
if default is not None:
neededDefaults.append((tagName, default))
return metadata + neededDefaults
def assertRequiredPresent(metadata: Metadata) -> Metadata:
ourTagNames: Iterator[str] = map(first, metadata)
requiredTags: Iterator[Tag] = filter(lambda tag: tag.required, tags)
for tag in requiredTags:
if (tag.name not in ourTagNames):
raise MetadataError(STRING_ERROR_MISSING_TAG.substitute(tagName=tag.name))
return metadata
return list(map(
lambda *args: validatePair(tags, *args),
withDefaults(withoutDuplicates(
assertRequiredPresent(metadata)
))
))
def validator(tags: list[Tag]) -> Callable[[Metadata], Metadata]:
return lambda metadata: validate(tags, metadata)
def valueGetter_all(metadata: Metadata) -> Callable[[Tag], list[TagValue]]:
return lambda tag: [second(pair) for pair in metadata if first(pair) == tag.name]
def valueGetter_one(metadata: Metadata) -> Callable[[Tag], Optional[TagValue]]:
v = valueGetter_all(metadata)
return lambda tag: None if len(v(tag)) == 0 else v(tag)[0]