aboutsummaryrefslogtreecommitdiffstats
path: root/src/bbss/buttons.py
blob: 633dc0b9872b585944727d1a5d644f4852801ff9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from .lists import ListFile, ListFileEntry, ListFileIterator
from dataclasses import dataclass
from typing import cast
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 cast(ButtonListFileEntry, 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)