Skip to content

Commit

Permalink
flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
Wim-De-Clercq committed Oct 14, 2024
1 parent 845293d commit 6948a1d
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 51 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion crabpy/data/update_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 19 additions & 8 deletions crabpy/gateway/capakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"] = "*"
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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"}
Expand All @@ -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 = (
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = (
Expand Down
59 changes: 46 additions & 13 deletions crabpy/gateway/crab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from crabpy.gateway.exception import GatewayResourceNotFoundException
from crabpy.gateway.exception import GatewayRuntimeException


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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
)


Expand All @@ -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,
)
11 changes: 8 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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"


Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down
22 changes: 17 additions & 5 deletions tests/gateway/test_adressenregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
}

Expand Down Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 6948a1d

Please sign in to comment.