Skip to content

Commit 2725137

Browse files
committed
Format code using Ruff
1 parent dee9696 commit 2725137

File tree

14 files changed

+174
-79
lines changed

14 files changed

+174
-79
lines changed

.github/workflows/tests.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ jobs:
4747
pip install --requirement=requirements-test.txt
4848
pip install --editable=.[service]
4949
50-
- name: Run tests
51-
run: pytest
50+
- name: Run linters
51+
if: matrix.python-version != '3.6' && matrix.python-version != '3.7'
52+
run: |
53+
poe lint
54+
55+
- name: Run software tests
56+
run: |
57+
poe test
5258
5359
- name: Upload coverage to Codecov
5460
uses: codecov/codecov-action@v4

apicast/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
"""Access bee flight forecast information published by Deutscher Wetterdienst (DWD)"""
2+
23
__appname__ = "apicast"
34
__version__ = "0.8.6"

apicast/api.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
# (c) 2020-2021 Andreas Motl <[email protected]>
33
# License: GNU Affero General Public License, Version 3
44
import logging
5-
from typing import List, Dict
5+
from typing import Dict, List
66

77
from fastapi import FastAPI, Query
88
from fastapi.responses import HTMLResponse, PlainTextResponse
99

1010
from apicast import __appname__, __version__
11-
from apicast.core import (DwdBeeflightForecast, dwd_copyright, dwd_source,
12-
producer_link, producer_name)
11+
from apicast.core import (
12+
DwdBeeflightForecast,
13+
dwd_copyright,
14+
dwd_source,
15+
producer_link,
16+
producer_name,
17+
)
1318
from apicast.format import Formatter
1419

1520
app = FastAPI()
@@ -21,9 +26,11 @@
2126

2227
@app.get("/", response_class=HTMLResponse)
2328
def index():
24-
2529
appname = f"{__appname__} {__version__}"
26-
description = "Apicast acquires bee flight forecast information published by Deutscher Wetterdienst (DWD)."
30+
description = (
31+
"Apicast acquires bee flight forecast information "
32+
"published by Deutscher Wetterdienst (DWD)."
33+
)
2734

2835
data_index_items = []
2936
for location in dbf.get_station_slugs():
@@ -44,7 +51,7 @@ def index():
4451
</div>
4552
<div style="clear: both"/>
4653
</li>
47-
"""
54+
""" # noqa: E501
4855
data_index_items.append(item)
4956

5057
data_index_items_html = "\n".join(data_index_items)
@@ -105,12 +112,12 @@ def index():
105112
</ul>
106113
</body>
107114
</html>
108-
"""
115+
""" # noqa: E501
109116

110117

111118
@app.get("/robots.txt", response_class=PlainTextResponse)
112119
def robots():
113-
return f"""
120+
return """
114121
User-agent: *
115122
Disallow: /beeflight/
116123
""".strip()
@@ -135,7 +142,6 @@ def beeflight_forecast_by_slug(
135142
format: str = Query(default="json"),
136143
translate: bool = Query(default=False),
137144
):
138-
139145
station_slug = f"{state}/{station}"
140146

141147
try:
@@ -164,7 +170,7 @@ def beeflight_forecast_by_slug(
164170

165171

166172
def make_json_response(data: List[Dict], location: str = None):
167-
response = {
173+
return {
168174
"meta": {
169175
"source": dwd_source,
170176
"producer": f"{producer_name} - {producer_link}",
@@ -175,7 +181,6 @@ def make_json_response(data: List[Dict], location: str = None):
175181
},
176182
"data": data,
177183
}
178-
return response
179184

180185

181186
def start_service(listen_address, reload: bool = False):

apicast/cli.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def run():
5858
# Start HTTP service with dynamic code reloading
5959
apicast service --reload
6060
61-
"""
61+
""" # noqa: E501
6262

6363
name = f"{__appname__} {__version__}"
6464

@@ -89,7 +89,6 @@ def run():
8989

9090
# Run command.
9191
if options.stations:
92-
9392
if options.slugs:
9493
result = dbf.get_station_slugs()
9594
print("\n".join(result))
@@ -106,7 +105,6 @@ def run():
106105

107106

108107
def format_beeflight_forecast(result, format="json"):
109-
110108
if not result.data:
111109
raise ValueError("No data found or unable to parse")
112110

apicast/core.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
See also https://community.hiveeyes.org/t/dwd-prognose-bienenflug/787
1111
"""
12+
1213
import dataclasses
1314
from typing import List
1415

