From 1e109546b65ca789e3e9d3ee800d626eb7a8d8fa Mon Sep 17 00:00:00 2001 From: Castro0o Date: Wed, 19 Feb 2020 08:59:01 +0100 Subject: [PATCH] uploading image with smw info box --- functions.py | 15 --------------- templates/smw_infobox_template.jinja | 12 ++++++++++++ upload_imgs_dir.py | 10 +++++++--- 3 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 templates/smw_infobox_template.jinja diff --git a/functions.py b/functions.py index 59f0dd5..35707d5 100644 --- a/functions.py +++ b/functions.py @@ -153,18 +153,3 @@ def reorder_imgs(dir, dry): if dry == False: os.replace(src_img, dst_img) return listimgs(dir) # update list w/ renamed imgs - -smw_propval_template = ''' -\{\{ImageMetadata -|Title={title} -|Date={date} -|Part={part} -|Partof={partof} -|Creator={creator} -|Organization={organization} -|Format={format} -|Event={event} -|Topic={topic} -\}\} -[[Template:ImageMetadata]] -''' diff --git a/templates/smw_infobox_template.jinja b/templates/smw_infobox_template.jinja new file mode 100644 index 0000000..ab8d340 --- /dev/null +++ b/templates/smw_infobox_template.jinja @@ -0,0 +1,12 @@ +{{ '{{' }}ImageMetadata +|Title={{ title }} +|Date={{ date }} +|Part={{ part }} +|Partof={{ partof }} +|Creator={{ creator }} +|Organization={{ organization }} +|Format={{ format }} +|Event={{ event }} +|Topic={{ topic }} +{{ '}}' }} +[[Template:ImageMetadata]] \ No newline at end of file diff --git a/upload_imgs_dir.py b/upload_imgs_dir.py index 2f0da3c..6c4d443 100644 --- a/upload_imgs_dir.py +++ b/upload_imgs_dir.py @@ -1,8 +1,8 @@ import os, argparse, sys, re from mwclient import Site +from jinja2 import Template from functions import (print_colormsg, - reorder_imgs, - smw_propval_template) + reorder_imgs) p = argparse.ArgumentParser(description='Upload files from a directory, with metadata values to the wiki.\n' 'Note that any VALUES CONTAINING ' @@ -64,6 +64,10 @@ elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0: print_colormsg(f'Error: --title "{args.title}" already exists in wiki. Provide a different one', level='fail') sys.exit() +# read template file +with open(os.path.join(wd, 'templates/smw_infobox_template.jinja')) as tmplt: + smw_propval_template = Template(tmplt.read()) + lsimgs = reorder_imgs(dir=args.dir, dry=args.dry) dirname = os.path.split(args.dir)[-1].replace(' ', '_') dirname = re.sub(r'[\W]', '', dirname) #remove non letters or digits @@ -80,7 +84,7 @@ for n, _file in enumerate(lsimgs): f'Already exists in {url} Will NOT be uploaded', level='warning') else: - img_smw_prop_val = smw_propval_template.format( + img_smw_prop_val = smw_propval_template.render( title=args.title, date=args.date, part=n + 1,