aboutsummaryrefslogtreecommitdiffstats
path: root/src/bbss/buttons.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/bbss/buttons.py')
-rw-r--r--src/bbss/buttons.py28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/bbss/buttons.py b/src/bbss/buttons.py
index 633dc0b..77ddcf4 100644
--- a/src/bbss/buttons.py
+++ b/src/bbss/buttons.py
@@ -1,6 +1,7 @@
-from .lists import ListFile, ListFileEntry, ListFileIterator
+from .lists import BaseListFile, ListFileEntry, parse_listfile
from dataclasses import dataclass
-from typing import cast
+from typing import Optional, Self
+from collections.abc import Sequence
import requests
@dataclass(frozen=True)
@@ -13,17 +14,24 @@ class ButtonListFileEntry(ListFileEntry):
def exists(self) -> bool:
return requests.head(self.url()).ok
-class ButtonListFileIterator(ListFileIterator):
- def __next__(self) -> ButtonListFileEntry:
- return cast(ButtonListFileEntry, super().__next__())
+ def get(self) -> requests.Response:
+ return requests.get(self.url())
-@dataclass(frozen=True)
-class ButtonListFile(ListFile):
+class ButtonListFile(BaseListFile[ButtonListFileEntry]):
def __init__(self, contents: str, root: str):
super().__init__(contents)
- self.root = root
+ self._root = root
+
+ def construct_entry(entry: str, comment: Optional[str]):
+ return ButtonListFileEntry(entry, comment, root)
+ self._entries = parse_listfile(contents, construct_entry)
@classmethod
- def from_url(cls, url: str):
+ def from_url(cls, url: str) -> Optional[Self]:
(root, _, _) = url.rpartition('/')
- return cls(requests.get(url, stream = False).text, root)
+ req = requests.get(url, stream = False)
+ if not req.ok:
+ return None
+ return cls(req.text, root)
+
+__all__ = ["ButtonListFileEntry", "ButtonListFile"]