From ca30a43450214fd9d4290bc95f0e340d82cf7b0d Mon Sep 17 00:00:00 2001 From: alyx Date: Fri, 24 Nov 2023 02:15:43 -0500 Subject: Lists, buttons --- src/bbss/buttons.py | 28 ++++++++++++++++++++++++++++ src/bbss/lists.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/bbss/buttons.py create mode 100644 src/bbss/lists.py (limited to 'src/bbss') 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) -- cgit v1.2.3-70-g09d2