From f3acf9f5cce1ccffab62f7fcbd773b5fed813ef5 Mon Sep 17 00:00:00 2001 From: Q Date: Sat, 15 Feb 2020 12:09:58 +0200 Subject: [PATCH] SimpleWebPage with includes --- web/SimpleWebPage.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/web/SimpleWebPage.py b/web/SimpleWebPage.py index 00ab550..88d841a 100755 --- a/web/SimpleWebPage.py +++ b/web/SimpleWebPage.py @@ -5,6 +5,7 @@ import os,sys,time import urllib +from glob import fnmatch VERSION = "20181206" IMAGE_EXTENSIONS = ['png', 'gif', 'jpg', 'jpeg', 'tif', 'tiff'] @@ -27,9 +28,22 @@ def setup(): help="Do no print .. link for parent folder.") parser.add_argument("--images",action="store_true",dest="images",default=False, help="Show images with tags") + parser.add_argument("--include","-i", + action="store", + dest="includes", + default=['*'], + help="Glob match for files to be included in the table. ex. *.jpg. You can pass several includes.", + nargs = '*' + ) parser.add_argument("--version",action='version', version=VERSION) - parser.add_argument("path",type=str,action="store",default=os.path.abspath('.'),nargs='?', - help="Root path of the index") + parser.add_argument( + "path", + type=str, + action="store", + default=os.path.abspath('.'), + nargs='?', + help="Root path of the index" + ) options = parser.parse_args() options.path = os.path.abspath(options.path) @@ -75,6 +89,7 @@ def generate_index(opts): print(opts.filename + " exists, and not generated with SimpleWebPage. Exiting.") sys.exit(1) files = [ f for f in files if f != opts.filename] + files = match_files(files, opts.includes) if not opts.hidden: files = [ f for f in files if not f.startswith(".")] dirs = [ d for d in dirs if not d.startswith(".")] @@ -550,6 +565,17 @@ def is_imagefile(fname): return True return False + +def match_files(files, glob_list): + matched = [] + for f in files: + for g in glob_list: + if fnmatch.fnmatch(f,g): + matched.append(f) + break + return matched + + def sizeof(num): for x in [' B','KB','MB','GB','TB']: if num < 1024.0: