From 3eefd6e5ca7048555d441df8c6fbf4f2e255acac Mon Sep 17 00:00:00 2001 From: crunk Date: Thu, 27 Jun 2024 10:47:41 +0200 Subject: [PATCH] descriptions added --- distribusi/cli.py | 14 +++++++--- distribusi/distribusi.py | 55 +++++++++++++++++++++++++++++++--------- pyproject.toml | 50 ++++++++++++++++++++++++++++++++++++ setup.py | 14 +++++++--- 4 files changed, 115 insertions(+), 18 deletions(-) diff --git a/distribusi/cli.py b/distribusi/cli.py index 4e03cf8..d282608 100644 --- a/distribusi/cli.py +++ b/distribusi/cli.py @@ -17,13 +17,21 @@ def build_argparser(): ) parser.add_argument( - "-d", "--directory", help="Select which directory to distribute", default="." + "-d", + "--directory", + help="Select which directory to distribute", + default=".", ) - parser.add_argument("-s", "--style", help="Select a CSS style sheet to include") + parser.add_argument( + "-s", "--style", help="Select a CSS style sheet to include" + ) parser.add_argument( - "-v", "--verbose", help="Print verbose debug output", action="store_true" + "-v", + "--verbose", + help="Print verbose debug output", + action="store_true", ) parser.add_argument( diff --git a/distribusi/distribusi.py b/distribusi/distribusi.py index 302099d..87e4a7d 100644 --- a/distribusi/distribusi.py +++ b/distribusi/distribusi.py @@ -1,4 +1,3 @@ -import base64 import os import magic @@ -12,16 +11,35 @@ MIME_TYPE = magic.Magic(mime=True) def add_alttext(full_path_image): try: image_filename_no_ext = os.path.splitext(full_path_image)[0] - alttext_filename = f'{image_filename_no_ext}_alttext.txt' - if os.path.isfile(alttext_filename): - print(f"{image_filename_no_ext} has {alttext_filename}") - with open(alttext_filename, 'r') as alttext_file: - return alttext_file.read() + alttext_filename = f"{image_filename_no_ext}_alttext.txt" + return _read_matching_text_file( + image_filename_no_ext, alttext_filename + ) except Exception as e: print(f"exception {e} raised while making alttext") return +def add_description(full_path_image): + try: + image_filename_no_ext = os.path.splitext(full_path_image)[0] + description_filename = f"{image_filename_no_ext}_dv_description.txt" + return _read_matching_text_file( + image_filename_no_ext, description_filename + ) + except Exception as e: + print(f"exception {e} raised while adding description") + return + + +def _read_matching_text_file(image_filename_no_ext, filename): + if not os.path.isfile(filename): + return + print(f"{image_filename_no_ext} has {filename}") + with open(filename, "r") as text_file: + return text_file.read() + + def thumbnail(full_path_image, name, args): if full_path_image.endswith("_thumbnail.jpg"): return @@ -94,7 +112,7 @@ def write_index(args, index, html, html_head, html_footer): index_file.write(html_footer) -def handle_text_files(name, full_path): +def handle_text_files(name, full_path, subtype): if name.endswith(".html") or subtype == "html": subtype = "html" # what types of text files to expand @@ -119,9 +137,15 @@ def handle_image_files(name, full_path, args): return if args.alttexts: image_alttext = add_alttext(full_path) + if args.alttexts: + image_description = add_description(full_path) if image_alttext is None: - return f'{name}' - return f'{image_alttext}' + if image_description is None: + return f'{name}' + return f'
{image_description}
' + if image_description is None: + return f'{image_alttext}' + return f'
{image_alttext}
{image_description}
' return FILE_TYPES[type_].format(name, image_alttext) @@ -150,7 +174,9 @@ def distribusify(args, directory): # noqa if args.exclude_directory: if args.verbose: - print("Excluding directory:", ", ".join(args.exclude_directory)) + print( + "Excluding directory:", ", ".join(args.exclude_directory) + ) dirs[:] = [d for d in dirs if d not in args.exclude_directory] if args.no_hidden: @@ -172,6 +198,9 @@ def distribusify(args, directory): # noqa if name.endswith("_alttext.txt"): continue + if name.endswith("_dv_description.txt"): + continue + full_path = os.path.join(root, name) mime = MIME_TYPE.from_file(full_path) type_, subtype = mime.split("/") @@ -182,7 +211,7 @@ def distribusify(args, directory): # noqa if type_ in FILE_TYPES: match type_: case "text": - subtype, tag = handle_text_files(name, full_path) + subtype, tag = handle_text_files(name, full_path, subtype) case "image": tag = handle_image_files(name, full_path, args) if tag is None: @@ -197,7 +226,9 @@ def distribusify(args, directory): # noqa # catch exceptions not yet defined in FILE_TYPES or SUB_TYPES tag = "{}" if args.verbose: - message = "not in list of file types, adding as plain href: \n" + message = ( + "not in list of file types, adding as plain href: \n" + ) print(type_, subtype, message, name) subtype = subtype + " unkown-file" diff --git a/pyproject.toml b/pyproject.toml index d872ceb..02c81b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,3 +7,53 @@ build-backend = "setuptools.build_meta" [tool.black] skip-string-normalization = true + +[tool.ruff] +line-length = 79 +target-version = "py311" +#include = '\.pyi?$' +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".ipynb_checkpoints", + ".mypy_cache", + ".nox", + ".pants.d", + ".pyenv", + ".pytest_cache", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + ".vscode", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "site-packages", + "venv", +] + +[tool.ruff.lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +select = ["E4", "E7", "E9", "F"] +ignore = [] +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +[tool.ruff.format] +quote-style = "double" +indent-style = "space" +docstring-code-format = true +line-ending = "auto" +skip-magic-trailing-comma = false diff --git a/setup.py b/setup.py index 5c282f1..8fdf7de 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,9 @@ from setuptools import find_packages, setup -dependencies = ["pillow >= 10.3.0", "python-magic >= 0.4.15, < 1.0", "exif >= 1.6.0"] +dependencies = [ + "pillow >= 10.3.0", + "python-magic >= 0.4.15, < 1.0", +] with open("README.md", "r") as handle: long_description = handle.read() @@ -23,6 +26,11 @@ setup( zip_safe=False, platforms="any", install_requires=dependencies, - entry_points={"console_scripts": ["distribusi = distribusi.cli:cli_entrypoint"]}, - classifiers=["Programming Language :: Python :: 3", "Environment :: Console"], + entry_points={ + "console_scripts": ["distribusi = distribusi.cli:cli_entrypoint"] + }, + classifiers=[ + "Programming Language :: Python :: 3", + "Environment :: Console", + ], )