Skip to content

Commit

Permalink
Merge pull request #26 from dpriskorn/avoid_empty_match_list
Browse files Browse the repository at this point in the history
Avoid no matches in list
  • Loading branch information
dpriskorn authored Jul 15, 2023
2 parents 7ab5a55 + fd01acf commit 9856393
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/models/trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class TrailItem(ProjectBaseModel):
chosen_osm_id: int = 0
last_update: Optional[datetime]
summary: str = ""
testing: bool = False

class Config:
arbitrary_types_allowed = True
Expand All @@ -74,7 +75,7 @@ def has_osm_way_property(self) -> bool:
@property
def open_in_josm_urls(self):
if self.osm_ids:
string_list = [str(id_) for id_ in self.osm_ids]
string_list = [f"r{str(id_)}" for id_ in self.osm_ids]
return (
f"http://localhost:8111/load_object?"
f"new_layer=true&objects={','.join(string_list)}"
Expand Down Expand Up @@ -193,7 +194,8 @@ def __lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__(
f"Skipping {self.item.get_entity_url()} because self.label "
f"was empty in the chosen language"
)
console.input("Press enter to continue")
if not self.testing:
console.input("Press enter to continue")
return
if not isinstance(self.label, str):
raise TypeError("self.label was not a str")
Expand All @@ -202,7 +204,19 @@ def __lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__(
self.__remove_waymaked_result_duplicates__()
self.__get_details_from_waymarked_trails__()
self.__prepare_choices__()
self.questionary_return = self.__ask_question__()
if len(self.choices) > 2:
self.questionary_return = self.__ask_question__()
else:
if not self.item:
raise NoItemError()
console.print(
f"No choices from Waymarked Trials "
f"API = no match for "
f"{self.item.get_entity_url()}"
)
return_ = QuestionaryReturn()
return_.no_match = True
self.questionary_return = return_

def __prepare_choices__(self):
self.__convert_waymarked_results_to_choices__()
Expand Down Expand Up @@ -308,7 +322,11 @@ def enrich_wikidata(self):
console.input("Press enter to upload or ctrl+c to quit")
if self.summary:
self.item.write(summary=self.summary)
console.print(f"Upload done, see {self.item.get_entity_url()}")
console.print(
f"Upload done, see {self.item.get_entity_url()} "
f"and https://hiking.waymarkedtrails.org/"
f"#route?id={self.questionary_return.osm_id}"
)
else:
raise SummaryError()
else:
Expand Down Expand Up @@ -367,7 +385,7 @@ def __parse_response_from_osm_wikidata_link__(self):
self.__populate_osm_ids__()
# Act on it
if len(self.osm_ids) > 1:
self.__hanndle_multiple_matches__()
self.__handle_multiple_matches__()
elif len(self.osm_ids) == 1:
self.__handle_single_match__()
else:
Expand Down Expand Up @@ -422,14 +440,16 @@ def __populate_osm_ids__(self):
# the opening in JOSM and logic here
self.osm_ids.append(item.get("id"))

def __hanndle_multiple_matches__(self):
def __handle_multiple_matches__(self):
# we got multiple matches so we ask the user to fix the situation in JOSM
console.print(
f"We got {len(self.osm_ids)} matches from {osm_wikidata_link}. "
f"Please download the relations in JOSM and ensure 1-1 link. "
f"Click here to open in JOSM with remote control "
f"{self.open_in_josm_urls}"
)
if not self.testing:
console.input("Press enter to continue")
self.osm_wikidata_link_return = OsmWikidataLinkReturn(multiple_matches=True)

def __handle_single_match__(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_lookup_in_osm_wikidata_link_api_single_match_relation(self):
assert len(trail_item.osm_wikidata_link_results) == 1

def test_lookup_in_osm_wikidata_link_api_multiple_relations(self):
trail_item = TrailItem(wbi=WikibaseIntegrator(), qid="Q151653")
trail_item = TrailItem(wbi=WikibaseIntegrator(), qid="Q151653", testing=True)
trail_item.lookup_using_osm_wikidata_link()
console.print(trail_item.osm_wikidata_link_return.dict())
assert trail_item.osm_wikidata_link_return.multiple_matches is True
Expand Down

0 comments on commit 9856393

Please sign in to comment.