replace code with regex

This commit is contained in:
AJ Slater 2024-02-21 17:03:42 -08:00
parent 0ff6feb3ea
commit 4b9015878d
2 changed files with 3 additions and 19 deletions

View File

@ -77,27 +77,9 @@ class ComicFilenameParser:
value = value.strip("'").strip() value = value.strip("'").strip()
return value.strip('"').strip() return value.strip('"').strip()
def _parenthify_double_underscores(self) -> str:
"""Replace double underscores with parens."""
parts = self._unparsed_path.split("__")
num_parts = len(parts)
print(f"{num_parts=} {num_parts % 2}")
if num_parts < 3 or not num_parts % 2:
return self._unparsed_path
index = 0
mode = " ("
parenthified = parts[index]
index += 1
while index < len(parts):
parenthified += mode + parts[index]
print(f"{parenthified=}")
mode = ") " if mode == " (" else ") "
index += 1
return parenthified.strip()
def _clean_dividers(self): def _clean_dividers(self):
"""Replace non space dividers and clean extra spaces out of string.""" """Replace non space dividers and clean extra spaces out of string."""
data = self._parenthify_double_underscores() data = self._unparsed_path
# Simple substitutions # Simple substitutions
for regex, pair in REGEX_SUBS.items(): for regex, pair in REGEX_SUBS.items():

View File

@ -86,8 +86,10 @@ _SPACE_EQUIVALENT_RE = re_compile(r"_")
_EXTRA_SPACES_RE = re_compile(r"\s\s+") _EXTRA_SPACES_RE = re_compile(r"\s\s+")
_LEFT_PAREN_EQUIVALENT_RE = re_compile(r"\[") _LEFT_PAREN_EQUIVALENT_RE = re_compile(r"\[")
_RIGHT_PAREN_EQUIVALENT_RE = re_compile(r"\]") _RIGHT_PAREN_EQUIVALENT_RE = re_compile(r"\]")
_DOUBLE_UNDERSCORE_RE = re_compile(r"__(.*)__")
REGEX_SUBS: MappingProxyType[re.Pattern, tuple[str, int]] = MappingProxyType( REGEX_SUBS: MappingProxyType[re.Pattern, tuple[str, int]] = MappingProxyType(
{ {
_DOUBLE_UNDERSCORE_RE: (r"(\1)", 0),
_TOKEN_DIVIDERS_RE: (TOKEN_DELIMETER, 1), _TOKEN_DIVIDERS_RE: (TOKEN_DELIMETER, 1),
_SPACE_EQUIVALENT_RE: (r" ", 0), _SPACE_EQUIVALENT_RE: (r" ", 0),
_EXTRA_SPACES_RE: (r" ", 0), _EXTRA_SPACES_RE: (r" ", 0),