Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use Ruff for Python linting and formatting #203

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions .flake8

This file was deleted.

26 changes: 5 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,9 @@ repos:
rev: v4.6.0
hooks:
- id: end-of-file-fixer
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.5
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.12.0
hooks:
- id: reorder-python-imports
args: [--py38-plus]
- repo: https://github.com/psf/black
rev: "23.12.1"
hooks:
- id: black
args: [--safe, --quiet]
- repo: https://github.com/pycqa/flake8
rev: "7.0.0"
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear==24.2.6
- flake8-comprehensions==3.14.0
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
2 changes: 1 addition & 1 deletion amuser/am_api_ability.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This module contains the ``ArchivematicaAPIAbility`` class, which represents a
user's ability to use Archivematica's APIs to interact with Archivematica.
"""

import logging
import os
import time
Expand All @@ -11,7 +12,6 @@

from . import base


logger = logging.getLogger("amuser.api")


Expand Down
6 changes: 3 additions & 3 deletions amuser/am_browser_ability.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Archivematica. This class provides an interface to Selenium for opening browser
windows and interacting with Archivematica's GUIs.
"""

import logging
import time

Expand All @@ -24,7 +25,6 @@
from . import base
from . import constants as c


logger = logging.getLogger("amuser.browser")


Expand Down Expand Up @@ -183,8 +183,8 @@ def initiate_reingest(self, aip_uuid, reingest_type="metadata-only"):
}.get(reingest_type)
if not type_selector:
raise ArchivematicaBrowserAbilityError(
"Unable to initiate a reingest of type {} on AIP"
" {}".format(reingest_type, aip_uuid)
f"Unable to initiate a reingest of type {reingest_type} on AIP"
f" {aip_uuid}"
)
while True:
type_input_el = self.driver.find_element(By.CSS_SELECTOR, type_selector)
Expand Down
2 changes: 1 addition & 1 deletion amuser/am_browser_auth_ability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Archivematica Authentication Ability"""

import logging

from selenium.common.exceptions import TimeoutException
Expand All @@ -8,7 +9,6 @@

from . import selenium_ability


logger = logging.getLogger("amuser.authentication")


Expand Down
10 changes: 4 additions & 6 deletions amuser/am_browser_file_explorer_ability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Archivematica Browser File Explorer Ability"""

import logging
import time

Expand All @@ -13,7 +14,6 @@
from . import constants as c
from . import selenium_ability


logger = logging.getLogger("amuser.fileexplorer")


Expand Down Expand Up @@ -245,20 +245,18 @@ def get_xpath_matches_folder_text(folder_text):
"""
return (
"div[contains(@class, 'tree-label') and"
" descendant::span[starts-with(normalize-space(text()), '{0}') and"
f" descendant::span[starts-with(normalize-space(text()), '{folder_text}') and"
" starts-with(normalize-space(substring-after("
"normalize-space(text()),"
" '{0}')), '(')]]".format(folder_text)
f" '{folder_text}')), '(')]]"
)


def folder_label2icon_xpath(folder_label_xpath):
"""Given XPATH for TS folder label, return XPATH for its folder
icon.
"""
return "{}/preceding-sibling::i[@class='tree-branch-head']".format(
folder_label_xpath
)
return f"{folder_label_xpath}/preceding-sibling::i[@class='tree-branch-head']"


def folder_label2children_xpath(folder_label_xpath):
Expand Down
9 changes: 4 additions & 5 deletions amuser/am_browser_ingest_ability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Archivematica Ingest Tab Ability"""

