read options from existing html file

This commit is contained in:
q
2016-01-16 14:07:25 +02:00
parent f0d92805f5
commit 515a531eab

View File

@@ -22,20 +22,45 @@ def setup():
parser.add_argument("-p",action="store_false",dest="parent",default=True,
help="Do no print .. link for parent folder.")
parser.add_argument("--version",action='version', version=VERSION)
parser.add_argument("startpath",type=str,action="store",default=os.path.abspath('.'),nargs='?',
parser.add_argument("path",type=str,action="store",default=os.path.abspath('.'),nargs='?',
help="Root path of the index")
options=parser.parse_args()
options.startpath=os.path.abspath(options.startpath)
options.path=os.path.abspath(options.path)
if options.title==None:
options.title=os.path.basename(options.startpath)
options.title=os.path.basename(options.path)
return options
def setup2HTML(opts):
return '<meta name="SimpleWebPageSetup" content="%s"/>'%";".join([
'hidden=%s'%opts.hidden,
'parent=%s'%opts.parent,
'title=%s'%urllib.quote(opts.title)
])
def HTML2setup(opts):
f=open(os.path.join(opts.path,opts.filename), 'rt')
try:
for l in f.readlines():
if l.find('name="SimpleWebPageSetup"')>-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)
print("Reading options from existing "+opts.filename)
break
except:
pass
return opts
def generate_index(opts):
for path,dirs,files in os.walk(opts.startpath):
for path,dirs,files in os.walk(opts.path):
if opts.filename in files:
if not opts.overwrite:
print(opts.filename+" exists")
sys.exit(1)
opts=HTML2setup(opts)
files = [ f for f in files if f != opts.filename]
if not opts.hidden:
files = [ f for f in files if not f.startswith(".")]
@@ -43,7 +68,7 @@ def generate_index(opts):
f=open(os.path.join(path,opts.filename),'wt')
dirs.sort()
files.sort()
f.write(get_header(opts.title))
f.write(get_header(opts))
if opts.parent:
f.write(get_pathlink(path,'..'))
for di in dirs:
@@ -67,12 +92,14 @@ def get_pathlink(path,dname):
fdstr=time.strftime("%Y/%m/%d %H:%M:%S",fdate)
return '<tr><td><a href="'+urllib.quote(dname)+'">'+dname+'</a><td class="right">[DIR]</td><td class="right bytes">0</td><td class="right">'+fdstr+'</td></tr>\n'
def get_header(title):
def get_header(opts):
header='''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="SimpleWebPage '''+VERSION+'''" />
<title>Index of '''+title+'''</title>
'''+setup2HTML(opts)+'''
<title>Index of '''+opts.title+'''</title>
<style>
body {
font-family: monospace;
@@ -419,7 +446,7 @@ function alternate(table) {
</script>
</head>
<body>
<h1>Index of '''+title+'''</h1>
<h1>Index of '''+opts.title+'''</h1>
<table class="sortable" id="fileList"><thead><tr><th>Name</th><th class="right">Size</th><th class="right">Size B</th><th class="right">Modified</th></tr></thead><tbody>
'''
return header