2022-06-17 07:26:54 +10:00
|
|
|
from abc import ABC, abstractmethod
|
2022-06-29 15:52:00 +10:00
|
|
|
from collections.abc import AsyncGenerator, Awaitable, Callable
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Optional, Type, TypeVar
|
2022-06-17 07:26:54 +10:00
|
|
|
|
|
|
|
T = TypeVar("T")
|
2022-08-08 09:50:52 +10:00
|
|
|
|
2022-06-17 07:26:54 +10:00
|
|
|
Fetcher = Callable[[str], Awaitable[Optional[str]]]
|
|
|
|
|
2022-08-08 09:50:52 +10:00
|
|
|
|
2022-06-17 07:26:54 +10:00
|
|
|
ICON_FOLDER = "📂"
|
|
|
|
ICON_PACKAGE = "📦"
|
2022-08-08 13:19:32 +10:00
|
|
|
ICON_OPTICAL = "📀"
|
|
|
|
ICON_QCOW = "🐮"
|
2022-06-17 07:26:54 +10:00
|
|
|
ICON_OTHER = " "
|
|
|
|
|
|
|
|
|
2022-08-09 08:49:14 +10:00
|
|
|
class ContentError(Exception):
|
|
|
|
"""An error raised when indexed content appears to be invalid.
|
|
|
|
|
|
|
|
Errors of this type are raised when repo-autoindex is able to successfully
|
|
|
|
retrieve content and determine a repository type but fails to parse
|
|
|
|
repository metadata. For example, a corrupt yum repository may cause this
|
|
|
|
error to be raised.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class FetcherError(Exception):
|
|
|
|
# Internal-only error used to separate exceptions raised by fetchers from
|
|
|
|
# exceptions raised by anything else.
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-06-17 07:26:54 +10:00
|
|
|
@dataclass
|
|
|
|
class GeneratedIndex:
|
2022-08-08 09:50:52 +10:00
|
|
|
"""A single HTML index page generated by repo-autoindex."""
|
|
|
|
|
2022-06-17 07:26:54 +10:00
|
|
|
content: str
|
2022-08-08 09:50:52 +10:00
|
|
|
"""The content of this index page (an HTML document)."""
|
|
|
|
|
2022-06-17 07:26:54 +10:00
|
|
|
relative_dir: str = "."
|
2022-08-08 09:50:52 +10:00
|
|
|
"""The directory of this index page, relative to the root of the indexed
|
|
|
|
repository.
|
|
|
|
"""
|
2022-06-17 07:26:54 +10:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class IndexEntry:
|
|
|
|
href: str
|
|
|
|
text: str
|
2022-06-29 15:44:46 +10:00
|
|
|
time: str = ""
|
|
|
|
size: str = ""
|
2022-06-17 07:26:54 +10:00
|
|
|
padding: str = ""
|
|
|
|
icon: str = ICON_OTHER
|
|
|
|
|
|
|
|
|
|
|
|
class Repo(ABC):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
base_url: str,
|
|
|
|
entry_point_content: str,
|
|
|
|
fetcher: Fetcher,
|
|
|
|
):
|
|
|
|
self.base_url = base_url
|
|
|
|
self.entry_point_content = entry_point_content
|
|
|
|
self.fetcher = fetcher
|
|
|
|
|
|
|
|
@abstractmethod
|
2022-06-29 16:19:05 +10:00
|
|
|
def render_index(
|
2022-06-17 07:26:54 +10:00
|
|
|
self, index_href_suffix: str
|
|
|
|
) -> AsyncGenerator[GeneratedIndex, None]:
|
2022-07-07 14:02:46 +10:00
|
|
|
pass # pragma: no cover
|
2022-06-17 07:26:54 +10:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@abstractmethod
|
|
|
|
async def probe(cls: Type[T], fetcher: Fetcher, url: str) -> Optional[T]:
|
|
|
|
"""Determine if a specified URL seems to point at a repository of this type.
|
|
|
|
|
|
|
|
If so, returns an initialized Repo of a concrete subtype. If not, returns None.
|
|
|
|
"""
|
2022-07-07 14:02:46 +10:00
|
|
|
pass # pragma: no cover
|