diff --git a/_freeze.py b/_freeze.py index 54ec958..8b2bb94 100755 --- a/_freeze.py +++ b/_freeze.py @@ -79,8 +79,8 @@ def map_dir( src, dest ): #pylint: disable=missing-docstring args.extend( [ "--add-data", src + os.pathsep + dest ] ) map_dir( "vasl_templates/ui", "vasl_templates/ui" ) map_dir( "vasl_templates/resources", "vasl_templates/resources" ) -map_dir( "vasl_templates/webapp/static", "static" ) -map_dir( "vasl_templates/webapp/templates", "templates" ) +map_dir( "vasl_templates/webapp/static", "vasl_templates/webapp/static" ) +map_dir( "vasl_templates/webapp/templates", "vasl_templates/webapp/templates" ) if sys.platform == "win32": args.append( "--noconsole" ) args.extend( [ "--icon", APP_ICON ] ) diff --git a/setup.py b/setup.py index dd849b8..013a76d 100644 --- a/setup.py +++ b/setup.py @@ -21,6 +21,7 @@ setup( # nb: WebEngine seems to be broken in 5.10.1 :-/ "PyQT5==5.10.0", "pyyaml==3.13", + "pillow==5.3.0", "click==6.7", ], extras_require = { diff --git a/vasl_templates/webapp/__init__.py b/vasl_templates/webapp/__init__.py index 71337ef..baaa53e 100644 --- a/vasl_templates/webapp/__init__.py +++ b/vasl_templates/webapp/__init__.py @@ -29,15 +29,7 @@ def load_debug_config( fname ): # --------------------------------------------------------------------- # initialize Flask -if getattr( sys, "frozen", False ): - # NOTE: The support directories must have been set up by pyinstaller (via --add-data). - meipass = sys._MEIPASS #pylint: disable=no-member,protected-access - app = Flask( __name__, - template_folder = os.path.join( meipass, "templates" ), - static_folder = os.path.join( meipass, "static" ) - ) -else: - app = Flask( __name__ ) +app = Flask( __name__ ) # load the application configuration config_dir = os.path.join( BASE_DIR, "config" ) diff --git a/vasl_templates/webapp/data/default-template-pack/atmm.j2 b/vasl_templates/webapp/data/default-template-pack/atmm.j2 index 9098580..4a58873 100644 --- a/vasl_templates/webapp/data/default-template-pack/atmm.j2 +++ b/vasl_templates/webapp/data/default-template-pack/atmm.j2 @@ -16,7 +16,7 @@ td { margin: 0 ; padding: 0 ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}Anti-Tank Magnetic Mines + {%if PLAYER_FLAG%} {%endif%}Anti-Tank Magnetic Mines diff --git a/vasl_templates/webapp/data/default-template-pack/baz.j2 b/vasl_templates/webapp/data/default-template-pack/baz.j2 index a89f102..2b1db6b 100644 --- a/vasl_templates/webapp/data/default-template-pack/baz.j2 +++ b/vasl_templates/webapp/data/default-template-pack/baz.j2 @@ -18,7 +18,7 @@ td.r { text-align: right ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}Bazooka {%if BAZ_TYPE%} ('{{BAZ_TYPE}}) {%endif%} + {%if PLAYER_FLAG%} {%endif%}Bazooka {%if BAZ_TYPE%} ('{{BAZ_TYPE}}) {%endif%} diff --git a/vasl_templates/webapp/data/default-template-pack/mol-p.j2 b/vasl_templates/webapp/data/default-template-pack/mol-p.j2 index 7f2741a..2c30a98 100644 --- a/vasl_templates/webapp/data/default-template-pack/mol-p.j2 +++ b/vasl_templates/webapp/data/default-template-pack/mol-p.j2 @@ -19,7 +19,7 @@ ul { margin: 0 0 0 10px ; padding: 0 ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}MOL Projector + {%if PLAYER_FLAG%} {%endif%}MOL Projector diff --git a/vasl_templates/webapp/data/default-template-pack/mol.j2 b/vasl_templates/webapp/data/default-template-pack/mol.j2 index e99c770..7854db6 100644 --- a/vasl_templates/webapp/data/default-template-pack/mol.j2 +++ b/vasl_templates/webapp/data/default-template-pack/mol.j2 @@ -17,7 +17,7 @@ ul { margin: 0 0 0 10px ; padding: 0 ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}Molotov Cocktail + {%if PLAYER_FLAG%} {%endif%}Molotov Cocktail diff --git a/vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2 b/vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2 index 0632f9f..d390d79 100644 --- a/vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2 +++ b/vasl_templates/webapp/data/default-template-pack/ob_ordnance.j2 @@ -20,7 +20,7 @@ sup { font-size: 75% ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}{{PLAYER_NAME}} Ordnance + {%if PLAYER_FLAG%} {%endif%}{{PLAYER_NAME}} Ordnance {%for ord in OB_ORDNANCE%} diff --git a/vasl_templates/webapp/data/default-template-pack/ob_setup.j2 b/vasl_templates/webapp/data/default-template-pack/ob_setup.j2 index b80aa96..2990e5d 100644 --- a/vasl_templates/webapp/data/default-template-pack/ob_setup.j2 +++ b/vasl_templates/webapp/data/default-template-pack/ob_setup.j2 @@ -14,7 +14,7 @@ font-weight: bold ; {%if OB_SETUP_WIDTH%} width: {{OB_SETUP_WIDTH}} ; {%endif%} "> - {%if PLAYER_FLAG%} {%endif%}{{OB_SETUP}} + {%if PLAYER_FLAG%} {%endif%}{{OB_SETUP}} diff --git a/vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2 b/vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2 index 4163c9c..ab3eb98 100644 --- a/vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2 +++ b/vasl_templates/webapp/data/default-template-pack/ob_vehicles.j2 @@ -20,7 +20,7 @@ sup { font-size: 75% ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}{{PLAYER_NAME}} Vehicles + {%if PLAYER_FLAG%} {%endif%}{{PLAYER_NAME}} Vehicles {%for veh in OB_VEHICLES%} diff --git a/vasl_templates/webapp/data/default-template-pack/pf.j2 b/vasl_templates/webapp/data/default-template-pack/pf.j2 index ce3ac0b..4319337 100644 --- a/vasl_templates/webapp/data/default-template-pack/pf.j2 +++ b/vasl_templates/webapp/data/default-template-pack/pf.j2 @@ -18,7 +18,7 @@ td.r { text-align: right ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}Panzerfaust + {%if PLAYER_FLAG%} {%endif%}Panzerfaust diff --git a/vasl_templates/webapp/data/default-template-pack/piat.j2 b/vasl_templates/webapp/data/default-template-pack/piat.j2 index c494e5c..219bfb0 100644 --- a/vasl_templates/webapp/data/default-template-pack/piat.j2 +++ b/vasl_templates/webapp/data/default-template-pack/piat.j2 @@ -18,7 +18,7 @@ td.r { text-align: right ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}PIAT + {%if PLAYER_FLAG%} {%endif%}PIAT diff --git a/vasl_templates/webapp/data/default-template-pack/psk.j2 b/vasl_templates/webapp/data/default-template-pack/psk.j2 index c58b60f..d250d98 100644 --- a/vasl_templates/webapp/data/default-template-pack/psk.j2 +++ b/vasl_templates/webapp/data/default-template-pack/psk.j2 @@ -18,7 +18,7 @@ td.r { text-align: right ; } padding: 2px 5px ; font-weight: bold ; "> - {%if PLAYER_FLAG%} {%endif%}Panzerschrek + {%if PLAYER_FLAG%} {%endif%}Panzerschrek diff --git a/vasl_templates/webapp/snippets.py b/vasl_templates/webapp/snippets.py index e498d85..fde6b97 100644 --- a/vasl_templates/webapp/snippets.py +++ b/vasl_templates/webapp/snippets.py @@ -4,8 +4,10 @@ import os import json import re import zipfile +import io -from flask import jsonify, abort +from flask import request, jsonify, send_file, abort +from PIL import Image from vasl_templates.webapp import app from vasl_templates.webapp.config.constants import DATA_DIR @@ -108,6 +110,30 @@ def _do_get_template_pack( dname ): @app.route( "/flags/" ) def get_flag( nat ): """Get a flag image.""" + + # validate the nationality if not re.search( "^[-a-z]+$", nat ): abort( 404 ) - return app.send_static_file( "images/flags/{}.png".format( nat ) ) + + fname = "static/images/flags/{}.png".format( nat ) + with app.open_resource( fname, "rb" ) as fp: + + # load the image + img = Image.open( fp ) + + # check if we should resize the image + # NOTE: Resizing images in the HTML snippets looks dreadful (presumably + # because VASSAL's HTML engine is so ancient), so we do it ourself :-/ + height = request.args.get( "height" ) + if height: + height = int( height ) + if height > 0: + width = img.size[0] / ( float(img.size[1]) / height ) + width = int( width + 0.5 ) + img = img.resize( (width,height), Image.ANTIALIAS ) + + # return the image + buf = io.BytesIO() + img.save( buf, format="PNG" ) + buf.seek( 0 ) + return send_file( buf, mimetype="image/png" )