From e21a78324f9fffadd57c17f096ec24c4fd309461 Mon Sep 17 00:00:00 2001 From: RRA Date: Sun, 15 Sep 2019 14:29:16 +0200 Subject: [PATCH] distribusi now only removes indexes that are created by distribusi --- distribusi/cli.py | 8 ++++-- distribusi/distribusi.py | 55 +++++++++++++++++++++++-------------- distribusi/page_template.py | 3 +- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/distribusi/cli.py b/distribusi/cli.py index f8b49d6..4bc0305 100644 --- a/distribusi/cli.py +++ b/distribusi/cli.py @@ -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 diff --git a/distribusi/distribusi.py b/distribusi/distribusi.py index ae314e9..5292f1a 100644 --- a/distribusi/distribusi.py +++ b/distribusi/distribusi.py @@ -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 '' 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): @@ -75,8 +86,6 @@ def distribusify(args, directory): # noqa if args.verbose: 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,28 +150,34 @@ def distribusify(args, directory): # noqa a = "{}/".replace('{}', name) html.append(div(args, 'dir', 'dir', a, 'folder')) - with open(os.path.join(root, 'index.html'), '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) + 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: + 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: index = os.path.join(root, 'index.html') if 'index.html' in files: - if args.verbose: - print('Removing index.html from', root) try: - os.remove(index) + if check_distribusi_index(args, index): + if args.verbose: + print('Removing index.html from', root) + os.remove(index) except Exception as e: print(e) diff --git a/distribusi/page_template.py b/distribusi/page_template.py index fd644e7..6ada8d3 100644 --- a/distribusi/page_template.py +++ b/distribusi/page_template.py @@ -3,7 +3,8 @@ html_head = """ - + +