From e9863883b4eff6821a2533d83159d66a79acc651 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Thu, 14 Oct 2021 09:44:20 +0300 Subject: [PATCH] reverse sort and force behavior change --- web/SimpleWebPage.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/web/SimpleWebPage.py b/web/SimpleWebPage.py index 7fa2020..f9a5c35 100755 --- a/web/SimpleWebPage.py +++ b/web/SimpleWebPage.py @@ -20,7 +20,7 @@ try: except ImportError: MARKDOWN_AVAILABLE = False -VERSION = "20211002" +VERSION = "20211012" IMAGE_EXTENSIONS = ["png", "gif", "jpg", "jpeg", "tif", "tiff"] AUDIO_EXTENSIONS = ["wav", "mp3", "ogg"] VIDEO_EXTENSIONS = ["mp4", "ogg", "webm"] @@ -33,6 +33,7 @@ SAFE_OPTS = ( "media", "includes", "no_readme", + "reverse" ) @@ -41,14 +42,14 @@ def setup(): from argparse import ArgumentParser parser = ArgumentParser( - epilog="Recursively generate indexes: \n# find . -type d -not -path '*/.*' -exec SimpleWebPage -f \{\} \;" + epilog="Recursively generate indexes: \n# find . -type d -not -path '*/.*' -exec SimpleWebPage \{\} \;" ) parser.add_argument( - "-f", + "-f","--force", action="store_true", dest="overwrite", default=False, - help="Overwrite existing index file, even if it's not generated with SimpleWebPage. By default, if file is generated with SimpleWebPage it will be overwritten!", + help="Overwrite existing index file, even if it's not generated with SimpleWebPage. By default, if file is generated with SimpleWebPage it will be overwritten! This option will _not_ read existing config, but will replace it with command line switches.", ) parser.add_argument( "-H", @@ -97,7 +98,14 @@ def setup(): action="store_true", dest="recursive", default=False, - help="Include all files recursively in the list. Do not include any folders.", + help="Include all files recursively in the list. Folders are not shown as links.", + ) + parser.add_argument( + "--reverse", + action="store_true", + dest="reverse", + default=False, + help="Reverse sort" ) parser.add_argument( "--images", @@ -157,8 +165,9 @@ def setup2JSON(opts): return json.dumps(setup2safe(opts)) -def HTML2setup(opts): - """ returns new opts and was it able to read HTML """ +def HTML2setup(opts, overwrite): + """ returns new opts and was it able to read HTML, if overwriting, do not update config """ + try: read_config = False with open(os.path.join(opts.path, opts.filename), "rt") as f: @@ -168,9 +177,12 @@ def HTML2setup(opts): config = json.loads(content[9:]) for key in config: if key in SAFE_OPTS: - setattr(opts, key, config[key]) + if not overwrite: + setattr(opts, key, config[key]) read_config = True - print("Read options from existing " + opts.filename) + if not overwrite: + print("Read options from existing " + opts.filename) + print("Command line switches are not used.") break return (opts, read_config) except Exception as e: @@ -212,7 +224,7 @@ def generate_index(opts): opts.password_filename = opts.filename opts.filename = generate_password_page(path, opts.filename, opts.password) if opts.filename in files: - opts, existing_config = HTML2setup(opts) + opts, existing_config = HTML2setup(opts, opts.overwrite) if not existing_config and not opts.overwrite: print( opts.filename @@ -226,8 +238,8 @@ def generate_index(opts): files = [f for f in files if f != opts.password_filename] files = match_files(files, opts.includes) dirs = match_files(dirs, opts.includes) - dirs.sort() - files.sort() + dirs.sort(reverse = opts.reverse) + files.sort(reverse = opts.reverse) files.sort(key=lambda x: x.find("/") > 0) readme = get_readme(path, opts.no_readme) with open(os.path.join(path, opts.filename), "wt") as f: