#!/usr/bin/env python
# coding=utf-8
''' A script that creates an index for a folder.
'''
import os,sys,time
import urllib
from glob import fnmatch
VERSION = "20200427"
IMAGE_EXTENSIONS = ['png', 'gif', 'jpg', 'jpeg', 'tif', 'tiff']
def setup():
''' Setup the command line options '''
from argparse import ArgumentParser
parser = ArgumentParser(
epilog = "Recursively generate indexes: \n# find . -type d -not -path '*/.*' -exec SimpleWebPage -f \{\} \;"
)
parser.add_argument("-f",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!")
parser.add_argument("-H",action="store_true",dest="hidden",default=False,
help="Show hidden files")
parser.add_argument("-t",type=str,dest="title",default=None,
help="Name for the title (Default: Folder name)")
parser.add_argument("-o",type=str,dest="filename",default="index.html",
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 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"
)
options = parser.parse_args()
options.path = os.path.abspath(options.path)
if options.title == None:
options.title = os.path.basename(options.path)
return options
def setup2HTML(opts):
return ''%";".join([
'hidden=%s'%opts.hidden,
'parent=%s'%opts.parent,
'title=%s'%urllib.quote(opts.title),
'images=%s'%opts.images
])
def HTML2setup(opts):
""" returns new opts and was it able to read HTML """
try:
read_config = False
with open(os.path.join(opts.path,opts.filename), 'rt') as f:
for l in f.readlines():
if l.find(' -1:
content = l[l.find('name="SimpleWebPageSetup"'):]
for s in content.split('"')[3].split(";"):
(k,v) = s.split('=',1)
if k == 'hidden': opts.hidden = v == "True"
if k == 'parent': opts.parent = v == "True"
if k == 'title': opts.title = urllib.unquote(v)
if k == 'images': opts.images = v == "True"
read_config = True
print("Reading options from existing " + opts.filename)
break
return (opts, read_config)
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):
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)):
(fsize, fsstr, fsstrb, fdstr)=(0, "NA", "NA", "NA")
else:
fsize = os.path.getsize(os.path.join(path, fname))
fsstr = sizeof(fsize)
fsstrb = str(fsize)
fdate = time.localtime(os.path.getmtime(os.path.join(path, fname)))
fdstr = time.strftime("%Y/%m/%d %H:%M:%S", fdate)
if images and is_imagefile(fname):
fname_str = get_imagestr(fname)
else:
fname_str = fname
return '
| Name | Size | Size B | Modified |
|---|