refactor, at least label functions as internal
This commit is contained in:
parent
cc25eb73c1
commit
c6ed9d8416
@ -14,7 +14,7 @@ from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES
|
||||
MIME_TYPE = magic.Magic(mime=True)
|
||||
|
||||
|
||||
def add_alttext(full_path_image):
|
||||
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"
|
||||
@ -26,7 +26,7 @@ def add_alttext(full_path_image):
|
||||
return
|
||||
|
||||
|
||||
def add_description(full_path_image):
|
||||
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"
|
||||
@ -46,7 +46,7 @@ def _read_matching_text_file(image_filename_no_ext, filename):
|
||||
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"):
|
||||
return
|
||||
try:
|
||||
@ -68,7 +68,7 @@ def make_thumbnail(full_path_image, name):
|
||||
return
|
||||
|
||||
|
||||
def format_div(type_, subtype, tag, name):
|
||||
def _format_div(type_, subtype, tag, name):
|
||||
id_name = name.split(".")[0].replace(" ", "_")
|
||||
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)
|
||||
|
||||
|
||||
def check_distribusi_index(index):
|
||||
def _check_distribusi_index(index):
|
||||
"""
|
||||
check whether a index.html file is generated by distribusi
|
||||
"""
|
||||
@ -94,7 +94,7 @@ def check_distribusi_index(index):
|
||||
return False
|
||||
|
||||
|
||||
def write_index_html(index, html, cssfile):
|
||||
def _write_index_html(index, html, cssfile):
|
||||
with open(index, "w") as index_file:
|
||||
styled_html_head = html_head.format(cssfile=cssfile)
|
||||
index_file.write(styled_html_head)
|
||||
@ -103,7 +103,7 @@ def write_index_html(index, html, cssfile):
|
||||
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":
|
||||
subtype = "html"
|
||||
# what types of text files to expand
|
||||
@ -121,13 +121,13 @@ def handle_text_files(name, full_path, subtype):
|
||||
return subtype, tag
|
||||
|
||||
|
||||
def handle_image_files(name, full_path):
|
||||
thumbnail_filename = make_thumbnail(full_path, name)
|
||||
def _handle_image_files(name, full_path):
|
||||
thumbnail_filename = _make_thumbnail(full_path, name)
|
||||
if thumbnail_filename is None:
|
||||
return
|
||||
|
||||
image_alttext = add_alttext(full_path)
|
||||
image_description = add_description(full_path)
|
||||
image_alttext = _add_alttext(full_path)
|
||||
image_description = _add_description(full_path)
|
||||
|
||||
if not image_alttext and not image_description:
|
||||
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")
|
||||
if "index.html" in files:
|
||||
try:
|
||||
@ -164,13 +164,13 @@ def remove_index_html(root, files):
|
||||
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))
|
||||
files = list(filter(lambda f: not f.startswith("."), files))
|
||||
return dirs, files
|
||||
|
||||
|
||||
def is_skipped_file(name, cssfile):
|
||||
def _is_skippable_file(name, cssfile):
|
||||
if "index.html" in name:
|
||||
return True
|
||||
|
||||
@ -187,14 +187,14 @@ def is_skipped_file(name, cssfile):
|
||||
return True
|
||||
|
||||
|
||||
def distribusify(cssfile, directory):
|
||||
def distribusify(directory, cssfile):
|
||||
for root, dirs, files in os.walk(directory):
|
||||
html = []
|
||||
dirs, files = remove_hidden_files_and_folders(dirs, files)
|
||||
remove_index_html(root, files)
|
||||
dirs, files = _remove_hidden_files_and_folders(dirs, files)
|
||||
_remove_existing_index_html(root, files)
|
||||
|
||||
for name in sorted(files):
|
||||
if is_skipped_file(name, cssfile):
|
||||
if _is_skippable_file(name, cssfile):
|
||||
continue
|
||||
|
||||
full_path = os.path.join(root, name)
|
||||
@ -209,7 +209,7 @@ def distribusify(cssfile, directory):
|
||||
name, full_path, subtype
|
||||
)
|
||||
case "image":
|
||||
tag = handle_image_files(name, full_path)
|
||||
tag = _handle_image_files(name, full_path)
|
||||
if tag is None:
|
||||
continue
|
||||
case _:
|
||||
@ -223,7 +223,7 @@ def distribusify(cssfile, directory):
|
||||
tag = "<a href='{}'>{}</a>"
|
||||
|
||||
tag = tag.replace("{}", name)
|
||||
html.append(format_div(type_, subtype, tag, name))
|
||||
html.append(_format_div(type_, subtype, tag, name))
|
||||
|
||||
if root != directory:
|
||||
html.append('<a href="../index.html">../</a>')
|
||||
@ -233,4 +233,4 @@ def distribusify(cssfile, directory):
|
||||
html.insert(0, format_div("dir", "dir", tag, "folder"))
|
||||
|
||||
index = os.path.join(root, "index.html")
|
||||
write_index_html(index, html, cssfile)
|
||||
_write_index_html(index, html, cssfile)
|
||||
|
@ -2,8 +2,6 @@ import os
|
||||
import shutil
|
||||
import zipfile
|
||||
|
||||
# Tada!
|
||||
from distribusi.cli import build_argparser
|
||||
from distribusi.distribusi import distribusify
|
||||
from flask import flash, redirect, render_template, url_for
|
||||
from flask_login import current_user
|
||||
@ -14,7 +12,7 @@ from sqlalchemy.exc import (
|
||||
InvalidRequestError,
|
||||
)
|
||||
|
||||
from app import db
|
||||
from app import db, APP
|
||||
from distribusikan.add_files_to_describer import add_distribusi_files_to_db
|
||||
from distribusikan.distribusi_selector import selector_visible
|
||||
from distribusikan.distribusis_info import DistribusisInfo
|
||||
@ -96,10 +94,10 @@ def get_css_file(distribusi):
|
||||
|
||||
|
||||
def run_distribusi(userfolder, cssfile):
|
||||
print(f"Run distribusi on this folder: {userfolder} with css:{cssfile}")
|
||||
parser = build_argparser()
|
||||
args = parser.parse_args(["-t", "-a", "--menu-with-index", "-s", cssfile])
|
||||
distribusify(args, userfolder)
|
||||
APP.logger.info(
|
||||
f"Run distribusi on this folder: {userfolder} with css:{cssfile}"
|
||||
)
|
||||
distribusify(userfolder, cssfile)
|
||||
|
||||
|
||||
def set_distribusi_to_visible(distribusi, user):
|
||||
|
@ -14,17 +14,19 @@ config = context.config
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
logger = logging.getLogger('alembic.env')
|
||||
logger = logging.getLogger("alembic.env")
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
config.set_main_option(
|
||||
'sqlalchemy.url',
|
||||
str(current_app.extensions['migrate'].db.get_engine().url).replace(
|
||||
'%', '%%'))
|
||||
target_metadata = current_app.extensions['migrate'].db.metadata
|
||||
"sqlalchemy.url",
|
||||
str(current_app.extensions["migrate"].db.get_engine().url).replace(
|
||||
"%", "%%"
|
||||
),
|
||||
)
|
||||
target_metadata = current_app.extensions["migrate"].db.metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
@ -65,20 +67,20 @@ def run_migrations_online():
|
||||
# when there are no changes to the schema
|
||||
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
|
||||
def process_revision_directives(context, revision, directives):
|
||||
if getattr(config.cmd_opts, 'autogenerate', False):
|
||||
if getattr(config.cmd_opts, "autogenerate", False):
|
||||
script = directives[0]
|
||||
if script.upgrade_ops.is_empty():
|
||||
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:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
process_revision_directives=process_revision_directives,
|
||||
**current_app.extensions['migrate'].configure_args
|
||||
**current_app.extensions["migrate"].configure_args,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
|
Loading…
Reference in New Issue
Block a user