Skip to content

Commit

Permalink
feat: Ignore items which already have a osm way property. Add tests f…
Browse files Browse the repository at this point in the history
…or new method.
  • Loading branch information
dpriskorn committed Jul 14, 2023
1 parent 135fda6 commit 9afd31b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class DebugExit(BaseException):

class NoItemError(BaseException):
pass


class WBIError(BaseException):
pass
80 changes: 56 additions & 24 deletions src/models/trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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"""
Expand All @@ -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__()
Expand All @@ -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__():
Expand Down
10 changes: 10 additions & 0 deletions tests/test_trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9afd31b

Please sign in to comment.