tsvkit installer, SimpleWebPage with image support
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
import os,sys,time
|
||||
import urllib
|
||||
VERSION="20160116"
|
||||
IMAGE_EXTENSIONS=['png','gif','jpg','jpeg','tif','tiff']
|
||||
|
||||
def setup():
|
||||
''' Setup the command line options '''
|
||||
@@ -21,6 +22,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("--images",action="store_true",dest="images",default=False,
|
||||
help="Show images with <img> tags")
|
||||
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")
|
||||
@@ -34,7 +37,8 @@ def setup2HTML(opts):
|
||||
return '<meta name="SimpleWebPageSetup" content="%s"/>'%";".join([
|
||||
'hidden=%s'%opts.hidden,
|
||||
'parent=%s'%opts.parent,
|
||||
'title=%s'%urllib.quote(opts.title)
|
||||
'title=%s'%urllib.quote(opts.title),
|
||||
'images=%s'%opts.images
|
||||
])
|
||||
|
||||
def HTML2setup(opts):
|
||||
@@ -48,6 +52,7 @@ def HTML2setup(opts):
|
||||
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"
|
||||
print("Reading options from existing "+opts.filename)
|
||||
break
|
||||
except:
|
||||
@@ -74,18 +79,25 @@ def generate_index(opts):
|
||||
for di in dirs:
|
||||
f.write(get_pathlink(path,di))
|
||||
for fi in files:
|
||||
f.write(get_filelink(path,fi))
|
||||
f.write(get_filelink(path,fi,opts.images))
|
||||
f.write(get_footer())
|
||||
f.close()
|
||||
return
|
||||
|
||||
def get_filelink(path,fname):
|
||||
def get_filelink(path,fname,images=False):
|
||||
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)
|
||||
return '<tr><td><a href="'+urllib.quote(fname)+'">'+fname+'</a><td class="right">'+fsstr+'</td><td class="right bytes">'+fsstrb+'</td><td class="right">'+fdstr+'</td></tr>\n'
|
||||
if images and is_imagefile(fname):
|
||||
fname_str=get_imagestr(fname)
|
||||
else:
|
||||
fname_str=fname
|
||||
return '<tr><td><a href="'+urllib.quote(fname)+'">'+fname_str+'</a><td class="right">'+fsstr+'</td><td class="right bytes">'+fsstrb+'</td><td class="right">'+fdstr+'</td></tr>\n'
|
||||
|
||||
def get_imagestr(fname):
|
||||
return '<img src="'+urllib.quote(fname)+'" title="'+fname+'"/>'
|
||||
|
||||
def get_pathlink(path,dname):
|
||||
fdate=time.localtime(os.path.getmtime(os.path.join(path,dname)))
|
||||
@@ -456,6 +468,12 @@ def get_footer():
|
||||
</body></html>'''
|
||||
return footer
|
||||
|
||||
def is_imagefile(fname):
|
||||
for ext in IMAGE_EXTENSIONS:
|
||||
if fname.lower().endswith(ext):
|
||||
return True
|
||||
return False
|
||||
|
||||
def sizeof(num):
|
||||
for x in [' B','KB','MB','GB','TB']:
|
||||
if num < 1024.0:
|
||||
|
||||
Reference in New Issue
Block a user