Skip to content

Commit

Permalink
chore: configure ruff and remove legacy tools (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare authored Mar 10, 2024
1 parent 35bf34a commit e911e41
Show file tree
Hide file tree
Showing 17 changed files with 383 additions and 242 deletions.
21 changes: 5 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.1
hooks:
- id: pyupgrade
args:
- --py39-plus
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.253'
hooks:
- id: ruff
- repo: local
hooks:
- id: black
name: black
language: system
entry: black
types: [python]
- id: isort
name: isort
language: system
entry: isort
types: [python]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.3.2'
hooks:
- id: ruff
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
exclude: ^src/api/client.js$
Expand Down
13 changes: 10 additions & 3 deletions cridlib/get.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
"""Get a RaBe CRID."""

from __future__ import annotations

from datetime import datetime, timezone
from typing import Optional

from .lib import CRID, canonicalize_show
from .strategy import future, now, past


def get(timestamp: Optional[datetime] = None, fragment: str = "") -> CRID:
def get(timestamp: datetime | None = None, fragment: str = "") -> CRID:
"""Get a RaBe CRID.
Examples:
--------
You can get a CRID for a specific time.
```python
Expand All @@ -20,13 +24,16 @@ def get(timestamp: Optional[datetime] = None, fragment: str = "") -> CRID:
```
Parameters:
Args:
----
timestamp: Exact time you want a CRID for.
If left empty, a CRID for the current time is generated.
fragment: Optional fragment to add to the end of the CRID.
Returns:
-------
CRID: The generated CRID.
"""
_now = datetime.now(timezone.utc)
_ts = timestamp or _now
Expand Down
118 changes: 79 additions & 39 deletions cridlib/lib.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
"""Main Cridlib library."""

from __future__ import annotations

from datetime import datetime
from pathlib import PurePath
from typing import Optional
from typing import Self
from urllib.parse import parse_qs

from slugify import slugify
from uritools import uricompose, urisplit # type: ignore
from uritools import uricompose, urisplit # type: ignore[import-untyped]


def canonicalize_show(show: str) -> str:
"""Get the slug for a show.
Uses [python-slugify](https://github.com/un33k/python-slugify).
Parameters:
Args:
----
show: Name of show with non-ascii chars.
Returns:
-------
slugified show name.
"""
return slugify(show)

Expand Down Expand Up @@ -50,7 +58,8 @@ class CRIDMalformedMediaFragmentError(CRIDError):
class CRID:
"""Represent CRIDs and can parse, validate and render them.
Examples:
Examples
--------
Generate a CRID from an URL and render it's repr:
```python
>>> CRID("crid://rabe.ch/v1/test#t=clock=19930301T131200.00Z")
Expand All @@ -64,15 +73,19 @@ class CRID:
'crid://rabe.ch/v1/test#t=clock=19930301T131200.00Z'
```
""" # noqa: E501
def __init__(self, uri: Optional[str] = None) -> None:
"""
Parameters:
"""

def __init__(self: Self, uri: str | None = None) -> None:
"""Create new CRID.
Args:
----
uri: CRID URL to base the new CRID off of.
"""
self._show: Optional[str] = None
self._start: Optional[datetime] = None
self._show: str | None = None
self._start: datetime | None = None

self._uri = urisplit(uri)
if self.scheme != "crid":
Expand All @@ -89,7 +102,9 @@ def __init__(self, uri: Optional[str] = None) -> None:
# fragments are optional, but if provided we want them to contain t=code
if self.fragment:
try:
self._start = datetime.strptime(
# TODO(hairmare): investigate noqa for bug
# https://github.com/radiorabe/python-rabe-cridlib/issues/244
self._start = datetime.strptime( # noqa: DTZ007
parse_qs(parse_qs(self.fragment)["t"][0])["clock"][0],
"%Y%m%dT%H%M%S.%fZ",
)
Expand All @@ -98,69 +113,94 @@ def __init__(self, uri: Optional[str] = None) -> None:
except ValueError as ex: # pragma: no cover
raise CRIDMalformedMediaFragmentError(self.fragment, uri) from ex

def __str__(self) -> str:
"""
Returns:
CRID URL rendered as string.
def __str__(self: Self) -> str:
"""Stringfy.
Returns
-------
CRID URL rendered as string.
"""
return uricompose(*self._uri)

def __repr__(self) -> str:
def __repr__(self: Self) -> str:
"""Repr."""
_fqcn = f"{self.__class__.__module__}.{self.__class__.__qualname__}"
return f"<class '{_fqcn}' for '{str(self)}'>"
return f"<class '{_fqcn}' for '{self!s}'>"

@property
def scheme(self) -> str:
"""
Returns:
def scheme(self: Self) -> str:
"""Scheme.
Returns
-------
Scheme part of the CRID.
"""
return self._uri.scheme

@property
def authority(self) -> str:
"""
Returns:
def authority(self: Self) -> str:
"""Authority.
Returns
-------
Authority part (aka hostname) of CRID.
"""
return self._uri.authority

@property
def path(self) -> CRIDPath:
"""
Returns:
def path(self: Self) -> CRIDPath:
"""Path.
Returns
-------
Path part of CRID.
"""
return CRIDPath(self._uri.path)

@property
def fragment(self) -> str:
"""
Returns:
Framgment part of CRID.
def fragment(self: Self) -> str:
"""Fragment.
Returns
-------
Fragment part of CRID.
"""
return self._uri.fragment

@property
def version(self) -> str:
"""
Returns:
def version(self: Self) -> str:
"""Version.
Returns
-------
Version from CRIDs path.
"""
return self._version

@property
def show(self) -> Optional[str]:
"""
Returns:
def show(self: Self) -> str | None:
"""Slug.
Returns
-------
Show slug from CRIDs path.
"""
return self._show

@property
def start(self) -> Optional[datetime]:
"""
Returns:
def start(self: Self) -> datetime | None:
"""Start time.
Returns
-------
Start time form CRIDs media fragment.
"""
return self._start
8 changes: 7 additions & 1 deletion cridlib/parse.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
"""Parse an existing CRID."""

from .lib import CRID


def parse(value: str) -> CRID:
"""Get CRID dataclass from CRID.
Parameters:
Args:
----
value: CRID URL as a string.
Returns:
-------
CRID: The parsed CRID.
"""
return CRID(value)
14 changes: 9 additions & 5 deletions cridlib/strategy/future.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Handle shows in the future."""

from datetime import datetime, timezone
from pathlib import PurePath

from uritools import urisplit # type: ignore
from uritools import urisplit # type: ignore[import-untyped]

from ..util import get_session
from cridlib.util import get_session

__LIBRETIME_INFOV2_URL = (
"https://airtime.service.int.rabe.ch/api/live-info-v2/format/json"
Expand All @@ -16,13 +18,15 @@ def get_show(future: datetime) -> str: # pragma: no cover
Only returns a show for the next seven days because everything futher than
that is considered unreliable as of early 2023.
Parameters:
Args:
----
future: Date to get the show name for.
Returns:
-------
Name of the show scheduled for `future`.
"""
"""
_resp = get_session().get(
__LIBRETIME_INFOV2_URL,
params={
Expand All @@ -37,7 +41,7 @@ def get_show(future: datetime) -> str: # pragma: no cover
_next = _json.get("shows").get("next")
for _show in _next:
_start = datetime.fromisoformat(_show.get("starts")).replace(
tzinfo=timezone.utc
tzinfo=timezone.utc,
)
_end = datetime.fromisoformat(_show.get("ends")).replace(tzinfo=timezone.utc)
if _start <= future <= _end:
Expand Down
17 changes: 10 additions & 7 deletions cridlib/strategy/now.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import xml.etree.ElementTree as ET
"""Handle currently running show."""

import xml.etree.ElementTree as ET # noqa: N817
from pathlib import PurePath

from uritools import urisplit # type: ignore
from uritools import urisplit # type: ignore[import-untyped]

from ..util import get_session
from cridlib.util import get_session

__SONGTICKER_URL = "https://songticker.rabe.ch/songticker/0.9.3/current.xml"


def get_show() -> str:
"""Return the currently running show.
"""Get the currently running show.
Calls the the [nowplaying](https://github.com/radiorabe/nowplaying)
songticker's API.
Returns:
Returns
-------
Name of the currently running show.
"""
"""
_resp = get_session().get(__SONGTICKER_URL, timeout=10)
_tree = ET.fromstring(_resp.text)
_tree = ET.fromstring(_resp.text) # noqa: S314
_path = PurePath(urisplit(_tree[3][1].text).path)
return _path.stem
10 changes: 7 additions & 3 deletions cridlib/strategy/past.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Handle shows from the past."""

from datetime import datetime

from ..util import get_session
from cridlib.util import get_session

__ARCHIV_BROADCASTS_URL = "https://archiv.rabe.ch/api/broadcasts/"

Expand All @@ -10,13 +12,15 @@ def get_show(past: datetime) -> str:
Asks the the [raar](https://github.com/radiorabe/raar) archive for the info.
Parameters:
Args:
----
past: Date to get the show name for.
Returns:
-------
Show name from the archive for `past`.
"""
"""
_url = f"{__ARCHIV_BROADCASTS_URL}{past.year}/{past.month:02d}/{past.day:02d}/{past.hour:02d}{past.minute:02d}{past.second:02d}" # noqa: E501
_resp = get_session().get(_url, timeout=10)
_json = _resp.json()
Expand Down
Loading

0 comments on commit e911e41

Please sign in to comment.