Skip to content

Commit

Permalink
Add warning if no patterns were found
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object committed Dec 28, 2023
1 parent 186c5cd commit bf40d9f
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions doc/src/hexdoc_hexcasting/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PatternStubProps(StripHiddenModel):
path: RelativePath
regex: re.Pattern[str]
per_world_value: str | None = "true"
required: bool = True
"""If `True` (the default), raise an error if no patterns were loaded from here."""


class HexProperties(StripHiddenModel):
Expand Down Expand Up @@ -116,6 +118,8 @@ def _load_stub_patterns(
logger.debug(f"Load pattern stub from {stub.path}")
stub_text = stub.path.read_text("utf-8")

patterns = list[PatternInfo]()

for match in stub.regex.finditer(stub_text):
groups = match.groupdict()
id = props.mod_loc(groups["name"])
Expand All @@ -125,9 +129,21 @@ def _load_stub_patterns(
else:
is_per_world = groups.get("is_per_world") == stub.per_world_value

yield PatternInfo(
id=id,
startdir=Direction[groups["startdir"]],
signature=groups["signature"],
is_per_world=is_per_world,
patterns.append(
PatternInfo(
id=id,
startdir=Direction[groups["startdir"]],
signature=groups["signature"],
is_per_world=is_per_world,
)
)

pretty_path = stub.path.resolve().relative_to(Path.cwd())

if stub.required and not patterns:
raise ValueError(
f"No patterns found in {pretty_path} (check the pattern regex)"
)

logger.info(f"Loaded {len(patterns)} patterns from {pretty_path}")
return patterns

0 comments on commit bf40d9f

Please sign in to comment.