SimpleWbPage with recursive file listing

This commit is contained in:
Ville Rantanen
2020-04-27 12:38:42 +03:00
parent b08b2fac78
commit 84d6b299e7

View File

@@ -7,7 +7,7 @@ import os,sys,time
import urllib import urllib
from glob import fnmatch from glob import fnmatch
VERSION = "20181206" VERSION = "20200427"
IMAGE_EXTENSIONS = ['png', 'gif', 'jpg', 'jpeg', 'tif', 'tiff'] IMAGE_EXTENSIONS = ['png', 'gif', 'jpg', 'jpeg', 'tif', 'tiff']
def setup(): def setup():
@@ -26,6 +26,8 @@ def setup():
help="Output filename (Default: index.html)") help="Output filename (Default: index.html)")
parser.add_argument("-p",action="store_false",dest="parent",default=True, parser.add_argument("-p",action="store_false",dest="parent",default=True,
help="Do no print .. link for parent folder.") help="Do no print .. link for parent folder.")
parser.add_argument("-r",action="store_true",dest="recursive",default=False,
help="Include all files recursively in the list. Do not include any folders.")
parser.add_argument("--images",action="store_true",dest="images",default=False, parser.add_argument("--images",action="store_true",dest="images",default=False,
help="Show images with <img> tags") help="Show images with <img> tags")
parser.add_argument("--include","-i", parser.add_argument("--include","-i",
@@ -80,32 +82,57 @@ def HTML2setup(opts):
except: except:
return (opts, False) return (opts, False)
def get_files_and_folders(opts):
if opts.recursive:
rdirs = []
rfiles = []
rpath = None
for path, dirs, files in os.walk(opts.path):
if rpath == None:
rpath = path
if not opts.hidden:
files = [ f for f in files if not f.startswith(".")]
dirs[:] = [ d for d in dirs if not d.startswith(".")]
files = [os.path.join(os.path.relpath(path, opts.path), f) for f in files]
files = [f[2:] if f.startswith("./") else f for f in files ]
rfiles.extend(files)
return rdirs, rfiles, rpath
else:
for path, dirs, files in os.walk(opts.path):
if not opts.hidden:
files = [ f for f in files if not f.startswith(".")]
dirs = [ d for d in dirs if not d.startswith(".")]
return dirs, files, path
def generate_index(opts): def generate_index(opts):
for path,dirs, files in os.walk(opts.path): dirs, files, path = get_files_and_folders(opts)
existing_config = False existing_config = False
if opts.filename in files: if opts.filename in files:
opts, existing_config = HTML2setup(opts) opts, existing_config = HTML2setup(opts)
if not existing_config and not opts.overwrite: if not existing_config and not opts.overwrite:
print(opts.filename + " exists, and not generated with SimpleWebPage. Exiting.") print(opts.filename + " exists, and not generated with SimpleWebPage. Exiting.")
sys.exit(1) sys.exit(1)
files = [ f for f in files if f != opts.filename] files = [ f for f in files if f != opts.filename]
files = match_files(files, opts.includes) files = match_files(files, opts.includes)
if not opts.hidden: dirs.sort()
files = [ f for f in files if not f.startswith(".")] files.sort()
dirs = [ d for d in dirs if not d.startswith(".")] files.sort(key = lambda x: x.find("/") > 0)
dirs.sort() with open(os.path.join(path,opts.filename), 'wt') as f:
files.sort() f.write(get_header(opts))
with open(os.path.join(path,opts.filename), 'wt') as f: if opts.parent:
f.write(get_header(opts)) f.write(get_pathlink(path, '..'))
if opts.parent: for di in dirs:
f.write(get_pathlink(path, '..')) f.write(get_pathlink(path, di))
for di in dirs: for fi in files:
f.write(get_pathlink(path, di)) f.write(get_filelink(path, fi, opts.images))
for fi in files: f.write(get_footer())
f.write(get_filelink(path, fi, opts.images)) f.close()
f.write(get_footer()) return
f.close()
return
def get_filelink(path,fname,images=False): def get_filelink(path,fname,images=False):
if os.path.islink(os.path.join(path, fname)) and not os.path.exists(os.path.join(path, fname)): if os.path.islink(os.path.join(path, fname)) and not os.path.exists(os.path.join(path, fname)):