Skip to content

Commit

Permalink
Merge pull request #241 from OnroerendErfgoed/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
claeyswo committed Nov 24, 2023
2 parents ed18975 + 4734e59 commit f54e433
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.3.0 (23-11-2023)
------------------

- Homoniemen toevoegen bij straten (#238)

1.2.6 (16-10-2023)
------------------

Expand Down
83 changes: 69 additions & 14 deletions crabpy/gateway/adressenregister.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def get_deelgemeente_by_id(self, deelgemeente_id):
)

@LONG_CACHE.cache_on_arguments()
def list_straten(self, gemeente):
def list_straten(self, gemeente, include_homoniem=False):
"""
List all `straten` in a `Gemeente`.
Expand All @@ -389,7 +389,7 @@ def list_straten(self, gemeente):
if gemeente is None:
return []
return [
Straat.from_list_response(straat, self)
Straat.from_list_response(straat, self, include_homoniem)
for straat in self.client.get_straatnamen(niscode=gemeente.niscode)
]

Expand Down Expand Up @@ -671,7 +671,16 @@ class Straat(GatewayObject):
A street object is always located in one and exactly one :class:`Gemeente`.
"""

def __init__(self, id_, gateway, gemeente=AUTO, status=AUTO, naam=AUTO, uri=AUTO):
def __init__(
self,
id_,
gateway,
gemeente=AUTO,
status=AUTO,
naam=AUTO,
uri=AUTO,
homoniem=AUTO,
):
super().__init__(gateway)
self.id = id_
if naam is not AUTO:
Expand All @@ -680,6 +689,12 @@ def __init__(self, id_, gateway, gemeente=AUTO, status=AUTO, naam=AUTO, uri=AUTO
if not callable(naam):
raise ValueError("naam must be a callable")
self.naam = naam
if homoniem is not AUTO:
if isinstance(homoniem, (str, type(None))):
homoniem = CallableString(homoniem)
if not callable(homoniem):
raise ValueError("homoniem must be a callable")
self.homoniem = homoniem
if status is not AUTO:
self.status = status
if gemeente is not AUTO:
Expand All @@ -688,12 +703,19 @@ def __init__(self, id_, gateway, gemeente=AUTO, status=AUTO, naam=AUTO, uri=AUTO
self.uri = uri

@classmethod
def from_list_response(cls, straat, gateway):
def from_list_response(cls, straat, gateway, include_homoniem=False):
homoniem = (
straat["homoniemToevoeging"]["geografischeNaam"]["spelling"]
if straat.get("homoniemToevoeging")
else None
)
naam = straat["straatnaam"]["geografischeNaam"]["spelling"]
return Straat(
id_=straat["identificator"]["objectId"],
status=straat["straatnaamStatus"],
naam=straat["straatnaam"]["geografischeNaam"]["spelling"],
naam=f"{naam} ({homoniem})" if (homoniem and include_homoniem) else naam,
uri=straat["identificator"]["id"],
homoniem=homoniem,
gateway=gateway,
)

Expand All @@ -703,7 +725,7 @@ def from_get_response(cls, straat, gateway):
res._source_json = straat
return res

def naam(self, taal="nl"):
def naam(self, taal="nl", include_homoniem=False):
naam = next(
(
straatnaam["spelling"]
Expand All @@ -712,10 +734,34 @@ def naam(self, taal="nl"):
),
None,
)
if naam:
return naam

return self._source_json["straatnamen"][0]["spelling"]
naam = naam or self._source_json["straatnamen"][0]["spelling"]

return (
f"{naam} ({self.homoniem()})"
if (self.homoniem() and include_homoniem)
else naam
)

def homoniem(self, taal="nl"):
homoniem = next(
(
homoniemtoevoeging["spelling"]
for homoniemtoevoeging in self._source_json.get(
"homoniemToevoegingen", []
)
if homoniemtoevoeging["taal"] == taal
),
None,
)
if homoniem:
return homoniem

return (
self._source_json["homoniemToevoegingen"][0]["spelling"]
if self._source_json.get("homoniemToevoegingen")
else None
)

@LazyProperty
def uri(self):
Expand Down Expand Up @@ -822,12 +868,19 @@ def status(self):
return self._source_json["adresStatus"]

@LazyProperty
def straat(self):
def straat(self, include_homoniem=False):
homoniem = (
self._source_json["homoniemToevoeging"]["geografischeNaam"]["spelling"]
if self._source_json.get("homoniemToevoeging")
else None
)
naam = self._source_json["straatnaam"]["straatnaam"]["geografischeNaam"][
"spelling"
]
return Straat(
id_=self._source_json["straatnaam"]["objectId"],
naam=self._source_json["straatnaam"]["straatnaam"]["geografischeNaam"][
"spelling"
],
naam=f"{naam} ({homoniem})" if (homoniem and include_homoniem) else naam,
homoniem=homoniem,
gateway=self.gateway,
)

Expand Down Expand Up @@ -920,7 +973,9 @@ class Gebouw(GatewayObject):
A building.
"""

def __init__(self, id_, gateway, status=AUTO, percelen=AUTO, geojson=AUTO, uri=AUTO):
def __init__(
self, id_, gateway, status=AUTO, percelen=AUTO, geojson=AUTO, uri=AUTO
):
super().__init__(gateway=gateway)
self.id = id_
if status is not AUTO:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="crabpy",
version="1.2.6",
version="1.3.0",
description="Interact with geographical webservices by Informatie Vlaanderen.",
long_description=open("README.rst").read() + "\n\n" + open("CHANGES.rst").read(),
author="Onroerend Erfgoed",
Expand Down

0 comments on commit f54e433

Please sign in to comment.