diff --git a/src/exceptions.py b/src/exceptions.py index dd8ca3e..7034c0b 100644 --- a/src/exceptions.py +++ b/src/exceptions.py @@ -12,3 +12,7 @@ class DebugExit(BaseException): class NoItemError(BaseException): pass + + +class WBIError(BaseException): + pass diff --git a/src/models/enrich_hiking_trails.py b/src/models/enrich_hiking_trails.py index 609e3ae..99b07e2 100644 --- a/src/models/enrich_hiking_trails.py +++ b/src/models/enrich_hiking_trails.py @@ -34,13 +34,14 @@ def __get_hiking_trails_missing_osm_id__(self) -> None: self.__extract_item_ids__() def __get_sparql_result__(self): - """Get all trails in the specified country and + """Get all hiking trails and subtrails in the specified country and with labels in the specified language""" self.setup_wbi() + # Support all subclasses of Q2143825 hiking trail self.sparql_result = execute_sparql_query( f""" SELECT DISTINCT ?item ?itemLabel WHERE {{ - ?item wdt:P31 wd:Q2143825; + ?item wdt:P31/wdt:P279* wd:Q2143825; wdt:P17 wd:{config.country_qid}. minus{{?item wdt:P402 []}} # Fetch labels also diff --git a/src/models/trail_item.py b/src/models/trail_item.py index cda2d38..f769c3a 100644 --- a/src/models/trail_item.py +++ b/src/models/trail_item.py @@ -24,7 +24,7 @@ import config from src.console import console from src.enums import ItemEnum, OsmIdSource, Property, Status -from src.exceptions import NoItemError, SummaryError +from src.exceptions import NoItemError, SummaryError, WBIError from src.models.osm_wikidata_link_result import OsmWikidataLinkResult from src.models.osm_wikidata_link_return import OsmWikidataLinkReturn from src.models.project_base_model import ProjectBaseModel @@ -59,6 +59,18 @@ class TrailItem(ProjectBaseModel): class Config: arbitrary_types_allowed = True + @property + def has_osm_way_property(self) -> bool: + try: + if not self.item: + raise NoItemError() + if self.item.claims.get(property="P10689"): + return True + else: + raise WBIError("This should never happen") + except KeyError: + return False + @property def open_in_josm_urls(self): if self.osm_ids: @@ -175,7 +187,14 @@ def __lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__( self, ) -> None: if not self.label: - raise ValueError("self.label was empty") + if not self.item: + raise NoItemError() + print( + f"Skipping {self.item.get_entity_url()} because self.label " + f"was empty in the chosen language" + ) + console.input("Press enter to continue") + return if not isinstance(self.label, str): raise TypeError("self.label was not a str") logger.info(f"looking up: {self.label}") @@ -243,7 +262,13 @@ def fetch_and_lookup_from_waymarked_trails_and_present_choice_to_user(self): if not self.wbi: raise ValueError("self.wbi missing") self.__get_item_details__() - self.__lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__() + if not self.has_osm_way_property: + self.__lookup_label_on_waymarked_trails_and_ask_user_to_choose_a_match__() + else: + console.print( + f"Skipping item {self.item.get_entity_url()} " + f"which already has a OSM way property" + ) def enrich_wikidata(self): """We enrich Wikidata based on the choice of the user""" @@ -252,6 +277,7 @@ def enrich_wikidata(self): else: self.chosen_osm_id = self.osm_wikidata_link_results[0].id if self.item: + enrich = False if self.chosen_osm_id: self.__add_osm_id_to_item__() self.__remove_not_found_in_osm_claim__() @@ -260,29 +286,35 @@ def enrich_wikidata(self): "the [[Wikidata:Tools/hiking trail matcher" "|hiking trail matcher]]" ) + enrich = True else: - console.print("No match") - self.__add_or_replace_not_found_in_openstreetmap_claim__() - self.__remove_osm_relation_no_value_claim__() - self.summary = ( - "Added not found in OpenStreetMap via " - "the [[Wikidata:Tools/hiking trail" - " matcher|hiking trail matcher]]" - ) - if config.upload_to_wikidata: - if config.validate_before_upload: - print("Please validate that this json looks okay") - console.print(self.item.get_json()) - 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()}") + if self.questionary_return.no_match is True: + console.print("No match") + self.__add_or_replace_not_found_in_openstreetmap_claim__() + self.__remove_osm_relation_no_value_claim__() + self.summary = ( + "Added not found in OpenStreetMap via " + "the [[Wikidata:Tools/hiking trail" + " matcher|hiking trail matcher]]" + ) + enrich = True else: - raise SummaryError() - else: - console.print( - "Not uploading because config.upload_to_wikidata is False" - ) + logging.info("No enriching to be done") + if enrich is True: + if config.upload_to_wikidata: + if config.validate_before_upload: + print("Please validate that this json looks okay") + console.print(self.item.get_json()) + 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()}") + else: + raise SummaryError() + else: + console.print( + "Not uploading because config.upload_to_wikidata is False" + ) @staticmethod def __last_update_today_statement__(): @@ -496,7 +528,7 @@ def __remove_osm_relation_no_value_claim__(self): # Remove no-value claim try: if self.item.claims.get(Property.OSM_RELATION_ID.value): - print( + logger.info( "Removing OSM relation id = no-value claim because " "it was not a way to model the check that the community liked best" ) diff --git a/tests/test_trail_item.py b/tests/test_trail_item.py index eb69117..f0ec83f 100644 --- a/tests/test_trail_item.py +++ b/tests/test_trail_item.py @@ -98,3 +98,13 @@ def test_time_to_check_again_lets_check(self): trail_item = TrailItem(wbi=WikibaseIntegrator()) trail_item.last_update = datetime(day=24, month=9, year=2020, tzinfo=tzutc()) assert trail_item.time_to_check_again(testing=True) is True + + def test_has_osm_way_property_false(self): + trail_item = TrailItem(wbi=WikibaseIntegrator(), qid=self.last_update_test_item) + trail_item.__get_item_details__() + assert trail_item.has_osm_way_property is False + + def test_has_osm_way_property_true(self): + trail_item = TrailItem(wbi=WikibaseIntegrator(), qid="Q1692894") + trail_item.__get_item_details__() + assert trail_item.has_osm_way_property is True