distribusi now only removes indexes that are created by distribusi

This commit is contained in:
RRA 2019-09-15 14:29:16 +02:00
parent d5335f3212
commit e21a78324f
3 changed files with 40 additions and 22 deletions

View File

@ -49,21 +49,23 @@ def build_argparser():
parser.add_argument(
'-c',
'--captions',
help="Adds image captions based on EXIF metadata",
help="Adds image captions based on EXIF metadata, requires 'exiftool'",
action="store_true",
)
parser.add_argument(
'-r',
'--remove-index',
help="Removes previously made index.html. Warning: this will remove ALL index.html files in the directories listed",
help="Recursively removes all instances of index.html that have been previously made by distribusi",
action="store_true")
parser.add_argument(
'-e',
'--exclude-directory',
help="Exclude one or multiple directories from indexing",
nargs="*")
nargs="*",
metavar='DIR'
)
return parser

View File

@ -68,6 +68,17 @@ def div(args, type_, subtype, tag, name):
return html.format(id_name, subtype, tag)
def check_distribusi_index(args, index):
with open(index, 'r') as f:
if '<meta name="generator" content="distribusi" />' in f.read():
return True
else:
if args.verbose:
print(index, 'not generated by distribusi, skipping')
return False
def distribusify(args, directory): # noqa
for root, dirs, files in os.walk(directory):
@ -76,8 +87,6 @@ def distribusify(args, directory): # noqa
print('Excluding directory:', ", ".join(args.exclude_directory))
dirs[:] = [d for d in dirs if d not in args.exclude_directory]
if not args.remove_index:
html = []
@ -141,6 +150,11 @@ def distribusify(args, directory): # noqa
a = "<a href='{}'>{}/</a>".replace('{}', name)
html.append(div(args, 'dir', 'dir', a, 'folder'))
index = os.path.join(root, 'index.html')
if os.path.exists(index):
if check_distribusi_index(args, index):
pass
else:
with open(os.path.join(root, 'index.html'), 'w') as f:
if not args.no_template:
if args.style:
@ -160,9 +174,10 @@ def distribusify(args, directory): # noqa
if args.remove_index:
index = os.path.join(root, 'index.html')
if 'index.html' in files:
try:
if check_distribusi_index(args, index):
if args.verbose:
print('Removing index.html from', root)
try:
os.remove(index)
except Exception as e:
print(e)

View File

@ -3,6 +3,7 @@ html_head = """
<html lang="en">
<head>
<!-- Generated with distribusi https://git.vvvvvvaria.org/varia/distribusi -->
<meta name="generator" content="distribusi" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style>
.image{max-width: 100%%;}