added --force flag to force overwriting non-distribusi indexes, code formatting

This commit is contained in:
RRA 2019-09-15 15:06:23 +02:00
parent e21a78324f
commit 010020e08b
2 changed files with 40 additions and 24 deletions

View File

@ -64,8 +64,13 @@ def build_argparser():
'--exclude-directory', '--exclude-directory',
help="Exclude one or multiple directories from indexing", help="Exclude one or multiple directories from indexing",
nargs="*", nargs="*",
metavar='DIR' metavar='DIR')
)
parser.add_argument(
'-f',
'--force',
help="Force whether distribusi overwrites or removes instances of index.html not generated by distribusi, use at own risk!",
action="store_true")
return parser return parser

View File

@ -69,14 +69,37 @@ def div(args, type_, subtype, tag, name):
def check_distribusi_index(args, index): def check_distribusi_index(args, index):
"""
check whether a index.html file is generated by distribusi
"""
with open(index, 'r') as f: if not args.force:
if '<meta name="generator" content="distribusi" />' in f.read(): with open(index, 'r') as f:
return True if '<meta name="generator" content="distribusi" />' in f.read():
else: return True
if args.verbose: else:
print(index, 'not generated by distribusi, skipping') if args.verbose:
return False print(index, 'not generated by distribusi, skipping')
return False
elif args.force:
return True
def write_index(args,index, html, html_head, html_footer):
with open(index, 'w') as f:
if not args.no_template:
if args.style:
fs = open(args.style, "r")
style = fs.read()
styled_html_head = html_head % style
else:
styled_html_head = html_head % ''
f.write(styled_html_head)
for line in html:
f.write(line + '\n')
if not args.no_template:
f.write(html_footer)
def distribusify(args, directory): # noqa def distribusify(args, directory): # noqa
@ -153,23 +176,11 @@ def distribusify(args, directory): # noqa
index = os.path.join(root, 'index.html') index = os.path.join(root, 'index.html')
if os.path.exists(index): if os.path.exists(index):
if check_distribusi_index(args, index): if check_distribusi_index(args, index):
pass write_index(args,index,html, html_head, html_footer)
else: elif not os.path.exists(index):
with open(os.path.join(root, 'index.html'), 'w') as f: write_index(args,index,html, html_head, html_footer)
if not args.no_template:
if args.style:
fs = open(args.style, "r")
style = fs.read()
styled_html_head = html_head % style
else:
styled_html_head = html_head % ''
f.write(styled_html_head)
for line in html:
f.write(line + '\n')
if not args.no_template:
f.write(html_footer)
if args.remove_index: if args.remove_index:
index = os.path.join(root, 'index.html') index = os.path.join(root, 'index.html')