Skip to content

Commit

Permalink
HB2-66 add source info during places import
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric Hofstetter committed Nov 9, 2020
1 parent 374d0b8 commit dd289b9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions homebytwo/importers/geonames.py
Expand Up @@ -24,7 +24,6 @@ def import_places_from_geonames(
see https://www.geonames.org/countries/), e.g. `CH` or `allCountries`
:param file: path to local unzipped file. If provided, the `scope` parameter
will be ignored and the local file will be used.
:param update: should existing places be updated with the downloaded data.
"""
try:
file = file or get_geonames_remote_file(scope)
Expand All @@ -36,8 +35,9 @@ def import_places_from_geonames(
with file:
count = get_csv_line_count(file, header=False)
data = parse_places_from_csv(file)
source_info = f"geonames.org {scope}"

return save_places_from_generator(data, count)
return save_places_from_generator(data, count, source_info)


def get_geonames_remote_file(scope: str = "allCountries") -> TextIOWrapper:
Expand Down
4 changes: 2 additions & 2 deletions homebytwo/importers/swissnames3d.py
Expand Up @@ -66,7 +66,6 @@ def import_places_from_swissnames3d(
see http://mapref.org/CoordinateReferenceFrameChangeLV03.LV95.html#Zweig1098
:param file: path to local unzipped file. if provided, the `projection`
parameter will be ignored.
:param update: should existing places be updated with the downloaded data.
"""
try:
file = file or get_swissnames3d_remote_file(projection=projection)
Expand All @@ -78,8 +77,9 @@ def import_places_from_swissnames3d(
with file:
count = get_csv_line_count(file, header=True)
data = parse_places_from_csv(file, projection=projection)
source_info = f"SwissNAMES3D {projection}"

return save_places_from_generator(data=data, count=count)
return save_places_from_generator(data, count, source_info)


def get_swissnames3d_remote_file(projection: str = "LV95") -> TextIOWrapper:
Expand Down
6 changes: 3 additions & 3 deletions homebytwo/importers/tests/test_import_places.py
Expand Up @@ -78,7 +78,7 @@ def test_save_places_from_generator():
for place in places
)
msg = "Created 20 new places and updated 5 places. "
assert save_places_from_generator(data, count=20) == msg
assert save_places_from_generator(data, count=20, source_info="test_source") == msg
assert Place.objects.count() == 25


Expand All @@ -87,7 +87,7 @@ def test_save_places_from_generator_empty():
places = []
data = (place for place in places)
msg = "Created 0 new places and updated 0 places. "
assert save_places_from_generator(data, count=0) == msg
assert save_places_from_generator(data, count=0, source_info="test_source") == msg
assert Place.objects.count() == 0


Expand All @@ -111,7 +111,7 @@ def test_save_places_from_generator_bad_place_type(capsys):
for place in [place]
)
msg = "Created 0 new places and updated 0 places. "
assert save_places_from_generator(data, count=1) == msg
assert save_places_from_generator(data, count=1, source_info="test_source") == msg
captured = capsys.readouterr()
assert "Place type code: BADCODE does not exist.\n" in captured.out
assert Place.objects.count() == 0
Expand Down
6 changes: 4 additions & 2 deletions homebytwo/importers/utils.py
Expand Up @@ -146,7 +146,9 @@ def get_csv_line_count(csv_file: TextIOWrapper, header: bool) -> int:
return max(count - int(header), 0)


def save_places_from_generator(data: Iterator[PlaceTuple], count: int) -> str:
def save_places_from_generator(
data: Iterator[PlaceTuple], count: int, source_info: str
) -> str:
"""
Save places from csv parsers in geonames.py or swissnames3d.py
"""
Expand All @@ -158,7 +160,7 @@ def save_places_from_generator(data: Iterator[PlaceTuple], count: int) -> str:
total=count,
unit="places",
unit_scale=True,
desc="saving places",
desc=f"saving places from {source_info}",
):

# retrieve PlaceType from the database
Expand Down

0 comments on commit dd289b9

Please sign in to comment.