Skip to content

Commit

Permalink
Python 3.10 (#12)
Browse files Browse the repository at this point in the history
* Add support for Python 3.10

* Isort

* Fix pylint errors

* Fix typing

* Change pylint config

* Fix typing

* Improve typing

* Remove unused import

* Disable consider-alternative-union-syntax
  • Loading branch information
bieniu authored Oct 2, 2021
1 parent 6546efb commit 36029d4
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 46 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ jobs:
strategy:
max-parallel: 4
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
python-version: [3.8, 3.9, 3.10.0-rc.2]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
4 changes: 4 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

[MASTER]
load-plugins=pylint.extensions.code_style,pylint.extensions.typing,pylint_strict_informational,pylint.extensions.docparams
disable=consider-alternative-union-syntax
46 changes: 23 additions & 23 deletions gios/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""
Python wrapper for getting air quality data from GIOS.
"""
from __future__ import annotations

import logging
from contextlib import suppress
from typing import Any, Dict, List, Optional, cast
from http import HTTPStatus
from typing import Any, Final

from aiohttp import ClientSession
from dacite import from_dict
Expand All @@ -15,38 +18,36 @@
ATTR_INDEX_LEVEL,
ATTR_NAME,
ATTR_VALUE,
HTTP_OK,
URL_INDEXES,
URL_SENSOR,
URL_STATION,
URL_STATIONS,
)
from .model import GiosSensors

_LOGGER = logging.getLogger(__name__)
_LOGGER: Final = logging.getLogger(__name__)


class Gios: # pylint:disable=(too-few-public-methods
class Gios: # pylint:disable=too-few-public-methods
"""Main class to perform GIOS API requests"""

def __init__(self, station_id: int, session: ClientSession) -> None:
"""Initialize."""
self.station_id = station_id
self.latitude: Optional[float] = None
self.longitude: Optional[float] = None
self.station_name: Optional[str] = None
self._station_data: List[Dict[str, Any]] = []
self.latitude: float | None = None
self.longitude: float | None = None
self.station_name: str | None = None
self._station_data: list[dict[str, Any]] = []

self.session = session

async def async_update(self) -> GiosSensors: # pylint:disable=too-many-branches
"""Update GIOS data."""
data: Dict[str, Dict[str, Any]] = {}
invalid_sensors: List[str] = []
data: dict[str, dict[str, Any]] = {}
invalid_sensors: list[str] = []

if not self.station_name:
stations = await self._get_stations()
if not stations:
if not (stations := await self._get_stations()):
raise ApiError("Invalid measuring stations list from GIOS API")

for station in stations:
Expand Down Expand Up @@ -112,39 +113,38 @@ async def async_update(self) -> GiosSensors: # pylint:disable=too-many-branches

return from_dict(data_class=GiosSensors, data=data)

async def _get_stations(self) -> List[Dict[str, Any]]:
async def _get_stations(self) -> Any:
"""Retreive list of measuring stations."""
return cast(List[Dict[str, Any]], await self._async_get(URL_STATIONS))
return await self._async_get(URL_STATIONS)

async def _get_station(self) -> List[Dict[str, Any]]:
async def _get_station(self) -> Any:
"""Retreive measuring station data."""
url = URL_STATION.format(self.station_id)
return cast(List[Dict[str, Any]], await self._async_get(url))
return await self._async_get(url)

async def _get_all_sensors(self, sensors: Dict[str, Any]) -> Dict[str, Any]:
async def _get_all_sensors(self, sensors: dict[str, Any]) -> dict[str, Any]:
"""Retreive all sensors data."""
data: Dict[str, Any] = {}
data: dict[str, Any] = {}
for sensor in sensors:
sensor_data = await self._get_sensor(sensors[sensor][ATTR_ID])
data[sensor] = sensor_data
return data

async def _get_sensor(self, sensor: int) -> Dict[str, Any]:
async def _get_sensor(self, sensor: int) -> Any:
"""Retreive sensor data."""
url = URL_SENSOR.format(sensor)
return await self._async_get(url)

async def _get_indexes(self) -> Dict[str, Any]:
async def _get_indexes(self) -> Any:
"""Retreive indexes data."""
url = URL_INDEXES.format(self.station_id)
return await self._async_get(url)

async def _async_get(self, url: str) -> Dict[str, Any]:
async def _async_get(self, url: str) -> Any:
"""Retreive data from GIOS API."""
data: Dict[str, Any] = {}
async with self.session.get(url) as resp:
_LOGGER.debug("Data retrieved from %s, status: %s", url, resp.status)
if resp.status != HTTP_OK:
if resp.status != HTTPStatus.OK.value:
_LOGGER.warning("Invalid response from GIOS API: %s", resp.status)
raise ApiError(str(resp.status))
data = await resp.json()
Expand Down
23 changes: 12 additions & 11 deletions gios/const.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Constants for GIOS library."""
ATTR_AQI: str = "AQI"
ATTR_ID: str = "id"
ATTR_INDEX: str = "index"
ATTR_INDEX_LEVEL: str = "{}IndexLevel"
ATTR_NAME: str = "name"
ATTR_VALUE: str = "value"
from typing import Final

HTTP_OK: int = 200
URL_INDEXES: str = "http://api.gios.gov.pl/pjp-api/rest/aqindex/getIndex/{}"
URL_SENSOR: str = "http://api.gios.gov.pl/pjp-api/rest/data/getData/{}"
URL_STATION: str = "http://api.gios.gov.pl/pjp-api/rest/station/sensors/{}"
URL_STATIONS: str = "http://api.gios.gov.pl/pjp-api/rest/station/findAll"
ATTR_AQI: Final[str] = "AQI"
ATTR_ID: Final[str] = "id"
ATTR_INDEX: Final[str] = "index"
ATTR_INDEX_LEVEL: Final[str] = "{}IndexLevel"
ATTR_NAME: Final[str] = "name"
ATTR_VALUE: Final[str] = "value"

URL_INDEXES: Final[str] = "http://api.gios.gov.pl/pjp-api/rest/aqindex/getIndex/{}"
URL_SENSOR: Final[str] = "http://api.gios.gov.pl/pjp-api/rest/data/getData/{}"
URL_STATION: Final[str] = "http://api.gios.gov.pl/pjp-api/rest/station/sensors/{}"
URL_STATIONS: Final[str] = "http://api.gios.gov.pl/pjp-api/rest/station/findAll"
3 changes: 2 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pylint
pytest
pytest-asyncio
pytest-cov
pytest-error-for-skips
pytest-error-for-skips
pylint_strict_informational
29 changes: 21 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/usr/bin/env python
#!/usr/bin/env python3
from setuptools import setup


with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
with open("README.md", "r", encoding="utf-8") as file:
long_description = file.read()

with open("requirements.txt", "r", encoding="utf-8") as file:
install_requires=list(val.strip() for val in file.readlines())

with open("requirements-test.txt", "r", encoding="utf-8") as file:
tests_require=list(val.strip() for val in file.readlines())

setup(
name="gios",
version="2.0.0",
version="2.1.0",
author="Maciej Bieniek",
description="Python wrapper for getting air quality data from GIOŚ servers.",
long_description=long_description,
Expand All @@ -17,13 +23,20 @@
license="Apache-2.0 License",
packages=["gios"],
package_data={"gios": ["py.typed"]},
python_requires=">=3.6",
install_requires=list(val.strip() for val in open("requirements.txt")),
python_requires=">=3.8",
install_requires=install_requires,
classifiers=[
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3 :: Only",
"Typing :: Typed",
],
setup_requires=("pytest-runner"),
tests_require=list(val.strip() for val in open("requirements-test.txt")),
tests_require=tests_require,
)

0 comments on commit 36029d4

Please sign in to comment.