reverse sort and force behavior change

This commit is contained in:
ville rantanen
2021-10-14 09:44:20 +03:00
parent 96120bad3f
commit e9863883b4

View File

@@ -20,7 +20,7 @@ try:
except ImportError: except ImportError:
MARKDOWN_AVAILABLE = False MARKDOWN_AVAILABLE = False
VERSION = "20211002" VERSION = "20211012"
IMAGE_EXTENSIONS = ["png", "gif", "jpg", "jpeg", "tif", "tiff"] IMAGE_EXTENSIONS = ["png", "gif", "jpg", "jpeg", "tif", "tiff"]
AUDIO_EXTENSIONS = ["wav", "mp3", "ogg"] AUDIO_EXTENSIONS = ["wav", "mp3", "ogg"]
VIDEO_EXTENSIONS = ["mp4", "ogg", "webm"] VIDEO_EXTENSIONS = ["mp4", "ogg", "webm"]
@@ -33,6 +33,7 @@ SAFE_OPTS = (
"media", "media",
"includes", "includes",
"no_readme", "no_readme",
"reverse"
) )
@@ -41,14 +42,14 @@ def setup():
from argparse import ArgumentParser from argparse import ArgumentParser
parser = 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( parser.add_argument(
"-f", "-f","--force",
action="store_true", action="store_true",
dest="overwrite", dest="overwrite",
default=False, 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( parser.add_argument(
"-H", "-H",
@@ -97,7 +98,14 @@ def setup():
action="store_true", action="store_true",
dest="recursive", dest="recursive",
default=False, 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( parser.add_argument(
"--images", "--images",
@@ -157,8 +165,9 @@ def setup2JSON(opts):
return json.dumps(setup2safe(opts)) return json.dumps(setup2safe(opts))
def HTML2setup(opts): def HTML2setup(opts, overwrite):
""" returns new opts and was it able to read HTML """ """ returns new opts and was it able to read HTML, if overwriting, do not update config """
try: try:
read_config = False read_config = False
with open(os.path.join(opts.path, opts.filename), "rt") as f: with open(os.path.join(opts.path, opts.filename), "rt") as f:
@@ -168,9 +177,12 @@ def HTML2setup(opts):
config = json.loads(content[9:]) config = json.loads(content[9:])
for key in config: for key in config:
if key in SAFE_OPTS: if key in SAFE_OPTS:
if not overwrite:
setattr(opts, key, config[key]) setattr(opts, key, config[key])
read_config = True read_config = True
if not overwrite:
print("Read options from existing " + opts.filename) print("Read options from existing " + opts.filename)
print("Command line switches are not used.")
break break
return (opts, read_config) return (opts, read_config)
except Exception as e: except Exception as e:
@@ -212,7 +224,7 @@ def generate_index(opts):
opts.password_filename = opts.filename opts.password_filename = opts.filename
opts.filename = generate_password_page(path, opts.filename, opts.password) opts.filename = generate_password_page(path, opts.filename, opts.password)
if opts.filename in files: 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: if not existing_config and not opts.overwrite:
print( print(
opts.filename opts.filename
@@ -226,8 +238,8 @@ def generate_index(opts):
files = [f for f in files if f != opts.password_filename] files = [f for f in files if f != opts.password_filename]
files = match_files(files, opts.includes) files = match_files(files, opts.includes)
dirs = match_files(dirs, opts.includes) dirs = match_files(dirs, opts.includes)
dirs.sort() dirs.sort(reverse = opts.reverse)
files.sort() files.sort(reverse = opts.reverse)
files.sort(key=lambda x: x.find("/") > 0) files.sort(key=lambda x: x.find("/") > 0)
readme = get_readme(path, opts.no_readme) readme = get_readme(path, opts.no_readme)
with open(os.path.join(path, opts.filename), "wt") as f: with open(os.path.join(path, opts.filename), "wt") as f: