Browse Source

linting with ruff

master
Holger Frey 1 week ago
parent
commit
46f4c26480
  1. 55
      pyproject.toml
  2. 3
      work_helpers/_natural_sort.py
  3. 14
      work_helpers/excel2changelog.py
  4. 9
      work_helpers/excel2pylist.py
  5. 1
      work_helpers/nice_path.py
  6. 1
      work_helpers/password.py
  7. 2
      work_helpers/random_int.py
  8. 16
      work_helpers/sensospot_rename.py
  9. 12
      work_helpers/sg_mbp_build.py

55
pyproject.toml

@ -14,12 +14,18 @@ license = "Beerware"
requires = [ requires = [
"pyperclip >=1.8.0", "pyperclip >=1.8.0",
"click >= 7.1.2", "click >= 7.1.2",
"black", "pandas",
"openpyxl",
"ruff",
"py-gitea", "py-gitea",
"PyPDFForm",
] ]
[tool.flit.scripts] [tool.flit.scripts]
form_inspect = "work_helpers.fill_forms:inspect"
form_prepare_payments = "work_helpers.fill_forms:prepare_payments"
form_fill_payments = "work_helpers.fill_forms:payments"
nice_path = "work_helpers.nice_path:make_nice_path" nice_path = "work_helpers.nice_path:make_nice_path"
random_password = "work_helpers.password:get_random_password" random_password = "work_helpers.password:get_random_password"
random_ints = "work_helpers.random_int:generate_random_number_list" random_ints = "work_helpers.random_int:generate_random_number_list"
@ -30,3 +36,50 @@ sg_mbp_issue_ref = "work_helpers.sg_mbp_issue:sg_mbp_issue_ref"
xls2changelog = "work_helpers.excel2changelog:cli" xls2changelog = "work_helpers.excel2changelog:cli"
xls2markdown = "work_helpers.excel2markdown:cli" xls2markdown = "work_helpers.excel2markdown:cli"
xls2pylist = "work_helpers.excel2pylist:cli" xls2pylist = "work_helpers.excel2pylist:cli"
[tool.ruff]
# see https://docs.astral.sh/ruff/configuration/
line-length = 80
indent-width = 4
fix = true
[tool.ruff.lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
# McCabe complexity (`C901`) by default.
fixable = ["ALL"]
select = ["ALL"]
ignore = [
# ignored for now, should be activated in the future
# docstrings
"D",
# don't complain about not annotating self or cls
"ANN101",
"ANN102",
# ignored, "ruff format" will handle this
"COM812",
"ISC001",
# ignored, due to Windows / WSL2 setup
# flake8-executable
"EXE",
]
[tool.ruff.lint.pydocstyle]
convention = "pep257" # Accepts: "google", "numpy", or "pep257".
[tool.ruff.lint.per-file-ignores]
# see https://github.com/charliermarsh/ruff
"tests/*" = ["FBT003", "INP001", "PLR2004", "S101", "SLF001", "ANN"]
"noxfile.py" = ["ANN"]
[tool.ruff.format]
indent-style = "space"

3
work_helpers/_natural_sort.py

@ -21,7 +21,8 @@ def _nartural_sort_convert(text):
def _nartural_sort_alphanum_key(text): def _nartural_sort_alphanum_key(text):
return tuple( return tuple(
_nartural_sort_convert(part) for part in _NATURAL_SORT_REGEX_DIGITS.split(text) _nartural_sort_convert(part)
for part in _NATURAL_SORT_REGEX_DIGITS.split(text)
) )

14
work_helpers/excel2changelog.py

@ -13,13 +13,13 @@ sheet = "Input_Data_*"
col_start = "A" col_start = "A"
row_start = 8 row_start = 8
def xls_formula_spacing(formula): def xls_formula_spacing(formula):
for c in "();=": for c in "();=":
formula = formula.replace(c, f"{c} ") formula = formula.replace(c, f"{c} ")
return formula return formula
def text_to_changelog( def text_to_changelog(
raw_text, sheet, start_column, start_row, compact=False, keep=False raw_text, sheet, start_column, start_row, compact=False, keep=False
): ):
@ -55,9 +55,13 @@ def text_to_changelog(
prev = current prev = current
def clipboard_to_changelog(sheet, start_column, start_row, compact=False, keep=False): def clipboard_to_changelog(
sheet, start_column, start_row, compact=False, keep=False
):
xls = pyperclip.paste() xls = pyperclip.paste()
result = list(text_to_changelog(xls, sheet, start_column, start_row, compact, keep)) result = list(
text_to_changelog(xls, sheet, start_column, start_row, compact, keep)
)
line_count = len(result) line_count = len(result)
if line_count == 1: if line_count == 1:
print(f"Copied one line to the clipboard") print(f"Copied one line to the clipboard")
@ -74,7 +78,9 @@ def clipboard_to_changelog(sheet, start_column, start_row, compact=False, keep=F
@click.command() @click.command()
@click.option("-s", "--sheet", prompt=True, required=True, default="Input_Data_*") @click.option(
"-s", "--sheet", prompt=True, required=True, default="Input_Data_*"
)
@click.option("-w", "--well", prompt=True, required=True, default="A1") @click.option("-w", "--well", prompt=True, required=True, default="A1")
@click.option("-c", "--compact", is_flag=True) @click.option("-c", "--compact", is_flag=True)
@click.option("-k", "--keep", is_flag=True) @click.option("-k", "--keep", is_flag=True)

9
work_helpers/excel2pylist.py

@ -1,12 +1,15 @@
import click import click
import pyperclip import pyperclip
def _strip_parts(iterable): def _strip_parts(iterable):
return [item.strip() for item in iterable] return [item.strip() for item in iterable]
def _replace_empty_strings(iterable, replacement="None"): def _replace_empty_strings(iterable, replacement="None"):
return [i or replacement for i in iterable] return [i or replacement for i in iterable]
def prepare(text): def prepare(text):
lines = text.splitlines() lines = text.splitlines()
@ -31,13 +34,17 @@ def pad_field(index, t):
else: else:
return value.ljust(length) return value.ljust(length)
def pad_fields(iterable, lengths): def pad_fields(iterable, lengths):
return [pad_field(i, t) for i, t in enumerate(zip(iterable, lengths))] return [pad_field(i, t) for i, t in enumerate(zip(iterable, lengths))]
def build_list(table, lengths): def build_list(table, lengths):
padded = (pad_fields(l, lengths) for l in table) padded = (pad_fields(l, lengths) for l in table)
padded_lines = (", ".join(l) for l in padded) padded_lines = (", ".join(l) for l in padded)
lines_as_list = (f" [{l}], # noqa: E201, E202, E203, E501," for l in padded_lines) lines_as_list = (
f" [{l}], # noqa: E201, E202, E203, E501," for l in padded_lines
)
list_content = "\n".join(lines_as_list) list_content = "\n".join(lines_as_list)
return f"[\n{list_content}\n]\n" return f"[\n{list_content}\n]\n"

1
work_helpers/nice_path.py

@ -7,6 +7,7 @@ REPLACEMENTS = {
"G:": "Google Drive", "G:": "Google Drive",
} }
def replace(segment, replacements=REPLACEMENTS): def replace(segment, replacements=REPLACEMENTS):
return replacements.get(segment, segment) return replacements.get(segment, segment)

1
work_helpers/password.py

@ -23,4 +23,3 @@ def get_random_password(length=16):
pyperclip.copy(password) pyperclip.copy(password)
click.echo("Copied to clipboard:", err=True) click.echo("Copied to clipboard:", err=True)
click.echo(password) click.echo(password)

2
work_helpers/random_int.py

@ -2,6 +2,7 @@ import click
import random import random
import pyperclip import pyperclip
@click.command() @click.command()
@click.argument("length", default=100, type=int) @click.argument("length", default=100, type=int)
def generate_random_number_list(length=100): def generate_random_number_list(length=100):
@ -12,4 +13,3 @@ def generate_random_number_list(length=100):
pyperclip.copy(integer_list) pyperclip.copy(integer_list)
click.echo("Copied to clipboard:", err=True) click.echo("Copied to clipboard:", err=True)
click.echo(integer_list) click.echo(integer_list)

16
work_helpers/sensospot_rename.py

@ -23,7 +23,9 @@ def get_image_parts(image_path:pathlib.Path) -> tuple[str, str, str]:
return image_path.stem.rsplit("_", maxsplit=2) return image_path.stem.rsplit("_", maxsplit=2)
def get_unique_parts(images:list[pathlib.Path]) -> list[list[str], list[str], list[str]]: def get_unique_parts(
images: list[pathlib.Path],
) -> list[list[str], list[str], list[str]]:
parts = [get_image_parts(p) for p in images] parts = [get_image_parts(p) for p in images]
return [sorted(set(items)) for items in zip(*parts)] return [sorted(set(items)) for items in zip(*parts)]
@ -58,14 +60,18 @@ def read_rename_map(map_file:pathlib.Path) -> dict[str, str]:
return {k: v for k, v in _parse_rename_map(content)} return {k: v for k, v in _parse_rename_map(content)}
def prepare_rename(images:list[pathlib.Path], rename_map:dict[str, str], sep="_") -> list[tuple[pathlib.Path, pathlib.Path]]: def prepare_rename(
images: list[pathlib.Path], rename_map: dict[str, str], sep="_"
) -> list[tuple[pathlib.Path, pathlib.Path]]:
for path in images: for path in images:
renamed_parts = [rename_map[p] for p in get_image_parts(path)] renamed_parts = [rename_map[p] for p in get_image_parts(path)]
yield path, path.with_stem(sep.join(renamed_parts)) yield path, path.with_stem(sep.join(renamed_parts))
@click.command() @click.command()
@click.argument("directory", type=click.Path(exists=True, file_okay=False, dir_okay=True)) @click.argument(
"directory", type=click.Path(exists=True, file_okay=False, dir_okay=True)
)
def sensospot_rename(directory): def sensospot_rename(directory):
images = list_images(directory) images = list_images(directory)
if not images: if not images:
@ -85,7 +91,9 @@ def sensospot_rename(directory):
try: try:
prepared = list(prepare_rename(images, rename_map)) prepared = list(prepare_rename(images, rename_map))
except Exception: except Exception:
raise click.UsageError("Could not rename images. Please check the image directory and rename map file.") raise click.UsageError(
"Could not rename images. Please check the image directory and rename map file."
)
for src, dst in prepared: for src, dst in prepared:
click.echo(f"renaming: {src} -> {dst}") click.echo(f"renaming: {src} -> {dst}")

12
work_helpers/sg_mbp_build.py

@ -122,7 +122,9 @@ def _extract_changes_from_log(
def _get_group_from_folder(folder: Pathlike) -> str: def _get_group_from_folder(folder: Pathlike) -> str:
name = pathlib.Path(folder).name name = pathlib.Path(folder).name
middle = name.removeprefix(GROUP_FOLDER_PREFIX).removesuffix(GROUP_FOLDER_SUFFIX) middle = name.removeprefix(GROUP_FOLDER_PREFIX).removesuffix(
GROUP_FOLDER_SUFFIX
)
if middle in GROUPS: if middle in GROUPS:
return middle return middle
msg = f"Folder '{name}' is not an MBP group folder" msg = f"Folder '{name}' is not an MBP group folder"
@ -166,7 +168,9 @@ class Version:
## functions for `sg_mbp_build` ## functions for `sg_mbp_build`
def copy_workbooks(group: str, destination: Pathlike, build_version: str) -> None: def copy_workbooks(
group: str, destination: Pathlike, build_version: str
) -> None:
source = _get_workbook_folder(group) source = _get_workbook_folder(group)
all_xls_files = _files_in_folder(source, ".xlsx") all_xls_files = _files_in_folder(source, ".xlsx")
mbp_files = (f for f in all_xls_files if f.name.lower().startswith("mbp")) mbp_files = (f for f in all_xls_files if f.name.lower().startswith("mbp"))
@ -198,7 +202,9 @@ def copy_workbook_changelogs(
shutil.copyfile(log_file, new_path) shutil.copyfile(log_file, new_path)
def copy_changelog(cwd: Pathlike, destination: Pathlike, build_version: str) -> None: def copy_changelog(
cwd: Pathlike, destination: Pathlike, build_version: str
) -> None:
changelog = _get_changelog_path(cwd) changelog = _get_changelog_path(cwd)
new_path = pathlib.Path(destination) / f"CHANGELOG {build_version}.txt" new_path = pathlib.Path(destination) / f"CHANGELOG {build_version}.txt"
print(changelog.name, "->", new_path) print(changelog.name, "->", new_path)

Loading…
Cancel
Save