refactor, at least label functions as internal

This commit is contained in:
crunk 2024-12-21 13:07:54 +01:00
parent cc25eb73c1
commit c6ed9d8416
3 changed files with 37 additions and 37 deletions

View File

@ -14,7 +14,7 @@ from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES
MIME_TYPE = magic.Magic(mime=True) MIME_TYPE = magic.Magic(mime=True)
def add_alttext(full_path_image): def _add_alttext(full_path_image):
try: try:
image_filename_no_ext = os.path.splitext(full_path_image)[0] image_filename_no_ext = os.path.splitext(full_path_image)[0]
alttext_filename = f"{image_filename_no_ext}_alttext.txt" alttext_filename = f"{image_filename_no_ext}_alttext.txt"
@ -26,7 +26,7 @@ def add_alttext(full_path_image):
return return
def add_description(full_path_image): def _add_description(full_path_image):
try: try:
image_filename_no_ext = os.path.splitext(full_path_image)[0] image_filename_no_ext = os.path.splitext(full_path_image)[0]
description_filename = f"{image_filename_no_ext}_dv_description.txt" description_filename = f"{image_filename_no_ext}_dv_description.txt"
@ -46,7 +46,7 @@ def _read_matching_text_file(image_filename_no_ext, filename):
return text_file.read() return text_file.read()
def make_thumbnail(full_path_image, name): def _make_thumbnail(full_path_image, name):
if full_path_image.endswith("_thumbnail.jpg"): if full_path_image.endswith("_thumbnail.jpg"):
return return
try: try:
@ -68,7 +68,7 @@ def make_thumbnail(full_path_image, name):
return return
def format_div(type_, subtype, tag, name): def _format_div(type_, subtype, tag, name):
id_name = name.split(".")[0].replace(" ", "_") id_name = name.split(".")[0].replace(" ", "_")
filename = f'<span class="filename">{name}</span>' filename = f'<span class="filename">{name}</span>'
@ -84,7 +84,7 @@ def format_div(type_, subtype, tag, name):
return html.format(id_name, subtype, tag) return html.format(id_name, subtype, tag)
def check_distribusi_index(index): def _check_distribusi_index(index):
""" """
check whether a index.html file is generated by distribusi check whether a index.html file is generated by distribusi
""" """
@ -94,7 +94,7 @@ def check_distribusi_index(index):
return False return False
def write_index_html(index, html, cssfile): def _write_index_html(index, html, cssfile):
with open(index, "w") as index_file: with open(index, "w") as index_file:
styled_html_head = html_head.format(cssfile=cssfile) styled_html_head = html_head.format(cssfile=cssfile)
index_file.write(styled_html_head) index_file.write(styled_html_head)
@ -103,7 +103,7 @@ def write_index_html(index, html, cssfile):
index_file.write(html_footer) index_file.write(html_footer)
def handle_text_files(name, full_path, subtype): def _handle_text_files(name, full_path, subtype):
if name.endswith(".html") or subtype == "html": if name.endswith(".html") or subtype == "html":
subtype = "html" subtype = "html"
# what types of text files to expand # what types of text files to expand
@ -121,13 +121,13 @@ def handle_text_files(name, full_path, subtype):
return subtype, tag return subtype, tag
def handle_image_files(name, full_path): def _handle_image_files(name, full_path):
thumbnail_filename = make_thumbnail(full_path, name) thumbnail_filename = _make_thumbnail(full_path, name)
if thumbnail_filename is None: if thumbnail_filename is None:
return return
image_alttext = add_alttext(full_path) image_alttext = _add_alttext(full_path)
image_description = add_description(full_path) image_description = _add_description(full_path)
if not image_alttext and not image_description: if not image_alttext and not image_description:
return image_no_description.format( return image_no_description.format(
@ -153,7 +153,7 @@ def handle_image_files(name, full_path):
) )
def remove_index_html(root, files): def _remove_existing_index_html(root, files):
index = os.path.join(root, "index.html") index = os.path.join(root, "index.html")
if "index.html" in files: if "index.html" in files:
try: try:
@ -164,13 +164,13 @@ def remove_index_html(root, files):
return return
def remove_hidden_files_and_folders(dirs, files): def _remove_hidden_files_and_folders(dirs, files):
dirs = list(filter(lambda d: not d.startswith("."), dirs)) dirs = list(filter(lambda d: not d.startswith("."), dirs))
files = list(filter(lambda f: not f.startswith("."), files)) files = list(filter(lambda f: not f.startswith("."), files))
return dirs, files return dirs, files
def is_skipped_file(name, cssfile): def _is_skippable_file(name, cssfile):
if "index.html" in name: if "index.html" in name:
return True return True
@ -187,14 +187,14 @@ def is_skipped_file(name, cssfile):
return True return True
def distribusify(cssfile, directory): def distribusify(directory, cssfile):
for root, dirs, files in os.walk(directory): for root, dirs, files in os.walk(directory):
html = [] html = []
dirs, files = remove_hidden_files_and_folders(dirs, files) dirs, files = _remove_hidden_files_and_folders(dirs, files)
remove_index_html(root, files) _remove_existing_index_html(root, files)
for name in sorted(files): for name in sorted(files):
if is_skipped_file(name, cssfile): if _is_skippable_file(name, cssfile):
continue continue
full_path = os.path.join(root, name) full_path = os.path.join(root, name)
@ -209,7 +209,7 @@ def distribusify(cssfile, directory):
name, full_path, subtype name, full_path, subtype
) )
case "image": case "image":
tag = handle_image_files(name, full_path) tag = _handle_image_files(name, full_path)
if tag is None: if tag is None:
continue continue
case _: case _:
@ -223,7 +223,7 @@ def distribusify(cssfile, directory):
tag = "<a href='{}'>{}</a>" tag = "<a href='{}'>{}</a>"
tag = tag.replace("{}", name) tag = tag.replace("{}", name)
html.append(format_div(type_, subtype, tag, name)) html.append(_format_div(type_, subtype, tag, name))
if root != directory: if root != directory:
html.append('<a href="../index.html">../</a>') html.append('<a href="../index.html">../</a>')
@ -233,4 +233,4 @@ def distribusify(cssfile, directory):
html.insert(0, format_div("dir", "dir", tag, "folder")) html.insert(0, format_div("dir", "dir", tag, "folder"))
index = os.path.join(root, "index.html") index = os.path.join(root, "index.html")
write_index_html(index, html, cssfile) _write_index_html(index, html, cssfile)