@@ -20,7 +21,6 @@
2021

2122
from apicast import __appname__, __version__
2223

23-
2424
user_agent = f"{__appname__}/{__version__}"
2525
dwd_source = "https://www.dwd.de/DE/leistungen/biene_flug/bienenflug.html"
2626
dwd_copyright = "© Deutscher Wetterdienst (DWD), Agricultural Meteorology Department"
@@ -54,15 +54,13 @@ def copy(self):
5454

5555

5656
class DwdBeeflightForecast:
57-
5857
baseurl = "https://www.dwd.de/DE/leistungen/biene_flug/bienenflug.json?cl2Categories_LeistungsId=bienenflug"
5958

6059
session = requests.Session()
6160
session.headers["User-Agent"] = user_agent
6261

6362
@ttl_cache(60 * 60 * 24)
6463
def get_states(self) -> List[State]:
65-
6664
states: List[State] = []
6765

6866
# Request federal states.
@@ -75,18 +73,16 @@ def get_states(self) -> List[State]:
7573

7674
states.append(state)
7775

78-
# sites.sort(key=operator.attrgetter("label"))
76+
# sites.sort(key=operator.attrgetter("label")) # noqa: ERA001
7977

8078
return states
8179

8280
@ttl_cache(60 * 60 * 24)
8381
def get_stations(self) -> List[Station]:
84-
8582
stations: List[Station] = []
8683

