From 12e966cdd996fd47c0583850b2974cda5c658789 Mon Sep 17 00:00:00 2001 From: KK Date: Wed, 15 May 2024 10:27:14 +0200 Subject: [PATCH] style: fix issues identified by Ruff Fix formatting and linting issues pointed out by Ruff. Closes: #103 --- apis_ontology/api/serializers.py | 1 + .../commands/delete_source_objects.py | 4 +-- apis_ontology/models.py | 35 +++++++++++-------- apis_ontology/scripts/access_sharepoint.py | 7 ++-- apis_ontology/scripts/import_helpers.py | 7 ++-- .../import_nonbibl_entities_from_excel.py | 1 - apis_ontology/scripts/import_vorlass_data.py | 4 +-- .../scripts/import_zotero_collections.py | 6 ++-- apis_ontology/scripts/utils.py | 1 + apis_ontology/settings.py | 13 ++++--- apis_ontology/urls.py | 1 + manage.py | 1 + 12 files changed, 46 insertions(+), 35 deletions(-) diff --git a/apis_ontology/api/serializers.py b/apis_ontology/api/serializers.py index 96cb674..6ec2ea0 100644 --- a/apis_ontology/api/serializers.py +++ b/apis_ontology/api/serializers.py @@ -3,6 +3,7 @@ I.e. project-specific endpoints (not APIS built-in API). """ + from rest_framework import serializers from apis_ontology.models import Expression, Work, WorkType diff --git a/apis_ontology/management/commands/delete_source_objects.py b/apis_ontology/management/commands/delete_source_objects.py index 2c28a80..2c8c225 100644 --- a/apis_ontology/management/commands/delete_source_objects.py +++ b/apis_ontology/management/commands/delete_source_objects.py @@ -1,4 +1,3 @@ -import itertools from django.core.management.base import BaseCommand from apis_ontology.models import DataSource from apis_core.utils.caching import ( @@ -70,7 +69,6 @@ def handle(self, *args, **options): entities = [] entities_failed = [] - all_sources = DataSource.objects.all() all_entities = get_all_entity_class_names() entity_names = [m for m in all_entities] @@ -137,7 +135,7 @@ def handle(self, *args, **options): # else: try: ent_obj = ent_class.objects.filter(data_source__in=source_obj) - except: + except Exception: print(f"No objects left for source {source_obj.name}.") exit(0) diff --git a/apis_ontology/models.py b/apis_ontology/models.py index 1f8da9f..7a6fc5d 100644 --- a/apis_ontology/models.py +++ b/apis_ontology/models.py @@ -306,8 +306,9 @@ class TemporalFrequency(models.TextChoices): class FigureSpeech(models.TextChoices): DIRECT_QUOTE = "direct_quotation", _("direkt zitierend") - DIRECT_FIGURE_SPEECH = "direct_figure_speech", _( - "autonome, direkte Figurenrede" + DIRECT_FIGURE_SPEECH = ( + "direct_figure_speech", + _("autonome, direkte Figurenrede"), ) DIRECT_SPEECH = "direct_speech", _("direkte Rede") INDIRECT_SPEECH = "indirect_speech", _("indirekte Rede") @@ -324,11 +325,13 @@ class RepresentationOfThought(models.TextChoices): class Focalization(models.TextChoices): ZERO_FOCALIZATION = "zero", _("Nullfokalisierung") - INTERNAL_FOCALIZATION_VARIABLE = "internal_variable", _( - "interne Fokalisierung – variabel" + INTERNAL_FOCALIZATION_VARIABLE = ( + "internal_variable", + _("interne Fokalisierung – variabel"), ) - INTERNAL_FOCALIZATION_FIXED = "internal_fixed", _( - "interne Fokalisierung – fixiert" + INTERNAL_FOCALIZATION_FIXED = ( + "internal_fixed", + _("interne Fokalisierung – fixiert"), ) EXTERNAL_FOCALIZATION = "external", _("externe Fokalisierung") @@ -351,17 +354,21 @@ class NarrativeLevel(models.TextChoices): class NarrativeVoice(models.TextChoices): HETERODIEGETIC = "heterodiegetic", _("heterodiegetisches Erzählen") - HOMODIEGETIC_UNINVOLVED_EYEWITNESS = "homodiegetic_uninvolved", _( - "homodiegetisches Erzählen – unbeteiligter Beobachter:in" + HOMODIEGETIC_UNINVOLVED_EYEWITNESS = ( + "homodiegetic_uninvolved", + _("homodiegetisches Erzählen – unbeteiligter Beobachter:in"), ) # a.k.a. alterodiegetic - HOMODIEGETIC_WITNESS_PARTICIPANT = "homodiegetic_participant", _( - "homodiegetisches Erzählen – beteiligter Beobachter:in" + HOMODIEGETIC_WITNESS_PARTICIPANT = ( + "homodiegetic_participant", + _("homodiegetisches Erzählen – beteiligter Beobachter:in"), ) # a.k.a. alterodiegetic - HOMODIEGETIC_MINOR_CHARACTER = "homodiegetic_character", _( - "homodiegetisches Erzählen – Nebenfigur" + HOMODIEGETIC_MINOR_CHARACTER = ( + "homodiegetic_character", + _("homodiegetisches Erzählen – Nebenfigur"), ) # a.k.a. alterodiegetic - HOMODIEGETIC_PROTAGONIST = "homodiegetic_protagonist", _( - "homodiegetisches Erzählen – Hauptfigur" + HOMODIEGETIC_PROTAGONIST = ( + "homodiegetic_protagonist", + _("homodiegetisches Erzählen – Hauptfigur"), ) # a.k.a. autodiegetic siglum = models.CharField( diff --git a/apis_ontology/scripts/access_sharepoint.py b/apis_ontology/scripts/access_sharepoint.py index 27d9ba7..00ae6e9 100644 --- a/apis_ontology/scripts/access_sharepoint.py +++ b/apis_ontology/scripts/access_sharepoint.py @@ -6,7 +6,6 @@ import os import sharepy -import pandas as pd import getpass from pathlib import Path from urllib.parse import quote @@ -59,7 +58,7 @@ def sharepoint_connect(): try: s = sharepy.connect(sp_site, username=sp_user, password=sp_pass) s.save() - except Exception as e: + except Exception: print("Cannot connect to SharePoint!") exit(1) @@ -205,9 +204,9 @@ def fetch_file_data(s, sp_fname, sp_file_url): :return: the requested file """ sp_group = os.environ.get("SP_GROUP") - sp_dir = os.environ.get("SP_DIRECTORY") + # sp_dir = os.environ.get("SP_DIRECTORY") - source_file_by_name = f"https://{s.site}/sites/{sp_group}/_api/web/GetFolderByServerRelativeUrl('{sp_dir}')/Files('{sp_fname}')/$value" + # source_file_by_name = f"https://{s.site}/sites/{sp_group}/_api/web/GetFolderByServerRelativeUrl('{sp_dir}')/Files('{sp_fname}')/$value" source_file_by_url = f"https://{s.site}/sites/{sp_group}/_api/web/GetFileByServerRelativeUrl('{sp_file_url}')/$value" return source_file_by_url diff --git a/apis_ontology/scripts/import_helpers.py b/apis_ontology/scripts/import_helpers.py index 7fb0c89..1f2ef06 100644 --- a/apis_ontology/scripts/import_helpers.py +++ b/apis_ontology/scripts/import_helpers.py @@ -16,7 +16,6 @@ WorkType, Topic, ) -from .utils import create_import_date_string, convert_year_only_date def create_triple(entity_subj, entity_obj, prop): @@ -122,7 +121,7 @@ def get_work(siglum: str): work = Work.objects.get( siglum=siglum, ) - except Exception as e: + except Exception: pass return work @@ -138,7 +137,7 @@ def get_type(title: str): work_type = WorkType.objects.get( name=title, ) - except Exception as e: + except Exception: pass return work_type @@ -217,7 +216,7 @@ def create_person(person_data: dict, source: DataSource): full_name = first_name else: # TODO log as error instead of exiting program - f"Missing name to look up or create Person, exiting." + "Missing name to look up or create Person, exiting." exit(1) else: # rudimentary way of determining first name and last name from diff --git a/apis_ontology/scripts/import_nonbibl_entities_from_excel.py b/apis_ontology/scripts/import_nonbibl_entities_from_excel.py index 3967266..9e47023 100644 --- a/apis_ontology/scripts/import_nonbibl_entities_from_excel.py +++ b/apis_ontology/scripts/import_nonbibl_entities_from_excel.py @@ -1,5 +1,4 @@ import os -import numpy as np import pandas as pd import logging from apis_ontology.scripts.access_sharepoint import import_and_parse_data diff --git a/apis_ontology/scripts/import_vorlass_data.py b/apis_ontology/scripts/import_vorlass_data.py index 430af29..a8b3488 100644 --- a/apis_ontology/scripts/import_vorlass_data.py +++ b/apis_ontology/scripts/import_vorlass_data.py @@ -13,7 +13,7 @@ WorkType, StatusMixin, ) -from .additional_infos import WORK_TYPES, WORKTYPE_MAPPINGS +from .additional_infos import WORK_TYPES from .import_helpers import create_triple, create_source from apis_ontology.scripts.access_sharepoint import import_and_parse_data @@ -63,7 +63,7 @@ def parse_vorlass_xml(title_siglum_dict, vorlass_excel_source): ] with open( - f"./vorlass_data_frischmuth/04_derived_custom/Frischmuth_Vorlass_FNI-FRISCHMUTH_import-data.xml", + "./vorlass_data_frischmuth/04_derived_custom/Frischmuth_Vorlass_FNI-FRISCHMUTH_import-data.xml", "r", encoding="utf-8", ) as file_obj: diff --git a/apis_ontology/scripts/import_zotero_collections.py b/apis_ontology/scripts/import_zotero_collections.py index db86702..9072401 100644 --- a/apis_ontology/scripts/import_zotero_collections.py +++ b/apis_ontology/scripts/import_zotero_collections.py @@ -132,13 +132,13 @@ def zotero_login(): try: zot.key_info() - except zotero_errors.UserNotAuthorised as e: + except zotero_errors.UserNotAuthorised: print( f"Zotero login not possible: " f"{zot.request.text} ({zot.request.status_code})" ) exit(1) - except zotero_errors.HTTPError as e: + except zotero_errors.HTTPError: print( f"Zotero login not possible: " f"{zot.request.text} ({zot.request.status_code})" @@ -585,7 +585,7 @@ def fetch(sub_collections=sub_collections): try: key_match = zot.collection(k) active.append(key_match) - except Exception as e: + except Exception: print(f"Collection with key {k} not found.") if not active: diff --git a/apis_ontology/scripts/utils.py b/apis_ontology/scripts/utils.py index f03a6a3..6806e8b 100644 --- a/apis_ontology/scripts/utils.py +++ b/apis_ontology/scripts/utils.py @@ -1,6 +1,7 @@ """ Helper functions useful for various data import scripts. """ + import datetime from ..settings import APIS_BASE_URI diff --git a/apis_ontology/settings.py b/apis_ontology/settings.py index 044e85f..8c92539 100644 --- a/apis_ontology/settings.py +++ b/apis_ontology/settings.py @@ -15,7 +15,12 @@ import os from pathlib import Path -from apis_acdhch_default_settings.settings import * +from apis_acdhch_default_settings.settings import ( + INSTALLED_APPS, + MIDDLEWARE, + REST_FRAMEWORK, + SPECTACULAR_SETTINGS, +) # General Django settings @@ -144,9 +149,9 @@ SPECTACULAR_SETTINGS["COMPONENT_SPLIT_REQUEST"] = True SPECTACULAR_SETTINGS["COMPONENT_NO_READ_ONLY_REQUIRED"] = True -SPECTACULAR_SETTINGS[ - "DEFAULT_GENERATOR_CLASS" -] = "apis_core.generic.generators.CustomSchemaGenerator" +SPECTACULAR_SETTINGS["DEFAULT_GENERATOR_CLASS"] = ( + "apis_core.generic.generators.CustomSchemaGenerator" +) # Database diff --git a/apis_ontology/urls.py b/apis_ontology/urls.py index 86dfb06..9420934 100644 --- a/apis_ontology/urls.py +++ b/apis_ontology/urls.py @@ -14,6 +14,7 @@ 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ + from apis_acdhch_default_settings.urls import urlpatterns from django.urls import include, path from rest_framework import routers diff --git a/manage.py b/manage.py index aa689fd..c6c8667 100755 --- a/manage.py +++ b/manage.py @@ -1,5 +1,6 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" + import os import sys