diff options
author | alyx <alyx@aleteoryx.me> | 2023-11-24 02:15:43 -0500 |
---|---|---|
committer | alyx <alyx@aleteoryx.me> | 2023-11-24 02:15:43 -0500 |
commit | ca30a43450214fd9d4290bc95f0e340d82cf7b0d (patch) | |
tree | 9dcada98d9bc3afe9b5ee57f144076a53dff7cbb /src | |
parent | fea4addaa6659aff6b324ee4db4b4223df22d591 (diff) | |
download | bbss.py-ca30a43450214fd9d4290bc95f0e340d82cf7b0d.tar.gz bbss.py-ca30a43450214fd9d4290bc95f0e340d82cf7b0d.tar.bz2 bbss.py-ca30a43450214fd9d4290bc95f0e340d82cf7b0d.zip |
Lists, buttons
Diffstat (limited to 'src')
-rw-r--r-- | src/bbss/buttons.py | 28 | ||||
-rw-r--r-- | src/bbss/lists.py | 44 |
2 files changed, 72 insertions, 0 deletions
diff --git a/src/bbss/buttons.py b/src/bbss/buttons.py new file mode 100644 index 0000000..63de4d1 --- /dev/null +++ b/src/bbss/buttons.py @@ -0,0 +1,28 @@ +from .lists import ListFile, ListFileEntry, ListFileIterator +from dataclasses import dataclass +import requests + +@dataclass(frozen=True) +class ButtonListFileEntry(ListFileEntry): + root: str + + def url(self) -> str: + return self.root + "/" + self.entry + + def exists(self) -> bool: + return requests.head(self.url()).ok + +class ButtonListFileIterator(ListFileIterator): + def __next__(self) -> ButtonListFileEntry: + return super().__next__() + +@dataclass(frozen=True) +class ButtonListFile(ListFile): + def __init__(self, contents: str, root: str): + super().__init__(contents) + self.root = root + + @classmethod + def from_url(cls, url: str): + (root, _, _) = url.rpartition('/') + return cls(requests.get(url, stream = False).text, root) diff --git a/src/bbss/lists.py b/src/bbss/lists.py new file mode 100644 index 0000000..3d4779e --- /dev/null +++ b/src/bbss/lists.py @@ -0,0 +1,44 @@ +from dataclasses import dataclass +from collections.abc import Iterable, Iterator +import requests + +@dataclass(frozen=True) +class ListFileEntry: + entry: str + comment: str + +class ListFileIterator(Iterator[ListFileEntry]): + def __init__(self, entries: list[ListFileEntry]): + self._entries = entries + self.index = -1 + def __next__(self) -> ListFileEntry: + if self.index == len(self._entries): + raise StopIteration + self.index += 1 + return self._entries[self.index] + +@dataclass(frozen=True) +class ListFile(Iterable[ListFileEntry]): + def __init__(self, contents: str): + self._contents = contents + acc = [] + comment_acc = "" + for line in str.splitlines(): + if line.startswith("#"): + continue + elif line.startswith("##"): + comment_acc += line[2:].split() + else: + acc += ListFileEntry(comment = comment_acc if comment_acc else None, entry = line) + + self._entries = acc + + def __iter__(self) -> ListFileIterator: + return ListFileIterator(self._entries) + + def entries(self) -> list[ListFileEntry]: + return self._entries + + @classmethod + def from_url(cls, url: str): + return cls(requests.get(url, stream = False).text) |