Skip to content

Commit

Permalink
Extract common patterns for re-use in the linting script too
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJCLaw committed Sep 14, 2024
1 parent c31859c commit baca586
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
14 changes: 6 additions & 8 deletions scripts/lint-filenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@
#

from pathlib import Path
import re

from srawn_utils import ISSUE_FILENAME_PATTERN, YEAR_FOLDER_PATTERN

status = True

print("Verifying minutes filenames.")

repo = Path(".")

filename_regex = re.compile("^20\d{2}-\d{2}-\d{2}-srawn-\d{2}\.md$")
folder_regex = re.compile("^SR20\d{2}$")

year_folders = [f for f in repo.iterdir() if f.is_dir()]

for folder in year_folders:
result = re.match(folder_regex, folder.name)
result = YEAR_FOLDER_PATTERN.match(folder.name)

if result is not None:
print(f"Found valid folder name: {folder.name}")
Expand All @@ -49,10 +47,10 @@
if f.is_dir():
print(f"\tFound nested directory: {folder.stem}/{f.stem}")
status = False

# Verify file name
file_result = re.match(filename_regex, f.name)

file_result = ISSUE_FILENAME_PATTERN.match(f.name)

if file_result is None:
print(f"\tFound bad file name: {f.name}")
Expand Down
13 changes: 5 additions & 8 deletions scripts/srawn_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from collections.abc import Iterator
from pathlib import Path

YEAR_FOLDER_PATTERN = re.compile(r"^(SR20\d{2})$")
ISSUE_FILENAME_PATTERN = re.compile(r"^(20\d{2}-\d{2}-\d{2})-srawn-(\d{2})$")


class InvalidPath(ValueError):
pass
Expand All @@ -27,20 +30,14 @@ def date_text(self) -> str:


def parse_path(path: Path) -> ParsedIssuePath:
filename_match = re.match(
r"^(20\d{2}-\d{2}-\d{2})-srawn-(\d{2})$",
path.stem,
)
filename_match = ISSUE_FILENAME_PATTERN.match(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,
)
folder_match = YEAR_FOLDER_PATTERN.match(path.parent.name)
if not folder_match:
raise InvalidPath(
f"{path.parent.name!r} does not match format. Run the linter.",
Expand Down

0 comments on commit baca586

Please sign in to comment.