descriptions added

This commit is contained in:
crunk 2024-06-27 10:47:41 +02:00
parent 5934122c6e
commit 3eefd6e5ca
4 changed files with 115 additions and 18 deletions

View File

@ -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(

View File

@ -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'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{name}"></a>'
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a>'
if image_description is None:
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{name}"></a>'
return f'<figure><a href="{name}"><img class="thumbnail" src="{thumbnail_filename}"></a><figcaption>{image_description}</figcaption></figure>'
if image_description is None:
return f'<a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a>'
return f'<figure><a href="{name}"><img class="thumbnail" src="{thumbnail_filename}" alt="{image_alttext}"></a><figcaption>{image_description}</figcaption></figure>'
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 = "<a href='{}'>{}</a>"
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"

View File

@ -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

View File

@ -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",
],
)