From 7687aa96a23e554b54bf3af2d084f45a6024131e Mon Sep 17 00:00:00 2001 From: lowrussia Date: Mon, 8 Jul 2019 15:24:22 +0200 Subject: [PATCH] added arguments for including exif metadata, including external css, not including filenames of pictures --- distribusi/cli.py | 22 +++++++++++++- distribusi/distribusi.py | 57 ++++++++++++++++++++++++++----------- distribusi/page_template.py | 7 +++-- 3 files changed, 66 insertions(+), 20 deletions(-) diff --git a/distribusi/cli.py b/distribusi/cli.py index 632ba25..8447da4 100644 --- a/distribusi/cli.py +++ b/distribusi/cli.py @@ -2,7 +2,6 @@ import argparse from distribusi.distribusi import distribusify - def build_argparser(): parser = argparse.ArgumentParser(""" distbusi is a content management system for the web that produces static @@ -19,6 +18,12 @@ def build_argparser(): help="Select which directory to distribute" ) + parser.add_argument( + '-s', + '--style', + help="Select a CSS style to include" + ) + parser.add_argument( '-v', '--verbose', @@ -40,12 +45,27 @@ def build_argparser(): action="store_true" ) + parser.add_argument( + '-nf', + '--no-filenames', + help="Don't use the template to ouput html", + action="store_true" + ) + + parser.add_argument( + '-c', + '--captions', + help="Print captions stored in exif metadata", + action="store_true" + ) + return parser def cli_entrypoint(): parser = build_argparser() args = parser.parse_args() + directory = '.' if args.directory: if args.verbose: diff --git a/distribusi/distribusi.py b/distribusi/distribusi.py index 4e2a2de..36b57a0 100644 --- a/distribusi/distribusi.py +++ b/distribusi/distribusi.py @@ -5,7 +5,7 @@ from io import BytesIO import magic from distribusi.page_template import html_footer, html_head from PIL import Image - +import subprocess CODE_TYPES = [ 'x-c', @@ -13,7 +13,7 @@ CODE_TYPES = [ ] FILE_TYPES = { - 'image': '', + 'image': '
{}
', 'pdf': ( '' '' @@ -32,8 +32,16 @@ FILE_TYPES = { MIME_TYPE = magic.Magic(mime=True) +def caption(image): + process = subprocess.Popen(['exiftool', '-Comment', image], stdout=subprocess.PIPE) + out, err = process.communicate() + try: + caption = out.decode("utf-8").split(": ", 1)[1] + except: + caption = '' + return caption -def thumbnail(image, name): +def thumbnail(image, name, args): size = (450, 450) im = Image.open(image) im.thumbnail(size) @@ -41,19 +49,25 @@ def thumbnail(image, name): im.save(output, format='JPEG') im_data = output.getvalue() data_url = base64.b64encode(im_data).decode() + cap = caption(image) + if cap and args.captions: + cap = "
{}
".format(cap) + else: + cap = '' return ( - "" - ).format(name, data_url) - + "
{}
" + ).format(name, data_url, cap) -def div(mime, tag, *values): +def div(args, mime, tag, *values): id_name = values[0].split('.')[0].replace(' ', '_') - + if not args.no_filenames: + filename = '
{}' + else: + filename = '' if 'image' in mime: - html = '
{}
{}
' + html = '
{}' + filename + '
' elif 'pdf' in mime: - html = '
{}
{}
' + html = '
{}' + filename + '
' else: html = '
{}
' @@ -92,9 +106,14 @@ def distribusify(args, directory): # noqa a = FILE_TYPES[mime] if mime == 'image' and args.thumbnail: - a = thumbnail(full_path, name) + a = thumbnail(full_path, name, args) else: - a = FILE_TYPES[mime] + cap = caption(full_path) + if cap and args.captions: + cap = "
{}
".format(cap) + else: + cap = '' + a = FILE_TYPES[mime].format(full_path, cap) if format in FILE_TYPES: a = FILE_TYPES[format] @@ -107,18 +126,24 @@ def distribusify(args, directory): # noqa print(message, mime, format, name) a = a.replace('{}', name) - html.append(div(mime, a, name)) + html.append(div(args, mime, a, name)) if root != directory: html.append('../') for name in dirs: a = "{}/".replace('{}', name) - html.append(div('dir', a, 'folder')) + html.append(div(args, 'dir', a, 'folder')) with open(os.path.join(root, 'index.html'), 'w') as f: if not args.no_template: - f.write(html_head) + if args.style: + fs = open(os.path.join(root, 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') diff --git a/distribusi/page_template.py b/distribusi/page_template.py index 2282ced..eb24d72 100644 --- a/distribusi/page_template.py +++ b/distribusi/page_template.py @@ -5,12 +5,13 @@ html_head = """ + %s + """