diff --git a/distribusi/cli.py b/distribusi/cli.py deleted file mode 100644 index 8f4c10c..0000000 --- a/distribusi/cli.py +++ /dev/null @@ -1,89 +0,0 @@ -import argparse -import os - -from distribusi.distribusi import distribusify - - -def build_argparser(): - parser = argparse.ArgumentParser( - """ - distribusi is a content management system for the web that produces static - index pages based on folders in the files system. It is inspired by the - automatic index functions featured in several popular web servers. - distribusi works by traversing the file system and directory hierarchy to - automatically list all the files in the directory, detect the file types - and providing them with relevant html classes and tags for easy styling. - """ - ) - - parser.add_argument( - "-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( - "-v", - "--verbose", - help="Print verbose debug output", - action="store_true", - ) - - parser.add_argument( - "-t", - "--thumbnail", - help="Generate 450x450 thumbnails for images", - action="store_true", - ) - - parser.add_argument( - "-a", - "--alttexts", - help="Adds file alttext based on same named files", - action="store_true", - ) - - parser.add_argument( - "-r", - "--remove-index", - help="Recursively removes all instances of index.html that have been previously made by distribusi", - action="store_true", - ) - - parser.add_argument( - "-e", - "--exclude-directory", - help="Exclude one or multiple directories from indexing", - nargs="*", - metavar="DIR", - ) - - parser.add_argument( - "-f", - "--force", - help="Force whether distribusi overwrites or removes instances of index.html not generated by distribusi, use at own risk!", - action="store_true", - ) - - parser.add_argument( - "--no-hidden", help="Exclude hidden directories", action="store_true" - ) - - parser.add_argument( - "--menu-with-index", - help="Append index.html to menu items to aid navigation", - action="store_true", - ) - - return parser - - -def cli_entrypoint(): - parser = build_argparser() - args = parser.parse_args() - distribusify(args, args.directory) diff --git a/distribusi/distribusi.py b/distribusi/distribusi.py index 6b09016..a8b0dec 100644 --- a/distribusi/distribusi.py +++ b/distribusi/distribusi.py @@ -46,7 +46,7 @@ def _read_matching_text_file(image_filename_no_ext, filename): return text_file.read() -def thumbnail(full_path_image, name, args): +def make_thumbnail(full_path_image, name): if full_path_image.endswith("_thumbnail.jpg"): return try: @@ -68,7 +68,7 @@ def thumbnail(full_path_image, name, args): return -def format_div(args, type_, subtype, tag, name): +def format_div(type_, subtype, tag, name): id_name = name.split(".")[0].replace(" ", "_") filename = f'{name}' @@ -84,25 +84,19 @@ def format_div(args, type_, subtype, tag, name): return html.format(id_name, subtype, tag) -def check_distribusi_index(args, index): +def check_distribusi_index(index): """ check whether a index.html file is generated by distribusi """ - if not args.force: - with open(index, "r") as f: - if '' in f.read(): - return True - else: - if args.verbose: - print(index, "not generated by distribusi, skipping") - return False - elif args.force: - return True + with open(index, "r") as f: + if '' in f.read(): + return True + return False -def write_index_html(args, index, html): +def write_index_html(index, html, cssfile): with open(index, "w") as index_file: - styled_html_head = html_head.format(cssfile=args.style) + styled_html_head = html_head.format(cssfile=cssfile) index_file.write(styled_html_head) for line in html: index_file.write(line + "\n") @@ -127,47 +121,43 @@ def handle_text_files(name, full_path, subtype): return subtype, tag -def handle_image_files(name, full_path, args): - if args.thumbnail: - thumbnail_filename = thumbnail(full_path, name, args) - if thumbnail_filename is None: - return +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( - name=name, thumbnail_filename=thumbnail_filename - ) - if not image_alttext: - return image_with_description.format( - name=name, - thumbnail_filename=thumbnail_filename, - image_description=image_description, - ) - if not image_description: - return image_with_alttext.format( - name=name, - thumbnail_filename=thumbnail_filename, - image_alttext=image_alttext, - ) - return image_full_figure.format( + if not image_alttext and not image_description: + return image_no_description.format( + name=name, thumbnail_filename=thumbnail_filename + ) + if not image_alttext: + return image_with_description.format( + name=name, + thumbnail_filename=thumbnail_filename, + image_description=image_description, + ) + if not image_description: + return image_with_alttext.format( name=name, thumbnail_filename=thumbnail_filename, image_alttext=image_alttext, - image_description=image_description, ) - return FILE_TYPES["image"].format(name, image_alttext) + return image_full_figure.format( + name=name, + thumbnail_filename=thumbnail_filename, + image_alttext=image_alttext, + image_description=image_description, + ) -def remove_index_html(root, files, args): +def remove_index_html(root, files): index = os.path.join(root, "index.html") if "index.html" in files: try: - if check_distribusi_index(args, index): - if args.verbose: - print("Removing index.html from", root) + if check_distribusi_index(index): os.remove(index) except Exception as e: print(e) @@ -180,7 +170,7 @@ def remove_hidden_files_and_folders(dirs, files): return dirs, files -def is_skipped_file(name, args): +def is_skipped_file(name, cssfile): if "index.html" in name: return True @@ -193,32 +183,18 @@ def is_skipped_file(name, args): if name.endswith("_dv_description.txt"): return True - if args.style in name: + if cssfile in name: return True -def distribusify(args, directory): # noqa +def distribusify(cssfile, directory): for root, dirs, files in os.walk(directory): html = [] - - if args.exclude_directory: - if args.verbose: - print( - "Excluding directory:", ", ".join(args.exclude_directory) - ) - dirs[:] = [d for d in dirs if d not in args.exclude_directory] - - if args.no_hidden: - dirs, files = remove_hidden_files_and_folders(dirs, files) - - if args.remove_index: - remove_index_html(root, files, args) - - if args.verbose: - print("Generating directory listing for", root) + dirs, files = remove_hidden_files_and_folders(dirs, files) + remove_index_html(root, files) for name in sorted(files): - if is_skipped_file(name, args): + if is_skipped_file(name, cssfile): continue full_path = os.path.join(root, name) @@ -226,8 +202,6 @@ def distribusify(args, directory): # noqa type_, subtype = mime.split("/") alttext = name - if args.verbose: - print("Found", name, "as", mime) if type_ in FILE_TYPES: match type_: case "text": @@ -235,7 +209,7 @@ def distribusify(args, directory): # noqa name, full_path, subtype ) case "image": - tag = handle_image_files(name, full_path, args) + tag = handle_image_files(name, full_path) if tag is None: continue case _: @@ -247,29 +221,16 @@ def distribusify(args, directory): # noqa if type_ not in FILE_TYPES and subtype not in SUB_TYPES: # 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" - ) - print(type_, subtype, message, name) - subtype = subtype + " unkown-file" tag = tag.replace("{}", name) - html.append(format_div(args, type_, subtype, tag, name)) + html.append(format_div(type_, subtype, tag, name)) if root != directory: - if args.menu_with_index: - html.append('../') - else: - html.append('../') + html.append('../') for name in sorted(dirs): - if args.menu_with_index: - tag = "{}".replace("{}", name) - else: - tag = "{}/".replace("{}", name) - - html.insert(0, format_div(args, "dir", "dir", tag, "folder")) + tag = "{}".replace("{}", name) + html.insert(0, format_div("dir", "dir", tag, "folder")) index = os.path.join(root, "index.html") - write_index_html(args, index, html) + write_index_html(index, html, cssfile) diff --git a/distribusi/templates/page_template.py b/distribusi/templates/page_template.py index 5f55cc0..1d51c6c 100644 --- a/distribusi/templates/page_template.py +++ b/distribusi/templates/page_template.py @@ -1,4 +1,4 @@ -html_head = '''\ +html_head = """\ @@ -8,9 +8,9 @@ html_head = '''\ -''' +""" -html_footer ='''\ +html_footer = """\ -''' +""" diff --git a/verse/distribusikan/distribusi_selector.py b/verse/distribusikan/distribusi_selector.py index 2021ba2..f7d9e53 100644 --- a/verse/distribusikan/distribusi_selector.py +++ b/verse/distribusikan/distribusi_selector.py @@ -112,6 +112,10 @@ def _delete_distribusi_files(distribusiname): cssfolder = os.path.join("themes/userthemes", distribusiname) if os.path.exists(cssfolder): shutil.rmtree(cssfolder) + + distribusi = Distribusis.query.filter_by( + distribusiname=distribusiname + ).first() if distribusi.publictheme is not None: publicthemefolder = os.path.join("themes/publicthemes", distribusiname) if os.path.exists(publicthemefolder): diff --git a/verse/distribusikan/upload.py b/verse/distribusikan/upload.py index 0bc63fe..4067bb2 100644 --- a/verse/distribusikan/upload.py +++ b/verse/distribusikan/upload.py @@ -79,7 +79,9 @@ def upload_updates_files(uploadfolder): db.session.commit() except (InvalidRequestError, DataError, InterfaceError, DatabaseError): db.session.rollback() - uploadform.sitename.errors.append("Something went wrong with the database!") + uploadform.sitename.errors.append( + "Something went wrong with the database!" + ) zipfilename = "{}.zip".format(distribusi.distribusiname) zipfile = uploadform.zipfile.data diff --git a/verse/migrations/env.py b/verse/migrations/env.py index 68a0091..68feded 100644 --- a/verse/migrations/env.py +++ b/verse/migrations/env.py @@ -14,19 +14,17 @@ 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: @@ -67,20 +65,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():