Skip to content

Commit

Permalink
chore: formatting with ruff and more type cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare committed Mar 25, 2024
1 parent b405d64 commit e0fd679
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 337 deletions.
31 changes: 5 additions & 26 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
repos:
- repo: local
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: black
name: black
language: system
entry: black
types: [python]
- id: isort
name: isort
language: system
entry: isort
types: [python]
- id: flake8
name: flake8
language: system
entry: flake8
types: [python]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies:
- types-openpyxl
- types-pytz
- types-python-dateutil
- types-tqdm
- id: ruff
- id: ruff-format
- 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
5 changes: 3 additions & 2 deletions docs/gen_ref_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file:
nav_file.writelines(nav.build_literate_nav())

readme = Path("README.md").open("r")
with mkdocs_gen_files.open("index.md", "w") as index_file:
with Path("README.md").open("r") as readme, mkdocs_gen_files.open(
"index.md", "w"
) as index_file:
index_file.writelines(readme.read())
253 changes: 54 additions & 199 deletions poetry.lock

Large diffs are not rendered by default.

30 changes: 12 additions & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ suisa_sendemeldung = 'suisa_sendemeldung.suisa_sendemeldung:main'

[tool.poetry.dependencies]
python = "^3.11"
acrclient = "^0.4.1"
acrclient = "^0.4.4"
ConfigArgParse = "^1.5.5"
iso3901 = "^0.3.0.post1"
openpyxl = "^3.1.2"
Expand All @@ -25,42 +25,36 @@ tqdm = "^4.65.0"
Babel = "^2.12.1"

[tool.poetry.group.dev.dependencies]
black = ">=23.3,<25.0"
flake8 = ">=6,<8"
flake8-debugger = "^4.1.2"
flake8-docstrings = "^1.7.0"
flake8-isort = "^6.0.0"
flake8-string-format = "^0.3.0"
flake8-tuple = "^0.4.1"
freezegun = "^1.2.2"
isort = "^5.12.0"
mock = "^5.0.2"
pytest = ">=7.4,<9.0"
pytest-cov = ">=4.1,<6.0"
pytest-env = ">=0.8.2,<1.2.0"
pytest-pylint = ">=0.19,<0.22"
requests-mock = "^1.11.0"
types-requests = "^2.31.0.1"
wheel = ">=0.40,<0.44"
mkdocs = "^1.5.3"
mkdocs-material = "^9.5.14"
mkdocs-gen-files = "^0.5.0"
mkdocs-literate-nav = "^0.6.1"
mkdocs-section-index = "^0.3.8"
mkdocs-autorefs = "^1.0.1"
mkdocstrings = {extras = ["python"], version = "^0.24.1"}
mock = "^5.0.2"
pytest = ">=7.4,<9.0"
pytest-cov = ">=4.1,<6.0"
pytest-env = ">=0.8.2,<1.2.0"
pytest-mypy = "^0.10.3"
pytest-pylint = ">=0.19,<0.22"
pytest-ruff = "^0.3.1"
requests-mock = "^1.11.0"
ruff = "^0.3.4"
types-openpyxl = "^3.1.0.20240311"
types-python-dateutil = "^2.9.0.20240316"
types-pytz = "^2024.1.0.20240203"
types-requests = "^2.31.0.1"
types-tqdm = "^4.66.0.20240106"
wheel = ">=0.40,<0.44"

[tool.isort]
profile = "black"

[tool.pytest.ini_options]
minversion = "7.4"
addopts = "--doctest-modules --cov=suisa_sendemeldung --pylint --cov-fail-under=100 --ignore=docs/ --mypy"
addopts = "--doctest-modules --cov=suisa_sendemeldung --cov-fail-under=100 --ignore=docs/ --mypy --ruff"
filterwarnings = [
"ignore::DeprecationWarning:pylint",
]
Expand Down
78 changes: 78 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# [ruff](https://docs.astral.sh/ruff/) config
#
# templated with https://github.com/radiorabe/backstage-software-templates

[lint]
select = [
"F", # pyflakes
"E", # pycodestyle errors
"I", # isort
"C90", # mccabe
"N", # pep8-naming
"D", # pydocstyle
"UP", # pyupgrade
"ANN", # flake8-annotations
"ASYNC", # flake8-async
"S", # flake8-bandit
"BLE", # flake8-blind-exception
"FBT", # flake8-boolean-trap
"B", # flake8-bugbear
"A", # flake8-builtins
"COM", # flake8-commas
"C4", # flake8-comprehensions
"DTZ", # flake8-datetimez
"T10", # flake8-debugger
"EM", # flake8-errmsg
"EXE", # flake8-executable
"FA", # flake8-future-annotations
"ISC", # flake8-implicit-str-concat
"ICN", # flake8-import-conventions
"G", # flake8-logging-format
"INP", # flake8-no-pep420
"PIE", # flake8-pie
"T20", # flake8-print
"PYI", # flake8-pyi
"PT", # flake8-pytest-style
"Q", # flake8-quotes
"RSE", # flake8-raise
"RET", # flake8-return
"SLF", # flake8-self
"SLOT", # flake8-slots
"SIM", # flake8-simplify
"TID", # flake8-tidy-imports
"TCH", # flake8-type-checking
"INT", # flake8-gettext
"ARG", # flake8-unused-arguments
"PTH", # flake8-use-pathlib
"TD", # flake8-todos
"ERA", # eradicate
"PGH", # pygrep-hooks
"PL", # Pylint
"TRY", # tryceratops
"PERF", # Perflint
"RUF", # ruff specific rules
]
ignore = [
"D203", # we prefer blank-line-before-class (D211) for black compat
"D213", # we prefer multi-line-summary-first-line (D212)
"COM812", # ignore due to conflict with formatter
"ISC001", # ignore due to conflict with formatter
]

