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
from glob import fnmatch
VERSION = "20181206"
VERSION = "20200427"
IMAGE_EXTENSIONS = ['png', 'gif', 'jpg', 'jpeg', 'tif', 'tiff']
def setup():
@@ -26,6 +26,8 @@ def setup():
help="Output filename (Default: index.html)")
parser.add_argument("-p",action="store_false",dest="parent",default=True,
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,
help="Show images with <img> tags")
parser.add_argument("--include","-i",
@@ -80,32 +82,57 @@ def HTML2setup(opts):
except:
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):
for path,dirs, files in os.walk(opts.path):
existing_config = False
if opts.filename in files:
opts, existing_config = HTML2setup(opts)
if not existing_config and not opts.overwrite:
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(".")]
dirs.sort()
files.sort()
with open(os.path.join(path,opts.filename), 'wt') as f:
f.write(get_header(opts))
if opts.parent:
f.write(get_pathlink(path, '..'))
for di in dirs:
f.write(get_pathlink(path, di))
for fi in files:
f.write(get_filelink(path, fi, opts.images))
f.write(get_footer())
f.close()
return
dirs, files, path = get_files_and_folders(opts)
existing_config = False
if opts.filename in files:
opts, existing_config = HTML2setup(opts)
if not existing_config and not opts.overwrite:
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)
dirs.sort()
files.sort()
files.sort(key = lambda x: x.find("/") > 0)
with open(os.path.join(path,opts.filename), 'wt') as f:
f.write(get_header(opts))
if opts.parent:
f.write(get_pathlink(path, '..'))
for di in dirs:
f.write(get_pathlink(path, di))
for fi in files:
f.write(get_filelink(path, fi, opts.images))
f.write(get_footer())
f.close()
return
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)):