Skip to content

Commit

Permalink
chore: Fix pre-commit warnings and add ruff to pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dpriskorn committed Jul 13, 2023
1 parent 6def551 commit 49d160b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ repos:
entry: black .
types: [python]

- id: ruff
name: ruff
language: system
entry: ruff
args:
# Tell ruff to fix sorting of imports
- "--fix"
- "--format=github"
- "--target-version=py37"
types: [python]

- id: codespell
name: codespell
language: system
Expand Down
6 changes: 5 additions & 1 deletion config.sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
user_name = ""
bot_password = "" # nosec
user_name_only = "input your user name here"
user_agent = f"hiking_trail_matcher, see https://github.com/dpriskorn/hiking_trail_matcher/ User:{user_name_only}"
user_agent = (
f"hiking_trail_matcher, "
f"see https://github.com/dpriskorn/"
f"hiking_trail_matcher/ User:{user_name_only}"
)

# This controls which hiking trails to fetch and work on
language_code = "en"
Expand Down
28 changes: 27 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pytest = "^7.2.1"
pyupgrade = "^3.3.1"
types-python-dateutil = "^2.8.19.13"
types-requests = "^2.31.0.1"
ruff = "^0.0.278"

[build-system]
requires = ["poetry-core"]
Expand Down
22 changes: 10 additions & 12 deletions src/models/enrich_hiking_trails.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import logging
from typing import Any, Dict, List, Optional

import requests # type: ignore
from pydantic import validate_arguments
from wikibaseintegrator import WikibaseIntegrator, wbi_config # type: ignore
from wikibaseintegrator.datatypes import ExternalID, Item, Time # type: ignore
from wikibaseintegrator.wbi_enums import ( # type: ignore
WikibaseDatePrecision,
WikibaseSnakType,
)
from wikibaseintegrator import WikibaseIntegrator # type: ignore
from wikibaseintegrator.wbi_helpers import execute_sparql_query # type: ignore
from wikibaseintegrator.wbi_login import Login # type: ignore