View File

@ -2,8 +2,6 @@ import os
import shutil import shutil
import zipfile import zipfile
# Tada!
from distribusi.cli import build_argparser
from distribusi.distribusi import distribusify from distribusi.distribusi import distribusify
from flask import flash, redirect, render_template, url_for from flask import flash, redirect, render_template, url_for
from flask_login import current_user from flask_login import current_user
@ -14,7 +12,7 @@ from sqlalchemy.exc import (
InvalidRequestError, InvalidRequestError,
) )
from app import db from app import db, APP
from distribusikan.add_files_to_describer import add_distribusi_files_to_db from distribusikan.add_files_to_describer import add_distribusi_files_to_db
from distribusikan.distribusi_selector import selector_visible from distribusikan.distribusi_selector import selector_visible
from distribusikan.distribusis_info import DistribusisInfo from distribusikan.distribusis_info import DistribusisInfo
@ -96,10 +94,10 @@ def get_css_file(distribusi):
def run_distribusi(userfolder, cssfile): def run_distribusi(userfolder, cssfile):
print(f"Run distribusi on this folder: {userfolder} with css:{cssfile}") APP.logger.info(
parser = build_argparser() f"Run distribusi on this folder: {userfolder} with css:{cssfile}"
args = parser.parse_args(["-t", "-a", "--menu-with-index", "-s", cssfile]) )
distribusify(args, userfolder) distribusify(userfolder, cssfile)
def set_distribusi_to_visible(distribusi, user): def set_distribusi_to_visible(distribusi, user):

View File

@ -14,17 +14,19 @@ config = context.config
# Interpret the config file for Python logging. # Interpret the config file for Python logging.
# This line sets up loggers basically. # This line sets up loggers basically.
fileConfig(config.config_file_name) fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env') logger = logging.getLogger("alembic.env")
# add your model's MetaData object here # add your model's MetaData object here
# for 'autogenerate' support # for 'autogenerate' support
# from myapp import mymodel # from myapp import mymodel
# target_metadata = mymodel.Base.metadata # target_metadata = mymodel.Base.metadata
config.set_main_option( config.set_main_option(
'sqlalchemy.url', "sqlalchemy.url",
str(current_app.extensions['migrate'].db.get_engine().url).replace( str(current_app.extensions["migrate"].db.get_engine().url).replace(
'%', '%%')) "%", "%%"
target_metadata = current_app.extensions['migrate'].db.metadata ),
)
target_metadata = current_app.extensions["migrate"].db.metadata
# other values from the config, defined by the needs of env.py, # other values from the config, defined by the needs of env.py,
# can be acquired: # can be acquired:
@ -65,20 +67,20 @@ def run_migrations_online():
# when there are no changes to the schema # when there are no changes to the schema
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
def process_revision_directives(context, revision, directives): def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False): if getattr(config.cmd_opts, "autogenerate", False):
script = directives[0] script = directives[0]
if script.upgrade_ops.is_empty(): if script.upgrade_ops.is_empty():
directives[:] = [] directives[:] = []
logger.info('No changes in schema detected.') logger.info("No changes in schema detected.")
connectable = current_app.extensions['migrate'].db.get_engine() connectable = current_app.extensions["migrate"].db.get_engine()
with connectable.connect() as connection: with connectable.connect() as connection:
context.configure( context.configure(
connection=connection, connection=connection,
target_metadata=target_metadata, target_metadata=target_metadata,
process_revision_directives=process_revision_directives, process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args **current_app.extensions["migrate"].configure_args,
) )
with context.begin_transaction(): with context.begin_transaction():