import logging
import os
import tempfile
Expand Down Expand Up @@ -105,7 +106,7 @@ def get_mets(self, transfer_name, sip_uuid=None, parse_xml=True):
self.am_url, sip_uuid
)
self.navigate(aip_preview_url)
mets_path = "storeAIP/{0}-{1}/METS.{1}.xml".format(transfer_name, sip_uuid)
mets_path = f"storeAIP/{transfer_name}-{sip_uuid}/METS.{sip_uuid}.xml"
handles_before = self.driver.window_handles
self.navigate_to_aip_directory_and_click(mets_path)
self.wait_for_new_window(handles_before)
Expand All @@ -116,10 +117,8 @@ def get_mets(self, transfer_name, sip_uuid=None, parse_xml=True):
while self.driver.current_url.strip() == "about:blank":
if attempts > self.max_check_mets_loaded_attempts:
msg = (
"Exceeded maximum allowable attempts ({}) for checking"
" if the METS file has loaded.".format(
self.max_check_mets_loaded_attempts
)
f"Exceeded maximum allowable attempts ({self.max_check_mets_loaded_attempts}) for checking"
" if the METS file has loaded."
)
logger.warning(msg)
raise ArchivematicaBrowserMETSAbilityError(msg)
Expand Down
6 changes: 2 additions & 4 deletions amuser/am_browser_jobs_tasks_ability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Archivematica Browser Jobs & Tasks Ability"""

import logging
import sys
import time
Expand All @@ -10,7 +11,6 @@
from . import selenium_ability
from . import utils


logger = logging.getLogger("amuser.jobstasks")


Expand Down Expand Up @@ -197,9 +197,7 @@ def get_job_uuid(
# The job is taking a long time to complete. Half the
# amount of checking to avoid stack-overflow.
logger.warning(
"Recursion limit close to being reached: level: {} <= {}".format(
level, sys.getrecursionlimit()
)
f"Recursion limit close to being reached: level: {level} <= {sys.getrecursionlimit()}"
)
time.sleep(self.quick_wait)
else:
Expand Down
22 changes: 8 additions & 14 deletions amuser/am_browser_preservation_planning_ability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Archivematica Browser Preservation Planning Ability"""

import logging

from selenium.common.exceptions import NoSuchElementException
Expand All @@ -7,7 +8,6 @@

from . import selenium_ability


logger = logging.getLogger("amuser.preservationplanning")