8784
# Request federal states.
8885
for state in self.get_states():
89-
9086
# Request sites.
9187
response = self.session.get(
9288
self.baseurl,
@@ -111,7 +107,7 @@ def get_stations(self) -> List[Station]:
111107

112108
stations.append(station)
113109

114-
# sites.sort(key=operator.attrgetter("label"))
110+
# sites.sort(key=operator.attrgetter("label")) # noqa: ERA001
115111

116112
return stations
117113

@@ -130,7 +126,6 @@ def get_station_by_slug(self, slug):
130126

131127
@ttl_cache(60 * 60)
132128
def get_data(self, station: Station) -> Result:
133-
134129
response = self.session.get(
135130
self.baseurl,
136131
params={
@@ -155,10 +150,7 @@ def get_data(self, station: Station) -> Result:
155150
data = self.parse_html_table(table)
156151

157152
# Ready.
158-
result = Result(
159-
station=station, station_name=station_name, data=data, footnote=footnote
160-
)
161-
return result
153+
return Result(station=station, station_name=station_name, data=data, footnote=footnote)
162154

163155
@staticmethod
164156
def parse_html_table(html):

apicast/format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ class Formatter:
4848
def __init__(self, result):
4949
self.result = deepcopy(result)
5050
self.data = self.result.data
51-
self.title = u"### Prognose des Bienenfluges in {}".format(self.result.station_name)
51+
self.title = "### Prognose des Bienenfluges in {}".format(self.result.station_name)
5252

5353
def translate(self):
54-
self.title = u"### Beeflight forecast for {}".format(self.result.station_name)
54+
self.title = "### Beeflight forecast for {}".format(self.result.station_name)
5555
for item in self.data:
5656
for index, slot in enumerate(item):
5757
for key, value in self.LABEL_MAP.items():
@@ -79,9 +79,9 @@ def machinify(self):
7979
item[key] = value
8080
return data
8181

82+
# ruff: noqa: T201
8283
def table_markdown(self):
8384
with io.StringIO() as buffer, redirect_stdout(buffer):
84-
8585
# Report about weather station / observation location
8686
print(self.title)
8787
print()

apicast/util.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def configure_http_logging(options):
3232
def normalize_options(options):
3333
normalized = {}
3434
for key, value in options.items():
35-
3635
# Add primary variant.
3736
key = key.strip("--<>")
3837
normalized[key] = value

pyproject.toml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
[tool.pytest.ini_options]
2+
addopts = """
3+
-ra -q --verbosity=3
4+
--cov --cov-report=term-missing --cov-report=xml
5+
"""
6+
minversion = "2.0"
7+
log_level = "DEBUG"
8+
log_cli_level = "DEBUG"
9+
testpaths = [
10+
"apicast",
11+
"test",
12+
]
13+
xfail_strict = true
14+
15+
[tool.coverage.run]
16+
source = ["apicast"]
17+
18+
[tool.coverage.report]
19+
show_missing = true
20+
fail_under = 0
21+
omit = [
22+
"test/*",
23+
]
24+
25+
[tool.ruff]
26+
line-length = 100
27+
extend-exclude = [
28+
]
29+
30+
[tool.ruff.lint]
31+
select = [
32+
# Pycodestyle
33+
"E",
34+
"W",
35+
# Pyflakes
36+
"F",
37+
# isort
38+
"I",
39+
# Bandit
40+
"S",
41+
# flake8-quotes
42+
"Q",
43+
# eradicate
44+
"ERA",
45+
# flake8-2020
46+
"YTT",
47+
# print
48+
"T20",
49+
# return
50+
"RET",
51+
# pyupgrade
52+
# "UP",
53+
# flake8-commas
54+
"COM",
55+
# future-annotations
56+
# "FA",
57+
# flake8-type-checking
58+
"TCH",
59+
# flake8-unused-arguments
60+
"ARG",
61+
# flake8-use-pathlib
62+
# "PTH"
63+
]
64+
extend-ignore = [
65+
# Unnecessary `elif` after `return` or `raise` statement.
66+
"RET505",
67+
"RET506",
68+
# No trailing commas.
69+
"COM812"
70+
]
71+
unfixable = ["ERA", "F401", "F841", "T20", "ERA001"]
72+
73+
[tool.ruff.lint.per-file-ignores]
74+
"apicast/cli.py" = ["T201"]
75+
"test/*" = ["S101"]
76+
77+
78+
# ===================
79+
# Tasks configuration
80+
# ===================
81+
82+
[tool.poe.tasks]
83+
format = [
84+
{cmd="ruff format"},
85+
{cmd="ruff check --fix"},
86+
]
87+
lint = [
88+
{cmd="ruff check"},
89+
]
90+
test = [
91+
{cmd="pytest"},
92+
]
93+
build = {cmd="python -m build"}
94+
check = ["lint", "test"]

requirements-test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
datadiff>=2.0,<3
22
fastapi[test]
33
marko<3
4+
poethepoet<0.26
45
pytest>=6.1.0,<8
56
pytest-cov<6
7+
ruff<0.4;python_version>='3.7'

setup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import os
33
from io import open
4-
from setuptools import setup, find_packages
4+
5+
from setuptools import find_packages, setup
56

67
here = os.path.abspath(os.path.dirname(__file__))
78
README = open(os.path.join(here, "README.rst"), encoding="UTF-8").read()
@@ -10,8 +11,8 @@
1011
name="apicast",
1112
version="0.8.6",
1213
description="Python client and HTTP service to access bee flight forecast "
13-
"information published by Deutscher Wetterdienst (DWD), the "
14-
"federal meteorological service in Germany.",
14+
"information published by Deutscher Wetterdienst (DWD), the "
15+
"federal meteorological service in Germany.",
1516
long_description=README,
1617
license="AGPL 3, EUPL 1.2",
1718
classifiers=[

0 commit comments

Comments
 (0)