[lint.per-file-ignores]
"tests/**/*.py" = [
"D", # pydocstyle is optional for tests
"ANN", # flake8-annotations are optional for tests
"S101", # assert is allow in tests
"S105", # tests may have hardcoded secrets
"S106", # tests may have hardcoded passwords
"S108", # /tmp is allowed in tests since it's expected to be mocked
"DTZ00", # tests often run in UTC
"INP001", # tests do not need a dunder init
]
"**/__init__.py" = [
"D104", # dunder init does not need a docstring because it might be empty
]
"docs/gen_ref_pages.py" = [
"INP001", # mkdocs does not need a dunder init
]
60 changes: 36 additions & 24 deletions suisa_sendemeldung/acrclient.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""module containing the ACRCloud client."""

from __future__ import annotations

from datetime import date, datetime, timedelta
from typing import Any, Self

import pytz
from acrclient import Client
from acrclient.models import GetBmCsProjectsResultsParams
from tqdm import tqdm


Expand All @@ -21,25 +25,28 @@ class ACRClient(Client):
# timezone of ACRCloud
ACR_TIMEZONE = "UTC"

def __init__(self, bearer_token, base_url="https://eu-api-v2.acrcloud.com"):
def __init__(
self: Self, bearer_token: str, base_url: str = "https://eu-api-v2.acrcloud.com"
) -> None:
"""Init subclass with default_date."""
super().__init__(bearer_token=bearer_token, base_url=base_url)
self.default_date = date.today() - timedelta(days=1)
self.default_date: date = date.today() - timedelta(days=1) # noqa: DTZ011

def get_data(
self,
project_id,
stream_id,
requested_date=None,
timezone=ACR_TIMEZONE,
):
self: Self,
project_id: int,
stream_id: str,
requested_date: date | None = None,
timezone: str = ACR_TIMEZONE,
) -> Any: # noqa: ANN401
"""Fetch metadata from ACRCloud for `stream_id`.
Arguments:
---------
project_id: The Project ID of the stream.
stream_id: The ID of the stream.
requested_date (optional): The date of the entries you want (default: yesterday).
timezone (optional): The timezone to use for localization.
requested_date: The date of the entries you want (default: yesterday).
timezone: The timezone to use for localization.
Returns:
-------
Expand All @@ -51,27 +58,31 @@ def get_data(
data = self.get_bm_cs_projects_results(
project_id=project_id,
stream_id=stream_id,
params={
"date": requested_date.strftime("%Y%m%d"),
},
params=GetBmCsProjectsResultsParams(
type="day",
date=requested_date.strftime("%Y%m%d"),
min_duration=None,
max_duration=None,
isrc_country=None,
),
)
for entry in data:
metadata = entry.get("metadata")
ts_utc = pytz.utc.localize(
datetime.strptime(metadata.get("timestamp_utc"), ACRClient.TS_FMT),
datetime.strptime(metadata.get("timestamp_utc"), ACRClient.TS_FMT), # noqa: DTZ007
)
ts_local = ts_utc.astimezone(pytz.timezone(timezone))
metadata.update({"timestamp_local": ts_local.strftime(ACRClient.TS_FMT)})

return data

def get_interval_data(
self,
project_id,
stream_id,
start,
end,
timezone=ACR_TIMEZONE,
def get_interval_data( # noqa: PLR0913, ANN201
self: Self,
project_id: int,
stream_id: str,
start: date,
end: date,
timezone: str = ACR_TIMEZONE,
): # pylint: disable-msg=too-many-locals,too-many-arguments
"""Get data specified by interval from start to end.
Expand All @@ -92,7 +103,7 @@ def get_interval_data(
# if we have to localize the timestamps we may need more data
if timezone != ACRClient.ACR_TIMEZONE:
# compute utc offset
offset = pytz.timezone(timezone).utcoffset(datetime.now())
offset = pytz.timezone(timezone).utcoffset(datetime.now()) # noqa: DTZ005
# decrease start by 1 day if we're ahead of utc
if offset > timedelta(seconds=1):
computed_start = start - timedelta(days=1)
Expand All @@ -113,7 +124,8 @@ def get_interval_data(
dates.append(ptr)
ptr += timedelta(days=1)
data = []
# make the prefix longer by this amount so tqdm lines up with the one in the main code
# make the prefix longer by this amount so tqdm lines up with
# the one in the main code
ljust_amount: int = 27
for ptr in tqdm(dates, desc="load ACRCloud data".ljust(ljust_amount)):
data += self.get_data(
Expand All @@ -128,7 +140,7 @@ def get_interval_data(
for entry in reversed(data):
metadata = entry.get("metadata")
timestamp = metadata.get("timestamp_local")
timestamp_date = datetime.strptime(timestamp, ACRClient.TS_FMT).date()
timestamp_date = datetime.strptime(timestamp, ACRClient.TS_FMT).date() # noqa: DTZ007
if timestamp_date < start or timestamp_date > end:
data.remove(entry)

Expand Down
Loading

0 comments on commit e0fd679

Please sign in to comment.