Expand Down Expand Up @@ -157,8 +157,8 @@ def save_policy_check_command(self, policy_command, description):
option.click()
break
self.driver.find_element(By.ID, "id_description").send_keys(description)
js_script = 'document.getElementById("id_command").value =' " `{}`;".format(
policy_command
js_script = (
'document.getElementById("id_command").value =' f" `{policy_command}`;"
)
self.driver.execute_script(js_script)
self.driver.find_element(By.ID, "id_script_type").send_keys("Python")
Expand Down Expand Up @@ -213,9 +213,7 @@ def search_for_fpr_rule(self, purpose, format_, command_description):
description. Uses the FPR asynchronous search input.
"""
terse_format = format_.split(":")[2].strip()
search_term = '"{}" "{}" "{}"'.format(
purpose, terse_format, command_description
)
search_term = f'"{purpose}" "{terse_format}" "{command_description}"'
self.search_rules(search_term)

def ensure_fpr_rule_enabled(self, purpose, format_, command_description):
Expand All @@ -234,17 +232,13 @@ def ensure_fpr_rule_enabled(self, purpose, format_, command_description):
]
if not disabled_rules:
logger.info(
'Tried to enable FPR rule with purpose "{}" that runs command "{}"'
' against files with format "{}" but did not find it'.format(
purpose, command_description, format_
)
f'Tried to enable FPR rule with purpose "{purpose}" that runs command "{command_description}"'
f' against files with format "{format_}" but did not find it'
)
return
assert len(disabled_rules) == 1, (
'Expected to enable one FPR rule with purpose "{}" that runs command "{}"'
' against files with format "{}" but found {} disabled rules'.format(
purpose, command_description, format_, len(disabled_rules)
)
f'Expected to enable one FPR rule with purpose "{purpose}" that runs command "{command_description}"'
f' against files with format "{format_}" but found {len(disabled_rules)} disabled rules'
)
rule = disabled_rules[0]
rule.find_element(By.CSS_SELECTOR, "td:nth-child(6) a:nth-child(3)").click()
Expand Down
10 changes: 5 additions & 5 deletions amuser/am_browser_ss_ability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Archivematica Browser Storage Service Ability"""

import logging
import pprint
import time
Expand All @@ -11,7 +12,6 @@
from . import selenium_ability
from . import utils


logger = logging.getLogger("amuser.ss")


Expand Down Expand Up @@ -43,8 +43,8 @@ def approve_aip_delete_request(self, aip_uuid):
matching_rows.append(row_el)
if len(matching_rows) != 1:
raise ArchivematicaBrowserStorageServiceAbilityError(
"More than one delete request row {} matches AIP"
" {}".format(len(matching_rows), aip_uuid)
f"More than one delete request row {len(matching_rows)} matches AIP"
f" {aip_uuid}"
)
matching_rows[0].find_element(By.TAG_NAME, "textarea").send_keys("Cuz wanna")
matching_rows[0].find_element(By.CSS_SELECTOR, 'input[name="approve"]').click()
Expand Down Expand Up @@ -318,9 +318,9 @@ def add_replicator_to_default_aip_stor_loc(self, replicator_location_uuid):
break
if not found_replicator:
raise ArchivematicaBrowserStorageServiceAbilityError(
"Unable to find replicator location {} as a possible replicator"
f"Unable to find replicator location {replicator_location_uuid} as a possible replicator"
" for the default AIP Storage"
" location".format(replicator_location_uuid)
" location"
)
self.driver.find_element(By.CSS_SELECTOR, "input[type=submit]").click()

Expand Down
6 changes: 2 additions & 4 deletions amuser/am_browser_transfer_ability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Archivematica Transfer Tab Ability"""

import logging
import os
import time
Expand All @@ -11,7 +12,6 @@
from . import constants as c
from . import selenium_ability


logger = logging.getLogger("amuser.transfer")


Expand Down Expand Up @@ -249,9 +249,7 @@ def approve_transfer(
re-click the micro-service <div> to make the hidden <select> visible
again.
"""
approve_transfer_option_selector = "option[value='{}']".format(
approve_option_uuid
)
approve_transfer_option_selector = f"option[value='{approve_option_uuid}']"
while True:
try:
approve_transfer_option = transfer_div_elem.find_element(
Expand Down
18 changes: 7 additions & 11 deletions amuser/am_browser_transfer_ingest_ability.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Archivematica Transfer & Ingest Tabs Ability"""

import logging
import sys
import time
Expand All @@ -15,7 +16,6 @@
from . import selenium_ability
from . import utils


logger = logging.getLogger("amuser.transferingest")


Expand Down Expand Up @@ -116,10 +116,8 @@ def assert_no_option(
assert f'Unable to select choice "{choice_text}"' == str(exc)
else:
raise AssertionError(
'We were able to select choice "{}" at decision point "{}" even'
" though we expected this not to be possible.".format(
choice_text, decision_point
)
f'We were able to select choice "{choice_text}" at decision point "{decision_point}" even'
" though we expected this not to be possible."
)

@selenium_ability.recurse_on_stale
Expand All @@ -142,9 +140,7 @@ def wait_for_microservice_visibility(
# The job is taking a long time to complete. Half the
# amount of checking to avoid stack-overflow.
logger.warning(
"Recursion limit close to being reached: level: {} <= {}".format(
level, sys.getrecursionlimit()
)
f"Recursion limit close to being reached: level: {level} <= {sys.getrecursionlimit()}"
)
time.sleep(self.micro_wait)
else:
Expand Down Expand Up @@ -186,9 +182,9 @@ def wait_for_transfer_micro_service_group(self, group_name, transfer_uuid):
while True:
if attempts > max_attempts:
msg = (
"Exceeded maxumim allowable attempts ({}) for checking"
" whether micro-service group {} of transfer {} is"
" visible.".format(max_attempts, group_name, transfer_uuid)
f"Exceeded maxumim allowable attempts ({max_attempts}) for checking"
f" whether micro-service group {group_name} of transfer {transfer_uuid} is"
" visible."
)
logger.warning(msg)
raise ArchivematicaBrowserTransferIngestAbilityError(msg)
Expand Down
Loading