read options from existing html file
This commit is contained in:
@@ -22,20 +22,45 @@ def setup():
|
|||||||
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("--version",action='version', version=VERSION)
|
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")
|
help="Root path of the index")
|
||||||
options=parser.parse_args()
|
options=parser.parse_args()
|
||||||
options.startpath=os.path.abspath(options.startpath)
|
options.path=os.path.abspath(options.path)
|
||||||
if options.title==None:
|
if options.title==None:
|
||||||
options.title=os.path.basename(options.startpath)
|
options.title=os.path.basename(options.path)
|
||||||
return options
|
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):
|
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 opts.filename in files:
|
||||||
if not opts.overwrite:
|
if not opts.overwrite:
|
||||||
print(opts.filename+" exists")
|
print(opts.filename+" exists")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
opts=HTML2setup(opts)
|
||||||
files = [ f for f in files if f != opts.filename]
|
files = [ f for f in files if f != opts.filename]
|
||||||
if not opts.hidden:
|
if not opts.hidden:
|
||||||
files = [ f for f in files if not f.startswith(".")]
|
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')
|
f=open(os.path.join(path,opts.filename),'wt')
|
||||||
dirs.sort()
|
dirs.sort()
|
||||||
files.sort()
|
files.sort()
|
||||||
f.write(get_header(opts.title))
|
f.write(get_header(opts))
|
||||||
if opts.parent:
|
if opts.parent:
|
||||||
f.write(get_pathlink(path,'..'))
|
f.write(get_pathlink(path,'..'))
|
||||||
for di in dirs:
|
for di in dirs:
|
||||||
@@ -67,12 +92,14 @@ def get_pathlink(path,dname):
|
|||||||
fdstr=time.strftime("%Y/%m/%d %H:%M:%S",fdate)
|
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'
|
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">
|
header='''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||||
<meta name="generator" content="SimpleWebPage '''+VERSION+'''" />
|
<meta name="generator" content="SimpleWebPage '''+VERSION+'''" />
|
||||||
<title>Index of '''+title+'''</title>
|
'''+setup2HTML(opts)+'''
|
||||||
|
<title>Index of '''+opts.title+'''</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
@@ -419,7 +446,7 @@ function alternate(table) {
|
|||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<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>
|
<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
|
return header
|
||||||
|
|||||||
Reference in New Issue
Block a user