aboutsummaryrefslogtreecommitdiffstats
path: root/src/bbss/sizes.py
diff options
context:
space:
mode:
authoralyx <alyx@aleteoryx.me>2023-11-25 13:25:26 -0500
committeralyx <alyx@aleteoryx.me>2023-11-25 13:25:26 -0500
commit646d743dce4a3704febfa28be3abce405823baef (patch)
tree3cdc1de58e91fe7c9dafd53e26b5d95d703e5fb9 /src/bbss/sizes.py
parent4d93c08e447f8d1d8d2a3206267856014adf4de7 (diff)
downloadbbss.py-646d743dce4a3704febfa28be3abce405823baef.tar.gz
bbss.py-646d743dce4a3704febfa28be3abce405823baef.tar.bz2
bbss.py-646d743dce4a3704febfa28be3abce405823baef.zip
Modelling, guts basically done.
Diffstat (limited to 'src/bbss/sizes.py')
-rw-r--r--src/bbss/sizes.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/bbss/sizes.py b/src/bbss/sizes.py
new file mode 100644
index 0000000..26818e9
--- /dev/null
+++ b/src/bbss/sizes.py
@@ -0,0 +1,38 @@
+from .lists import BaseListFile, ListFileEntry, parse_listfile
+from .buttons import ButtonListFile
+from dataclasses import dataclass
+from typing import Optional, Self
+from collections.abc import Sequence
+import requests
+
+@dataclass(frozen=True)
+class SizeListFileEntry(ListFileEntry):
+ root: str
+
+ def url(self) -> str:
+ return self.root + "/" + self.entry + "/list.txt"
+
+ def exists(self) -> bool:
+ return requests.head(self.url()).ok
+
+ def get(self) -> Optional[ButtonListFile]:
+ return ButtonListFile.from_url(self.url())
+
+class SizeListFile(BaseListFile[SizeListFileEntry]):
+ def __init__(self, contents: str, root: str):
+ super().__init__(contents)
+ self._root = root
+
+ def construct_entry(entry: str, comment: Optional[str]):
+ return SizeListFileEntry(entry, comment, root)
+ self._entries = parse_listfile(contents, construct_entry)
+
+ @classmethod
+ def from_url(cls, url: str) -> Optional[Self]:
+ (root, _, _) = url.rpartition('/')
+ req = requests.get(url, stream = False)
+ if not req.ok:
+ return None
+ return cls(req.text, root)
+
+__all__ = ["SizeListFileEntry", "SizeListFile"]