2021-04-29 20:13:46 +02:00
|
|
|
# I'm sorry that this code has become a bit dense and unreadable. While
|
|
|
|
# reading, it is important to remember what True and False mean. I'd love to
|
|
|
|
# have some proper sum-types for the inputs and outputs, they'd make this code
|
|
|
|
# a lot easier to understand.
|
|
|
|
|
|
|
|
import ast
|
2021-04-29 09:51:25 +02:00
|
|
|
import re
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from dataclasses import dataclass
|
2021-04-29 16:52:00 +02:00
|
|
|
from pathlib import PurePath
|
2021-04-29 09:51:25 +02:00
|
|
|
from typing import Dict, Optional, Union
|
|
|
|
|
|
|
|
|
|
|
|
class Rule(ABC):
|
|
|
|
@abstractmethod
|
2021-04-29 20:13:46 +02:00
|
|
|
def transform(self, path: PurePath) -> Union[PurePath, bool]:
|
|
|
|
"""
|
|
|
|
Try to apply this rule to the path. Returns another path if the rule
|
|
|
|
was successfully applied, True if the rule matched but resulted in an
|
|
|
|
exclamation mark, and False if the rule didn't match at all.
|
|
|
|
"""
|
|
|
|
|
2021-04-29 09:51:25 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
# These rules all use a Union[T, bool] for their right side. They are passed a
|
|
|
|
# T if the arrow's right side was a normal string, True if it was an
|
|
|
|
# exclamation mark and False if it was missing entirely.
|
|
|
|
|
2021-04-29 09:51:25 +02:00
|
|
|
class NormalRule(Rule):
|
2021-04-29 20:13:46 +02:00
|
|
|
def __init__(self, left: PurePath, right: Union[PurePath, bool]):
|
|
|
|
|
2021-04-29 09:51:25 +02:00
|
|
|
self._left = left
|
|
|
|
self._right = right
|
|
|
|
|
2021-04-29 16:52:00 +02:00
|
|
|
def _match_prefix(self, path: PurePath) -> Optional[PurePath]:
|
2021-04-29 09:51:25 +02:00
|
|
|
left_parts = list(reversed(self._left.parts))
|
|
|
|
path_parts = list(reversed(path.parts))
|
|
|
|
|
|
|
|
if len(left_parts) > len(path_parts):
|
|
|
|
return None
|
|
|
|
|
|
|
|
while left_parts and path_parts:
|
|
|
|
left_part = left_parts.pop()
|
|
|
|
path_part = path_parts.pop()
|
|
|
|
|
|
|
|
if left_part != path_part:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if left_parts:
|
|
|
|
return None
|
|
|
|
|
2021-04-29 16:52:00 +02:00
|
|
|
return PurePath(*path_parts)
|
2021-04-29 09:51:25 +02:00
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
def transform(self, path: PurePath) -> Union[PurePath, bool]:
|
2021-04-29 09:51:25 +02:00
|
|
|
if rest := self._match_prefix(path):
|
2021-04-29 20:13:46 +02:00
|
|
|
if isinstance(self._right, bool):
|
|
|
|
return self._right or path
|
|
|
|
else:
|
|
|
|
return self._right / rest
|
2021-04-29 09:51:25 +02:00
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
return False
|
2021-04-29 09:51:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ExactRule(Rule):
|
2021-04-29 20:13:46 +02:00
|
|
|
def __init__(self, left: PurePath, right: Union[PurePath, bool]):
|
2021-04-29 09:51:25 +02:00
|
|
|
self._left = left
|
|
|
|
self._right = right
|
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
def transform(self, path: PurePath) -> Union[PurePath, bool]:
|
2021-04-29 09:51:25 +02:00
|
|
|
if path == self._left:
|
2021-04-29 20:13:46 +02:00
|
|
|
if isinstance(self._right, bool):
|
|
|
|
return self._right or path
|
|
|
|
else:
|
|
|
|
return self._right
|
2021-04-29 09:51:25 +02:00
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
return False
|
2021-04-29 09:51:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ReRule(Rule):
|
2021-04-29 20:13:46 +02:00
|
|
|
def __init__(self, left: str, right: Union[str, bool]):
|
2021-04-29 09:51:25 +02:00
|
|
|
self._left = left
|
|
|
|
self._right = right
|
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
def transform(self, path: PurePath) -> Union[PurePath, bool]:
|
2021-04-29 09:51:25 +02:00
|
|
|
if match := re.fullmatch(self._left, str(path)):
|
2021-04-29 20:13:46 +02:00
|
|
|
if isinstance(self._right, bool):
|
|
|
|
return self._right or path
|
|
|
|
|
|
|
|
vars: Dict[str, Union[str, int, float]] = {}
|
2021-04-29 09:51:25 +02:00
|
|
|
|
|
|
|
groups = [match[0]] + list(match.groups())
|
|
|
|
for i, group in enumerate(groups):
|
2021-04-29 20:13:46 +02:00
|
|
|
vars[f"g{i}"] = group
|
|
|
|
|
2021-04-29 09:51:25 +02:00
|
|
|
try:
|
2021-04-29 20:13:46 +02:00
|
|
|
vars[f"i{i}"] = int(group)
|
2021-04-29 09:51:25 +02:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2021-04-29 20:13:46 +02:00
|
|
|
vars[f"f{i}"] = float(group)
|
2021-04-29 09:51:25 +02:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
result = eval(f"f{self._right!r}", vars)
|
|
|
|
return PurePath(result)
|
2021-04-29 09:51:25 +02:00
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
return False
|
2021-04-29 09:51:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class RuleParseException(Exception):
|
|
|
|
line: "Line"
|
|
|
|
reason: str
|
|
|
|
|
|
|
|
def pretty_print(self) -> None:
|
|
|
|
print(f"Error parsing rule on line {self.line.line_nr}:")
|
|
|
|
print(self.line.line)
|
|
|
|
spaces = " " * self.line.index
|
|
|
|
print(f"{spaces}^--- {self.reason}")
|
|
|
|
|
|
|
|
|
|
|
|
class Line:
|
|
|
|
def __init__(self, line: str, line_nr: int):
|
|
|
|
self._line = line
|
|
|
|
self._line_nr = line_nr
|
|
|
|
self._index = 0
|
|
|
|
|
|
|
|
def get(self) -> Optional[str]:
|
|
|
|
if self._index < len(self._line):
|
|
|
|
return self._line[self._index]
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def line(self) -> str:
|
|
|
|
return self._line
|
|
|
|
|
|
|
|
@property
|
|
|
|
def line_nr(self) -> str:
|
|
|
|
return self._line
|
|
|
|
|
|
|
|
@property
|
|
|
|
def index(self) -> int:
|
|
|
|
return self._index
|
|
|
|
|
|
|
|
@index.setter
|
|
|
|
def index(self, index: int) -> None:
|
|
|
|
self._index = index
|
|
|
|
|
|
|
|
def advance(self) -> None:
|
|
|
|
self._index += 1
|
|
|
|
|
|
|
|
def expect(self, string: str) -> None:
|
|
|
|
for char in string:
|
|
|
|
if self.get() == char:
|
|
|
|
self.advance()
|
|
|
|
else:
|
|
|
|
raise RuleParseException(self, f"Expected {char!r}")
|
|
|
|
|
|
|
|
|
|
|
|
QUOTATION_MARKS = {'"', "'"}
|
|
|
|
|
|
|
|
|
|
|
|
def parse_string_literal(line: Line) -> str:
|
|
|
|
escaped = False
|
2021-04-29 20:13:46 +02:00
|
|
|
|
|
|
|
# Points to first character of string literal
|
|
|
|
start_index = line.index
|
2021-04-29 09:51:25 +02:00
|
|
|
|
|
|
|
quotation_mark = line.get()
|
|
|
|
if quotation_mark not in QUOTATION_MARKS:
|
|
|
|
# This should never happen as long as this function is only called from
|
|
|
|
# parse_string.
|
|
|
|
raise RuleParseException(line, "Invalid quotation mark")
|
|
|
|
line.advance()
|
|
|
|
|
|
|
|
while c := line.get():
|
|
|
|
if escaped:
|
|
|
|
escaped = False
|
|
|
|
line.advance()
|
|
|
|
elif c == quotation_mark:
|
|
|
|
line.advance()
|
2021-04-29 20:13:46 +02:00
|
|
|
stop_index = line.index
|
|
|
|
literal = line.line[start_index:stop_index]
|
|
|
|
return ast.literal_eval(literal)
|
2021-04-29 09:51:25 +02:00
|
|
|
elif c == "\\":
|
|
|
|
escaped = True
|
|
|
|
line.advance()
|
|
|
|
else:
|
|
|
|
line.advance()
|
|
|
|
|
|
|
|
raise RuleParseException(line, "Expected end of string literal")
|
|
|
|
|
|
|
|
|
|
|
|
def parse_until_space_or_eol(line: Line) -> str:
|
|
|
|
result = []
|
|
|
|
while c := line.get():
|
|
|
|
if c == " ":
|
|
|
|
break
|
|
|
|
result.append(c)
|
|
|
|
line.advance()
|
|
|
|
|
|
|
|
return "".join(result)
|
|
|
|
|
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
def parse_string(line: Line) -> Union[str, bool]:
|
2021-04-29 09:51:25 +02:00
|
|
|
if line.get() in QUOTATION_MARKS:
|
|
|
|
return parse_string_literal(line)
|
|
|
|
else:
|
2021-04-29 20:13:46 +02:00
|
|
|
string = parse_until_space_or_eol(line)
|
|
|
|
if string == "!":
|
|
|
|
return True
|
|
|
|
return string
|
2021-04-29 09:51:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def parse_arrow(line: Line) -> str:
|
|
|
|
line.expect("-")
|
|
|
|
|
|
|
|
name = []
|
|
|
|
while True:
|
|
|
|
if c := line.get():
|
|
|
|
if c == "-":
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
name.append(c)
|
|
|
|
line.advance()
|
|
|
|
else:
|
|
|
|
raise RuleParseException(line, "Expected rest of arrow")
|
|
|
|
|
|
|
|
line.expect("->")
|
|
|
|
return "".join(name)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_rule(line: Line) -> Rule:
|
2021-04-29 20:13:46 +02:00
|
|
|
# Parse left side
|
|
|
|
leftindex = line.index
|
2021-04-29 09:51:25 +02:00
|
|
|
left = parse_string(line)
|
2021-04-29 20:13:46 +02:00
|
|
|
if isinstance(left, bool):
|
|
|
|
line.index = leftindex
|
|
|
|
raise RuleParseException(line, "Left side can't be '!'")
|
|
|
|
|
|
|
|
# Parse arrow
|
2021-04-29 09:51:25 +02:00
|
|
|
line.expect(" ")
|
|
|
|
arrowindex = line.index
|
|
|
|
arrowname = parse_arrow(line)
|
|
|
|
|
2021-04-29 20:13:46 +02:00
|
|
|
# Parse right side
|
|
|
|
if line.get():
|
|
|
|
line.expect(" ")
|
|
|
|
right = parse_string(line)
|
|
|
|
else:
|
|
|
|
right = False
|
|
|
|
rightpath: Union[PurePath, bool]
|
|
|
|
if isinstance(right, bool):
|
|
|
|
rightpath = right
|
|
|
|
else:
|
|
|
|
rightpath = PurePath(right)
|
|
|
|
|
|
|
|
# Dispatch
|
2021-04-29 09:51:25 +02:00
|
|
|
if arrowname == "":
|
2021-04-29 20:13:46 +02:00
|
|
|
return NormalRule(PurePath(left), rightpath)
|
2021-04-29 09:51:25 +02:00
|
|
|
elif arrowname == "exact":
|
2021-04-29 20:13:46 +02:00
|
|
|
return ExactRule(PurePath(left), rightpath)
|
2021-04-29 09:51:25 +02:00
|
|
|
elif arrowname == "re":
|
|
|
|
return ReRule(left, right)
|
|
|
|
else:
|
|
|
|
line.index = arrowindex + 1 # For nicer error message
|
|
|
|
raise RuleParseException(line, "Invalid arrow name")
|
|
|
|
|
|
|
|
|
|
|
|
class Transformer:
|
|
|
|
def __init__(self, rules: str):
|
|
|
|
"""
|
|
|
|
May throw a RuleParseException.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._rules = []
|
|
|
|
for i, line in enumerate(rules.split("\n")):
|
|
|
|
line = line.strip()
|
|
|
|
if line:
|
|
|
|
self._rules.append(parse_rule(Line(line, i)))
|
|
|
|
|
2021-04-29 16:52:00 +02:00
|
|
|
def transform(self, path: PurePath) -> Optional[PurePath]:
|
2021-04-29 09:51:25 +02:00
|
|
|
for rule in self._rules:
|
2021-04-29 20:13:46 +02:00
|
|
|
result = rule.transform(path)
|
|
|
|
if isinstance(result, PurePath):
|
2021-04-29 09:51:25 +02:00
|
|
|
return result
|
2021-04-29 20:13:46 +02:00
|
|
|
elif result: # Exclamation mark
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
continue
|
2021-04-29 09:51:25 +02:00
|
|
|
|
2021-05-15 14:03:15 +02:00
|
|
|
return path
|