From e93a3e3b28a62cbae57e2bd2d05fffaf741f5e2d Mon Sep 17 00:00:00 2001 From: crunk Date: Fri, 22 Nov 2024 16:15:46 +0100 Subject: [PATCH] refactoring --- distribusi/distribusi.py | 45 +++++++++++++-------------- distribusi/templates/page_template.py | 19 +++-------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/distribusi/distribusi.py b/distribusi/distribusi.py index d811ea1..6b09016 100644 --- a/distribusi/distribusi.py +++ b/distribusi/distribusi.py @@ -100,19 +100,12 @@ def check_distribusi_index(args, index): return True -def write_index(args, index, html): +def write_index_html(args, index, html): with open(index, "w") as index_file: - if args.style: - file_style = open(args.style, "r") - style = file_style.read() - styled_html_head = html_head % style - else: - styled_html_head = html_head % "" + styled_html_head = html_head.format(cssfile=args.style) index_file.write(styled_html_head) - for line in html: index_file.write(line + "\n") - index_file.write(html_footer) @@ -187,6 +180,23 @@ def remove_hidden_files_and_folders(dirs, files): return dirs, files +def is_skipped_file(name, args): + if "index.html" in name: + return True + + if name.endswith("_thumbnail.jpg"): + return True + + if name.endswith("_alttext.txt"): + return True + + if name.endswith("_dv_description.txt"): + return True + + if args.style in name: + return True + + def distribusify(args, directory): # noqa for root, dirs, files in os.walk(directory): html = [] @@ -208,16 +218,7 @@ def distribusify(args, directory): # noqa print("Generating directory listing for", root) for name in sorted(files): - if "index.html" in name: - continue - - if name.endswith("_thumbnail.jpg"): - continue - - if name.endswith("_alttext.txt"): - continue - - if name.endswith("_dv_description.txt"): + if is_skipped_file(name, args): continue full_path = os.path.join(root, name) @@ -271,8 +272,4 @@ def distribusify(args, directory): # noqa html.insert(0, format_div(args, "dir", "dir", tag, "folder")) index = os.path.join(root, "index.html") - if os.path.exists(index): - if check_distribusi_index(args, index): - write_index(args, index, html) - elif not os.path.exists(index): - write_index(args, index, html) + write_index_html(args, index, html) diff --git a/distribusi/templates/page_template.py b/distribusi/templates/page_template.py index 1d70ccc..5f55cc0 100644 --- a/distribusi/templates/page_template.py +++ b/distribusi/templates/page_template.py @@ -1,25 +1,16 @@ -html_head = """ +html_head = '''\ - + -""" +''' -html_footer = """ +html_footer ='''\ -""" +'''