Expand Down Expand Up @@ -40,7 +34,8 @@ 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 with labels in the specified language"""
"""Get all trails in the specified country and
with labels in the specified language"""
self.setup_wbi()
self.sparql_result = execute_sparql_query(
f"""
Expand All @@ -49,7 +44,8 @@ def __get_sparql_result__(self):
wdt:P17 wd:{config.country_qid}.
minus{{?item wdt:P402 []}}
# Fetch labels also
SERVICE wikibase:label {{ bd:serviceParam wikibase:language "{config.language_code}". }}
SERVICE wikibase:label
{{ bd:serviceParam wikibase:language "{config.language_code}". }}
}}
"""
)
Expand All @@ -58,7 +54,8 @@ def __get_sparql_result__(self):
def __extract_wcdqs_json_entity_id__(
self, data: Dict, sparql_variable: str = "item"
) -> str:
"""We default to "item" as sparql value because it is customary in the Wikibase ecosystem"""
"""We default to "item" as sparql value because
it is customary in the Wikibase ecosystem"""
return str(data[sparql_variable]["value"].replace(self.rdf_entity_prefix, ""))

@validate_arguments
Expand Down Expand Up @@ -94,7 +91,7 @@ def __lookup_in_osm_wikidata_link__(trail_item: TrailItem) -> TrailItem:
trail_item.__ask_user_to_approve_match_from_osm_wikidata_link__()
else:
if trail_item.osm_wikidata_link_return.no_match:
console.print(f"Got no match from OSM Wikidata Link API")
console.print("Got no match from OSM Wikidata Link API")
# Return mutated object
return trail_item

Expand Down Expand Up @@ -147,7 +144,8 @@ def __iterate_items__(self):
self.__lookup_in_waymarked_trails__(trail_item=trail_item)
else:
logger.info(
f"Skipping item with recent last update statement, see {trail_item.item.get_entity_url()}"
f"Skipping item with recent last update statement, "
f"see {trail_item.item.get_entity_url()}"
)
count += 1
logger.debug("end of loop")
38 changes: 28 additions & 10 deletions src/models/trail_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def __parse_not_found_in_osm_last_update_statement__(self):
)
if len(last_update_list) > 1:
print(
"Found more than one last update qualifier. Only considering the last one"
"Found more than one last update qualifier. "
"Only considering the last one"
)
for entry in last_update_list:
date_string = pydash.get(
Expand All @@ -159,7 +160,8 @@ def __parse_not_found_in_osm_last_update_statement__(self):
self.last_update = date
except KeyError:
logger.info(
"No qualifier found for the not found in OSM-claim. Ignoring it"
"No qualifier found for the not "
"found in OSM-claim. Ignoring it"
)
else:
print("No not found in-property with a know value found")
Expand Down Expand Up @@ -217,7 +219,8 @@ def __lookup_in_the_waymarked_trails_database__(self, search_term: str) -> None:
if not search_term:
raise ValueError("search_term was empty")
url = (
f"https://hiking.waymarkedtrails.org/api/v1/list/search?query={search_term}"
f"https://hiking.waymarkedtrails.org/api/"
f"v1/list/search?query={search_term}"
)
result = requests.get(url=url, timeout=config.request_timeout)
if result.status_code == 200:
Expand All @@ -233,7 +236,8 @@ def __get_details_from_waymarked_trails__(self) -> None:
[result.get_details() for result in self.waymarked_results]

def fetch_and_lookup_from_waymarked_trails_and_present_choice_to_user(self):
"""We collect all the information and help the user choose the right match"""
"""We collect all the information and help
the user choose the right match"""
if not self.wbi:
raise ValueError("self.wbi missing")
self.__get_item_details__()
Expand All @@ -249,12 +253,20 @@ def enrich_wikidata(self):
if self.item:
if self.chosen_osm_id:
self.__add_osm_id_to_item__()
self.summary = "Added match to OpenStreetMap via the [[Wikidata:Tools/hiking trail matcher|hiking trail matcher]]"
self.summary = (
"Added match to OpenStreetMap via "
"the [[Wikidata:Tools/hiking trail matcher"
"|hiking trail matcher]]"
)
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]]"
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")
Expand Down Expand Up @@ -346,7 +358,10 @@ def __ask_user_to_approve_match_from_osm_wikidata_link__(self) -> None:
f"'{self.description}' in Wikidata?(Y/n)"
)
else:
question = f"Does the above match '{self.label}' (description missing) in Wikidata?(Y/n)"
question = (
f"Does the above match '{self.label}' "
f"(description missing) in Wikidata?(Y/n)"
)
answer = console.input(question)
if answer == "" or answer.lower() == "y":
# we got enter/yes
Expand Down Expand Up @@ -389,7 +404,9 @@ def __handle_single_match__(self):
self.osm_wikidata_link_return = OsmWikidataLinkReturn(single_match=True)

def __add_osm_id_to_item__(self):
console.print(f"Got match, adding OSM relation id = {self.chosen_osm_id} to WD")
console.print(
f"Got match, adding " f"OSM relation id = {self.chosen_osm_id} to WD"
)
if self.osm_id_source == OsmIdSource.QUESTIONNAIRE:
self.item.add_claims(
claims=ExternalID(
Expand Down Expand Up @@ -431,7 +448,8 @@ def time_to_check_again(self, testing: bool = False) -> bool:
return False
else:
logger.info(
"The item is missing a not found in osm claim with a last update qualifier statement"
"The item is missing a not found in osm claim "
"with a last update qualifier statement"
)
return True

Expand Down Expand Up @@ -478,4 +496,4 @@ def __remove_osm_relation_no_value_claim__(self):
)
self.item.claims.remove(Property.OSM_RELATION_ID.value)
except KeyError:
logger.debug("No OSM_RELATION_ID found on this item to clean up")
logger.debug("No OSM_RELATION_ID found on this item to clean up")

0 comments on commit 49d160b

Please sign in to comment.