From 49d160bb297bb20027ca1a0fa5d99cde5deedc0e Mon Sep 17 00:00:00 2001 From: Dennis Priskorn Date: Thu, 13 Jul 2023 19:55:22 +0200 Subject: [PATCH] chore: Fix pre-commit warnings and add ruff to pre-commit --- .pre-commit-config.yaml | 11 +++++++++ config.sample.py | 6 ++++- poetry.lock | 28 +++++++++++++++++++++- pyproject.toml | 1 + src/models/enrich_hiking_trails.py | 22 ++++++++--------- src/models/trail_item.py | 38 ++++++++++++++++++++++-------- 6 files changed, 82 insertions(+), 24 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 78d0172..6deebf5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/config.sample.py b/config.sample.py index c54b5ec..6192dd3 100644 --- a/config.sample.py +++ b/config.sample.py @@ -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" diff --git a/poetry.lock b/poetry.lock index 806816e..70da952 100644 --- a/poetry.lock +++ b/poetry.lock @@ -983,6 +983,32 @@ pygments = ">=2.13.0,<3.0.0" [package.extras] jupyter = ["ipywidgets (>=7.5.1,<9)"] +[[package]] +name = "ruff" +version = "0.0.278" +description = "An extremely fast Python linter, written in Rust." +optional = false +python-versions = ">=3.7" +files = [ + {file = "ruff-0.0.278-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:1a90ebd8f2a554db1ee8d12b2f3aa575acbd310a02cd1a9295b3511a4874cf98"}, + {file = "ruff-0.0.278-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:38ca1c0c8c1221fe64c0a66784c91501d09a8ed02a4dbfdc117c0ce32a81eefc"}, + {file = "ruff-0.0.278-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2c62a0bde4d20d087cabce2fa8b012d74c2e985da86d00fb3359880469b90e31"}, + {file = "ruff-0.0.278-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:7545bb037823cd63dca19280f75a523a68bd3e78e003de74609320d6822b5a52"}, + {file = "ruff-0.0.278-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:8cb380d2d6fdb60656a0b5fa78305535db513fc72ce11f4532cc1641204ef380"}, + {file = "ruff-0.0.278-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:d11149c7b186f224f2055e437a030cd83b164a43cc0211314c33ad1553ed9c4c"}, + {file = "ruff-0.0.278-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:666e739fb2685277b879d493848afe6933e3be30d40f41fe0e571ad479d57d77"}, + {file = "ruff-0.0.278-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:ec8b0469b54315803aaf1fbf9a37162a3849424cab6182496f972ad56e0ea702"}, + {file = "ruff-0.0.278-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c25b96602695a147d62a572865b753ef56aff1524abab13b9436724df30f9bd7"}, + {file = "ruff-0.0.278-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:a48621f5f372d5019662db5b3dbfc5f1450f927683d75f1153fe0ebf20eb9698"}, + {file = "ruff-0.0.278-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:1078125123a3c68e92463afacedb7e41b15ccafc09e510c6c755a23087afc8de"}, + {file = "ruff-0.0.278-py3-none-musllinux_1_2_i686.whl", hash = "sha256:3ce0d620e257b4cad16e2f0c103b2f43a07981668a3763380542e8a131d11537"}, + {file = "ruff-0.0.278-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:1cae4c07d334eb588f171f1363fa89a8911047eb93184276be11a24dbbc996c7"}, + {file = "ruff-0.0.278-py3-none-win32.whl", hash = "sha256:70d39f5599d8449082ab8ce542fa98e16413145eb411dd1dc16575b44565d52d"}, + {file = "ruff-0.0.278-py3-none-win_amd64.whl", hash = "sha256:e131595ab7f4ce61a1650463bd2fe304b49e7d0deb0dfa664b92817c97cdba5f"}, + {file = "ruff-0.0.278-py3-none-win_arm64.whl", hash = "sha256:737a0cfb6c36aaa92d97a46957dfd5e55329299074ad06ed12663b98e0c6fc82"}, + {file = "ruff-0.0.278.tar.gz", hash = "sha256:1a9f1d925204cfba81b18368b7ac943befcfccc3a41e170c91353b674c6b7a66"}, +] + [[package]] name = "setuptools" version = "68.0.0" @@ -1254,4 +1280,4 @@ notebooks = ["jupyter"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<3.13" -content-hash = "050bc650d5cffcb870f6a6cd52a835cfa1a486fca7086433aa441486cded536b" +content-hash = "37a36ec6efdd398ec6f817d292d3f49988a4e02873b01164891cc077aa59c313" diff --git a/pyproject.toml b/pyproject.toml index f1bc3e1..b0b628a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"] diff --git a/src/models/enrich_hiking_trails.py b/src/models/enrich_hiking_trails.py index 32262ca..91b87be 100644 --- a/src/models/enrich_hiking_trails.py +++ b/src/models/enrich_hiking_trails.py @@ -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 @@ -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""" @@ -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}". }} }} """ ) @@ -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 @@ -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 @@ -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") diff --git a/src/models/trail_item.py b/src/models/trail_item.py index edc1112..8fe5dcf 100644 --- a/src/models/trail_item.py +++ b/src/models/trail_item.py @@ -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( @@ -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") @@ -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: @@ -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__() @@ -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") @@ -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 @@ -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( @@ -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 @@ -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") \ No newline at end of file + logger.debug("No OSM_RELATION_ID found on this item to clean up")