-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate
TocReader
class, load multiple interfaces
- Loading branch information
Showing
7 changed files
with
118 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterator, Mapping | ||
from functools import cached_property | ||
from pathlib import Path | ||
|
||
from typing_extensions import Self | ||
|
||
|
||
class TocReader(Mapping[str, str]): | ||
"""Extracts key-value pairs from TOC files.""" | ||
|
||
def __init__(self, contents: str) -> None: | ||
self._entries = { | ||
k: v | ||
for e in contents.splitlines() | ||
if e.startswith('##') | ||
for k, v in (map(str.strip, e.lstrip('#').partition(':')[::2]),) | ||
if k | ||
} | ||
|
||
def __iter__(self) -> Iterator[str]: | ||
return iter(self._entries) | ||
|
||
def __getitem__(self, key: str, /) -> str: | ||
return self._entries[key] | ||
|
||
def __len__(self) -> int: | ||
return len(self._entries) | ||
|
||
@classmethod | ||
def from_path(cls, path: Path) -> Self: | ||
return cls(path.read_text(encoding='utf-8-sig', errors='replace')) | ||
|
||
@cached_property | ||
def interfaces(self) -> list[int]: | ||
interface = self.get('Interface') | ||
return list(map(int, interface.split(','))) if interface else [] |
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,50 @@ | ||
from __future__ import annotations | ||
|
||
import importlib.resources | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from instawow.matchers.addon_toc import TocReader | ||
|
||
|
||
@pytest.fixture | ||
def fake_addon_toc(): | ||
with importlib.resources.as_file( | ||
importlib.resources.files() / 'fixtures' / 'FakeAddon' / 'FakeAddon.toc' | ||
) as file: | ||
yield file | ||
|
||
|
||
def test_loading_toc_from_path(fake_addon_toc: Path): | ||
TocReader.from_path(fake_addon_toc) | ||
with pytest.raises(FileNotFoundError): | ||
TocReader.from_path(fake_addon_toc.parent / 'MissingAddon.toc') | ||
|
||
|
||
def test_parsing_toc_entries(fake_addon_toc: Path): | ||
toc_reader = TocReader.from_path(fake_addon_toc) | ||
assert dict(toc_reader) == { | ||
'Normal': 'Normal entry', | ||
'Compact': 'Compact entry', | ||
} | ||
|
||
|
||
def test_toc_entry_indexing(fake_addon_toc: Path): | ||
toc_reader = TocReader.from_path(fake_addon_toc) | ||
assert toc_reader['Normal'] == 'Normal entry' | ||
assert toc_reader['Compact'] == 'Compact entry' | ||
assert toc_reader.get('Indented') is None | ||
assert toc_reader.get('Comment') is None | ||
assert toc_reader.get('Nonexistent') is None | ||
|
||
|
||
def test_toc_interfaces(): | ||
toc_reader = TocReader('## Interface: 10100') | ||
assert toc_reader.interfaces == [10100] | ||
|
||
toc_reader = TocReader('## Interface: 10100, 20200') | ||
assert toc_reader.interfaces == [10100, 20200] | ||
|
||
toc_reader = TocReader('## Interface: 10100 , 20200 ') | ||
assert toc_reader.interfaces == [10100, 20200] |
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