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 bc5e68b commit ebd188a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
10 changes: 4 additions & 6 deletions scripts/lint-filenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

import re
from pathlib import Path

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 @@ -52,7 +50,7 @@

# 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
15 changes: 6 additions & 9 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}).md$")


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.name)
if not filename_match:
raise InvalidPath(
f"{path.stem!r} does not match format. Run the linter.",
f"{path.name!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 ebd188a

Please sign in to comment.