-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract common utils in rendering SRAWN
This removes a lot of duplicate handling & validation of path names in favour of having a single source of this logic. It also relocates a couple of instances of similar but separate logic to being alongside each other within the utils file so that even though they can't share code, they are at least defined next to each other. Alongside doing this, we also fix some warnings from Python due to the regexes not being marked as raw strings (such that '\d' was previously trying to be an escape at the string parsing level, rather than a regex escape).
- Loading branch information
1 parent
f61b699
commit bc5e68b
Showing
4 changed files
with
107 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import contextlib | ||
import dataclasses | ||
import datetime | ||
import re | ||
from collections.abc import Iterator | ||
from pathlib import Path | ||
|
||
|
||
class InvalidPath(ValueError): | ||
pass | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class ParsedIssuePath: | ||
path: Path | ||
date: datetime.date | ||
issue_number: str | ||
sryear: str | ||
|
||
@property | ||
def title(self) -> str: | ||
return f"{self.sryear} Issue {self.issue_number}" | ||
|
||
@property | ||
def date_text(self) -> str: | ||
return self.date.isoformat() | ||
|
||
|
||
def parse_path(path: Path) -> ParsedIssuePath: | ||
filename_match = re.match( | ||
r"^(20\d{2}-\d{2}-\d{2})-srawn-(\d{2})$", | ||
path.stem, | ||
) | ||
if not filename_match: | ||
raise InvalidPath( | ||
f"{path.stem!r} does not match format. Run the linter.", | ||
) | ||
date, issue_number = filename_match.groups() | ||
|
||
folder_match = re.match( | ||
r"^(SR20\d{2})$", | ||
path.parent.name, | ||
) | ||
if not folder_match: | ||
raise InvalidPath( | ||
f"{path.parent.name!r} does not match format. Run the linter.", | ||
) | ||
sryear, = folder_match.groups() | ||
|
||
return ParsedIssuePath( | ||
path, | ||
datetime.date.fromisoformat(date), | ||
issue_number, | ||
sryear, | ||
) | ||
|
||
|
||
@contextlib.contextmanager | ||
def exit_on_invalid() -> Iterator[None]: | ||
try: | ||
yield | ||
except InvalidPath as e: | ||
exit(str(e)) | ||
|
||
|
||
def get_years(root: Path) -> list[Path]: | ||
return sorted(root.glob("SR20*"), reverse=True) | ||
|
||
|
||
def get_year_issues(year_root: Path) -> list[ParsedIssuePath]: | ||
paths = sorted(year_root.glob("*.md")) | ||
return [parse_path(x) for x in paths] | ||
|
||
|
||
def get_all_issues(root: Path) -> list[ParsedIssuePath]: | ||
paths = sorted(root.glob("SR20*/*.md")) | ||
return [parse_path(x) for x in paths] |