forked from SimonAlling/userscript-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathignore.py
More file actions
42 lines (30 loc) · 1.06 KB
/
Copy pathignore.py
File metadata and controls
42 lines (30 loc) · 1.06 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
from typing import List, Pattern
import re
from modules.patterns import isIncludePattern_regex, regexify, withoutSurroundingSlashes
COMMENT_PREFIX: str = "#"
PORT_PREFIX: str = ":"
PIPE: str = "|"
REGEX_COMMENT: Pattern = re.compile(r"\#.*$")
def rulesIn(text: str) -> List[str]:
return list(filter(
lambda s: s != "",
map(withoutCommentAndTrimmed, text.splitlines())
))
def withoutCommentAndTrimmed(line: str) -> str:
return re.sub(REGEX_COMMENT, "", line).strip()
def withPortSuffix(regex: str) -> str:
return regex if PORT_PREFIX in regex else regex + r"\:\d+"
def ignoreRegex(ignoreRule: str) -> str:
return (
withoutSurroundingSlashes(ignoreRule)
if isIncludePattern_regex(ignoreRule)
else r"^(?:.+\.)?" + withPortSuffix(regexify(ignoreRule)) + r"$"
)
def entireIgnoreRegex(ignoreFileContent: str) -> str:
return PIPE.join(map(
ignoreRegex,
rulesIn(ignoreFileContent)
))
# https://stackoverflow.com/a/2637899
def negate(regex: str) -> str:
return "^(?!(?:" + regex + ")$).*$"