Skip to content

Commit

Permalink
mediainfo: Allow empty remap file and read lazily
Browse files Browse the repository at this point in the history
Fixes #310
  • Loading branch information
iamkroot committed Aug 3, 2024
1 parent 7f56a86 commit e5dc2a9
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions trakt_scrobbler/mediainfo_remap.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def __str__(self):


class RemapFile(BaseModel):
rules: List[RemapRule]
rules: List[RemapRule] = Field(default_factory=list)


def read_file(file: Path) -> List[RemapRule]:
Expand All @@ -298,13 +298,18 @@ def read_file(file: Path) -> List[RemapRule]:
return RemapFile.model_validate(data).rules


rules = read_file(REMAP_FILE_PATH)
if rules:
logger.debug(f"Read {len(rules)} remap {pluralize(len(rules), 'rule')} from {REMAP_FILE_PATH}")
_rules: Optional[List[RemapRule]] = None


def apply_remap_rules(path: Optional[str], media_info: dict):
for rule in rules:
global _rules
if _rules is None:
# read from file on first use
_rules = read_file(REMAP_FILE_PATH)
if _rules:
logger.debug(f"Read {len(_rules)} remap {pluralize(len(_rules), 'rule')} from {REMAP_FILE_PATH}")

for rule in _rules:
upd = rule.apply(path, media_info)
if upd is not None:
return upd
Expand Down

0 comments on commit e5dc2a9

Please sign in to comment.