From 6948a1d8ab2e05cd0b005ab2bc8c7ffc21f8997c Mon Sep 17 00:00:00 2001 From: Wim De Clercq Date: Mon, 14 Oct 2024 11:32:17 +0200 Subject: [PATCH] flake8 --- .pre-commit-config.yaml | 1 - crabpy/data/update_data.py | 2 +- crabpy/gateway/capakey.py | 27 ++++++++---- crabpy/gateway/crab.py | 59 ++++++++++++++++++++------ tests/__init__.py | 15 ++++--- tests/conftest.py | 11 +++-- tests/gateway/test_adressenregister.py | 22 +++++++--- tests/gateway/test_capakey.py | 10 ++--- tests/gateway/test_capakey_cached.py | 20 ++++++--- tests/test_wsse.py | 6 +-- 10 files changed, 122 insertions(+), 51 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1523830..4704df7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,6 @@ repos: hooks: - id: black types: [python] - args: [--line-length=90] - repo: https://github.com/pycqa/flake8 rev: 6.0.0 hooks: diff --git a/crabpy/data/update_data.py b/crabpy/data/update_data.py index 63ee261..a700eda 100644 --- a/crabpy/data/update_data.py +++ b/crabpy/data/update_data.py @@ -84,7 +84,7 @@ def create_gemeenten_json(filename="gemeenten.json"): province_niscodes = [int(provincie["niscode"]) for provincie in provincies] provincie = None gemeenten = [] - for index, row in df.where(pd.notnull(df), None).iterrows(): + for _, row in df.where(pd.notnull(df), None).iterrows(): if row["Code INS"] in province_niscodes: provincie = row["Code INS"] continue diff --git a/crabpy/gateway/capakey.py b/crabpy/gateway/capakey.py index da19809..428ff8e 100644 --- a/crabpy/gateway/capakey.py +++ b/crabpy/gateway/capakey.py @@ -13,10 +13,11 @@ from crabpy.gateway.exception import GatewayResourceNotFoundException from crabpy.gateway.exception import GatewayRuntimeException + log = logging.getLogger(__name__) -def capakey_rest_gateway_request(url, headers={}, params={}): +def capakey_rest_gateway_request(url, headers=None, params=None): """ Utility function that helps making requests to the CAPAKEY REST service. @@ -25,6 +26,8 @@ def capakey_rest_gateway_request(url, headers={}, params={}): :param dict params: Parameters to send with the URL. :returns: Result of the call. """ + headers = headers or {} + params = params or {} try: # calls to geoservices give a 403 if the user-agent is not set headers["user-agent"] = "*" @@ -53,7 +56,9 @@ class CapakeyRestGateway: caches = {} def __init__(self, **kwargs): - self.base_url = kwargs.get("base_url", "https://geo.api.vlaanderen.be/capakey/v2") + self.base_url = kwargs.get( + "base_url", "https://geo.api.vlaanderen.be/capakey/v2" + ) self.base_headers = {"Accept": "application/json"} cache_regions = ["permanent", "long", "short"] for cr in cache_regions: @@ -80,7 +85,8 @@ def _parse_centroid(center): @staticmethod def _parse_bounding_box(bounding_box): """ - Parse response bounding box from the CapakeyRestGateway to (MinimumX, MinimumY, MaximumX, MaximumY) + Parse response bounding box from the CapakeyRestGateway to + (MinimumX, MinimumY, MaximumX, MaximumY) :param bounding_box: response bounding box from the CapakeyRestGateway :return: (MinimumX, MinimumY, MaximumX, MaximumY) @@ -284,8 +290,10 @@ def get_sectie_by_id_and_afdeling(self, id, afdeling): def creator(): url = ( - self.base_url - + f"/municipality/{afdeling.gemeente.id}/department/{afdeling.id}/section/{id}" + f"{self.base_url}" + f"/municipality/{afdeling.gemeente.id}" + f"/department/{afdeling.id}" + f"/section/{id}" ) h = self.base_headers p = {"geometry": "full", "srs": "31370"} @@ -310,7 +318,8 @@ def parse_percid(self, capakey): import re match = re.match( - r"^([0-9]{5})([A-Z]{1})([0-9]{4})\/([0-9]{2})([A-Z\_]{1})([0-9]{3})$", capakey + r"^([0-9]{5})([A-Z]{1})([0-9]{4})\/([0-9]{2})([A-Z\_]{1})([0-9]{3})$", + capakey, ) if match: percid = ( @@ -431,7 +440,8 @@ def creator(): if self.caches["short"].is_configured: key = ( - f"get_perceel_by_id_and_sectie_rest#{id}#{sectie.id}#{sectie.afdeling.id}" + f"get_perceel_by_id_and_sectie_rest#{id}" + f"#{sectie.id}#{sectie.afdeling.id}" ) perceel = self.caches["short"].get_or_create(key, creator) else: @@ -851,7 +861,8 @@ def get_percid_from_capakey(capakey): import re match = re.match( - r"^([0-9]{5})([A-Z]{1})([0-9]{4})\/([0-9]{2})([A-Z\_]{1})([0-9]{3})$", capakey + r"^([0-9]{5})([A-Z]{1})([0-9]{4})\/([0-9]{2})([A-Z\_]{1})([0-9]{3})$", + capakey, ) if match: percid = ( diff --git a/crabpy/gateway/crab.py b/crabpy/gateway/crab.py index 2def09c..dc346f1 100644 --- a/crabpy/gateway/crab.py +++ b/crabpy/gateway/crab.py @@ -16,6 +16,7 @@ from crabpy.gateway.exception import GatewayResourceNotFoundException from crabpy.gateway.exception import GatewayRuntimeException + log = logging.getLogger(__name__) @@ -431,7 +432,8 @@ def _list_codeobject(self, function, sort, returnclass): def creator(): res = crab_gateway_request(self.client, function, sort) return [ - globals()[returnclass](r.Code, r.Naam, r.Definitie) for r in res.CodeItem + globals()[returnclass](r.Code, r.Naam, r.Definitie) + for r in res.CodeItem ] if self.caches["permanent"].is_configured: @@ -502,7 +504,9 @@ def list_aardterreinobjecten(self, sort=1): :rtype: A :class:`list` of :class:`Aardterreinobject` """ - return self._list_codeobject("ListAardTerreinobjecten", sort, "Aardterreinobject") + return self._list_codeobject( + "ListAardTerreinobjecten", sort, "Aardterreinobject" + ) def list_statushuisnummers(self, sort=1): """ @@ -976,7 +980,9 @@ def list_wegsegmenten_by_straat(self, straat): id = straat def creator(): - res = crab_gateway_request(self.client, "ListWegsegmentenByStraatnaamId", id) + res = crab_gateway_request( + self.client, "ListWegsegmentenByStraatnaamId", id + ) try: return [ Wegsegment(r.IdentificatorWegsegment, r.StatusWegsegment) @@ -1166,7 +1172,9 @@ def get_gebouw_by_id(self, id): """ def creator(): - res = crab_gateway_request(self.client, "GetGebouwByIdentificatorGebouw", id) + res = crab_gateway_request( + self.client, "GetGebouwByIdentificatorGebouw", id + ) if res is None: raise GatewayResourceNotFoundException() return Gebouw( @@ -1287,7 +1295,9 @@ def list_adresposities_by_huisnummer(self, huisnummer): id = huisnummer def creator(): - res = crab_gateway_request(self.client, "ListAdrespositiesByHuisnummerId", id) + res = crab_gateway_request( + self.client, "ListAdrespositiesByHuisnummerId", id + ) try: return [ Adrespositie(r.AdrespositieId, r.HerkomstAdrespositie) @@ -1416,7 +1426,9 @@ def get_adrespositie_by_id(self, id): """ def creator(): - res = crab_gateway_request(self.client, "GetAdrespositieByAdrespositieId", id) + res = crab_gateway_request( + self.client, "GetAdrespositieByAdrespositieId", id + ) if res is None: raise GatewayResourceNotFoundException() return Adrespositie( @@ -1919,7 +1931,9 @@ class Geometriemethodewegsegment(Codelijst): """ def __repr__(self): - return f"Geometriemethodewegsegment({self.id}, '{self.naam}', '{self.definitie}')" + return ( + f"Geometriemethodewegsegment({self.id}, '{self.naam}', '{self.definitie}')" + ) class Statusgebouw(Codelijst): @@ -2061,7 +2075,9 @@ def __unicode__(self): return f"{self.label} ({self.id})" def __repr__(self): - return f"Straat({self.id}, '{self.label}', {self.gemeente_id}, {self.status_id})" + return ( + f"Straat({self.id}, '{self.label}', {self.gemeente_id}, {self.status_id})" + ) def check_lazy_load_huisnummer(f): @@ -2166,7 +2182,10 @@ def __unicode__(self): return f"{self.huisnummer} ({self.id})" def __repr__(self): - return f"Huisnummer({self.id}, {self.status_id}, '{self.huisnummer}', {self.straat_id})" + return ( + f"Huisnummer({self.id}, {self.status_id}, " + f"'{self.huisnummer}', {self.straat_id})" + ) class Postkanton(GatewayObject): @@ -2281,7 +2300,9 @@ def wrapper(*args): class Wegsegment(GatewayObject): - def __init__(self, id, status, methode=None, geometrie=None, metadata=None, **kwargs): + def __init__( + self, id, status, methode=None, geometrie=None, metadata=None, **kwargs + ): self.id = id try: self.status_id = status.id @@ -2620,7 +2641,14 @@ class Subadres(GatewayObject): """ def __init__( - self, id, subadres, status, huisnummer_id=None, aard=None, metadata=None, **kwargs + self, + id, + subadres, + status, + huisnummer_id=None, + aard=None, + metadata=None, + **kwargs, ): self.id = int(id) self.subadres = subadres @@ -2683,7 +2711,10 @@ def __unicode__(self): return f"{self.subadres} ({self.id})" def __repr__(self): - return f"Subadres({self.id}, {self.status_id}, '{self.subadres}', {self.huisnummer_id})" + return ( + f"Subadres({self.id}, {self.status_id}, " + f"'{self.subadres}', {self.huisnummer_id})" + ) def check_lazy_load_adrespositie(f): @@ -2721,7 +2752,9 @@ class Adrespositie(GatewayObject): the `herkomst` and `aard` of each `Adrespositie` to know which one to pick. """ - def __init__(self, id, herkomst, geometrie=None, aard=None, metadata=None, **kwargs): + def __init__( + self, id, herkomst, geometrie=None, aard=None, metadata=None, **kwargs + ): self.id = id try: self.herkomst_id = herkomst.id diff --git a/tests/__init__.py b/tests/__init__.py index b0cf141..756fa4a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,16 +1,16 @@ +import configparser import contextlib import os -import configparser from crabpy.gateway import adressenregister + config = configparser.ConfigParser() TEST_DIR = os.path.dirname(__file__) config.read(os.path.join(TEST_DIR, "test.ini")) adressenregister.setup_cache( - {"long.backend": "dogpile.cache.null", "short.backend": "dogpile.cache.null"}, - None + {"long.backend": "dogpile.cache.null", "short.backend": "dogpile.cache.null"}, None ) @@ -36,11 +36,14 @@ def memory_cache(): "long.backend": "dogpile.cache.memory", "short.backend": "dogpile.cache.memory", }, - None + None, ) yield finally: adressenregister.setup_cache( - {"long.backend": "dogpile.cache.null", "short.backend": "dogpile.cache.null"}, - None + { + "long.backend": "dogpile.cache.null", + "short.backend": "dogpile.cache.null", + }, + None, ) diff --git a/tests/conftest.py b/tests/conftest.py index 5cacfae..3c9593c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,13 +1,14 @@ import json import os import re +from unittest.mock import Mock import pytest import responses -from unittest.mock import Mock from crabpy.gateway.crab import CrabGateway + CAPAKEY_URL = "https://geo.api.vlaanderen.be/capakey/v2" @@ -96,7 +97,9 @@ def department_response(mocked_responses): @pytest.fixture(scope="function") def department_sections_response(mocked_responses): url = re.compile( - r"{capakey}/municipality/\d+/department/\d+/section$".format(capakey=CAPAKEY_URL) + r"{capakey}/municipality/\d+/department/\d+/section$".format( + capakey=CAPAKEY_URL + ) ) mocked_responses.add( method="GET", url=url, json=load_json("department_sections.json") @@ -110,7 +113,9 @@ def department_section_response(mocked_responses): capakey=CAPAKEY_URL ) ) - mocked_responses.add(method="GET", url=url, json=load_json("department_section.json")) + mocked_responses.add( + method="GET", url=url, json=load_json("department_section.json") + ) @pytest.fixture(scope="function") diff --git a/tests/gateway/test_adressenregister.py b/tests/gateway/test_adressenregister.py index 0633820..e01c7a8 100644 --- a/tests/gateway/test_adressenregister.py +++ b/tests/gateway/test_adressenregister.py @@ -81,7 +81,9 @@ def create_client_get_straatnaam_item(): "gemeente": { "objectId": "11002", "detail": "https://api.basisregisters.vlaanderen.be/v1/gemeenten/11002", - "gemeentenaam": {"geografischeNaam": {"spelling": "Antwerpen", "taal": "nl"}}, + "gemeentenaam": { + "geografischeNaam": {"spelling": "Antwerpen", "taal": "nl"} + }, }, "straatnamen": [{"spelling": "Edelvalklaan", "taal": "nl"}], "homoniemToevoegingen": [], @@ -131,7 +133,9 @@ def create_client_get_adres_item(): "straatnaam": { "objectId": "93", "detail": "https://api.basisregisters.vlaanderen.be/v1/straatnamen/93", - "straatnaam": {"geografischeNaam": {"spelling": "Oudestraat", "taal": "nl"}}, + "straatnaam": { + "geografischeNaam": {"spelling": "Oudestraat", "taal": "nl"} + }, }, "huisnummer": "27", "busnummer": "A", @@ -160,7 +164,9 @@ def create_client_get_perceel_list_item(): "objectId": "13013C0384-02H003", "versieId": "2004-02-13T05:34:17+01:00", }, - "detail": "https://api.basisregisters.vlaanderen.be/v2/percelen/13013C0384-02H003", + "detail": ( + "https://api.basisregisters.vlaanderen.be/v2/percelen/13013C0384-02H003" + ), "perceelStatus": "gerealiseerd", } @@ -539,14 +545,20 @@ def test_straten(self, gateway, client): def test_provincie(self, gateway): g = Gemeente( - niscode="1", provincie_niscode="10000", naam="test-gemeente", gateway=gateway + niscode="1", + provincie_niscode="10000", + naam="test-gemeente", + gateway=gateway, ) provincie = g.provincie assert provincie.naam == "Antwerpen" def test_gewest(self, gateway): g = Gemeente( - niscode="1", provincie_niscode="10000", naam="test-gemeente", gateway=gateway + niscode="1", + provincie_niscode="10000", + naam="test-gemeente", + gateway=gateway, ) gewest = g.gewest assert gewest.naam == "Vlaams Gewest" diff --git a/tests/gateway/test_capakey.py b/tests/gateway/test_capakey.py index a557df5..b5e8ee9 100644 --- a/tests/gateway/test_capakey.py +++ b/tests/gateway/test_capakey.py @@ -14,11 +14,11 @@ from tests.conftest import CAPAKEY_URL -def connection_error(url, headers={}, params={}): +def connection_error(url, headers=None, params=None): raise requests.exceptions.ConnectionError -def request_exception(url, headers={}, params={}): +def request_exception(url, headers=None, params=None): raise requests.exceptions.RequestException @@ -193,10 +193,10 @@ def test_requests_connection(self): with pytest.raises(GatewayRuntimeException) as cm: capakey_rest_gateway_request("url") exception = cm.value.message - assert ( - exception - == "Could not execute request due to connection problems:\nConnectionError()" + expected = ( + "Could not execute request due to connection problems:\nConnectionError()" ) + assert exception == expected @patch("requests.get", MagicMock(side_effect=request_exception)) def test_requests_request_exception(self): diff --git a/tests/gateway/test_capakey_cached.py b/tests/gateway/test_capakey_cached.py index 62c4522..a88efda 100644 --- a/tests/gateway/test_capakey_cached.py +++ b/tests/gateway/test_capakey_cached.py @@ -1,6 +1,9 @@ import pytest -from crabpy.gateway.capakey import Gemeente, Afdeling, Sectie, Perceel +from crabpy.gateway.capakey import Afdeling +from crabpy.gateway.capakey import Gemeente +from crabpy.gateway.capakey import Perceel +from crabpy.gateway.capakey import Sectie @pytest.fixture(scope="function") @@ -24,7 +27,9 @@ class TestCapakeyRestCachedGateway: def test_cache_is_configured(self, capakey_rest_gateway): from dogpile.cache.backends.memory import MemoryBackend - assert isinstance(capakey_rest_gateway.caches["permanent"].backend, MemoryBackend) + assert isinstance( + capakey_rest_gateway.caches["permanent"].backend, MemoryBackend + ) assert capakey_rest_gateway.caches["permanent"].is_configured def test_list_gemeenten(self, capakey_rest_gateway, municipalities_response): @@ -65,7 +70,9 @@ def test_list_afdelingen( ): res = capakey_rest_gateway.list_kadastrale_afdelingen() assert isinstance(res, list) - assert capakey_rest_gateway.caches["permanent"].get("list_afdelingen_rest") == res + assert ( + capakey_rest_gateway.caches["permanent"].get("list_afdelingen_rest") == res + ) def test_list_afdelingen_by_gemeente( self, @@ -75,7 +82,8 @@ def test_list_afdelingen_by_gemeente( ): g = capakey_rest_gateway.get_gemeente_by_id(44021) assert ( - capakey_rest_gateway.caches["long"].get("get_gemeente_by_id_rest#44021") == g + capakey_rest_gateway.caches["long"].get("get_gemeente_by_id_rest#44021") + == g ) res = capakey_rest_gateway.list_kadastrale_afdelingen_by_gemeente(g) assert isinstance(res, list) @@ -108,7 +116,9 @@ def test_list_secties_by_afdeling_id( assert isinstance(res, list) assert len(res) == 1 assert ( - capakey_rest_gateway.caches["long"].get("list_secties_by_afdeling_rest#44021") + capakey_rest_gateway.caches["long"].get( + "list_secties_by_afdeling_rest#44021" + ) == res ) diff --git a/tests/test_wsse.py b/tests/test_wsse.py index 5e401d3..f355ab3 100644 --- a/tests/test_wsse.py +++ b/tests/test_wsse.py @@ -1,12 +1,10 @@ import unittest - -from crabpy.wsse import UsernameDigestToken - from base64 import b64encode +from suds.sax.element import Element from suds.wsse import wssens -from suds.sax.element import Element +from crabpy.wsse import UsernameDigestToken class UsernameDigestTokenTests(unittest.TestCase):