diff --git a/vasl_templates/server_settings.py b/vasl_templates/server_settings.py index 009e31b..c9b9231 100644 --- a/vasl_templates/server_settings.py +++ b/vasl_templates/server_settings.py @@ -12,8 +12,8 @@ from vasl_templates.main import app_settings from vasl_templates.main_window import MainWindow from vasl_templates.utils import show_msg_store from vasl_templates.webapp.vassal import VassalShim, SUPPORTED_VASSAL_VERSIONS_DISPLAY +from vasl_templates.webapp.vasl_mod import set_vasl_mod, SUPPORTED_VASL_MOD_VERSIONS_DISPLAY from vasl_templates.webapp.utils import MsgStore -from vasl_templates.webapp.file_server.vasl_mod import set_vasl_mod, SUPPORTED_VASL_MOD_VERSIONS_DISPLAY # --------------------------------------------------------------------- diff --git a/vasl_templates/webapp/file_server/__init__.py b/vasl_templates/webapp/file_server/__init__.py deleted file mode 100644 index 6cffead..0000000 --- a/vasl_templates/webapp/file_server/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -""" Classes to serve files from various sources. - -This stuff has been split out into a separate package so that it can be imported into the asl-rulebook project, -since that will often be running while a game is being played, so that can provide these services, without having -to run this project as well. -""" diff --git a/vasl_templates/webapp/file_server/utils.py b/vasl_templates/webapp/file_server/utils.py deleted file mode 100644 index 31b7f03..0000000 --- a/vasl_templates/webapp/file_server/utils.py +++ /dev/null @@ -1,65 +0,0 @@ -""" Miscellaneous utilities. """ - -import os -import json - -# --------------------------------------------------------------------- - -def get_vo_gpids( data_dir, extns ): #pylint: disable=too-many-locals,too-many-branches - """Get the GPID's for the vehicles/ordnance.""" - - gpids = set() - for vo_type in ("vehicles","ordnance"): #pylint: disable=too-many-nested-blocks - - # process each file - dname = os.path.join( data_dir, vo_type ) - for root,_,fnames in os.walk(dname): - for fname in fnames: - if os.path.splitext(fname)[1] != ".json": - continue - - # load the GPID's from the next file - # NOTE: We originally assumed that GPID's are integers, but the main VASL build file started - # to have non-numeric values, as do, apparently, extensions :-/ For back-compat, we support both. - entries = json.load( open( os.path.join(root,fname), "r" ) ) - for entry in entries: - entry_gpids = entry[ "gpid" ] - if not isinstance( entry_gpids, list ): - entry_gpids = [ entry_gpids ] - for gpid in entry_gpids: - if gpid: - gpids.add( get_effective_gpid( str(gpid) ) ) - - # process any extensions - if extns: #pylint: disable=too-many-nested-blocks - for extn in extns: - extn_info = extn[1] - for nat in extn_info: - if not isinstance( extn_info[nat], dict ): - continue - for vo_type in ("vehicles","ordnance"): - for piece in extn_info[ nat ].get( vo_type, [] ): - if isinstance( piece["gpid"], list ): - gpids.update( piece["gpid"] ) - else: - gpids.add( piece["gpid"] ) - - return gpids - -# --------------------------------------------------------------------- - -# VASL 6.4.3 removed several PieceSlot's. There's no comment for the commmit (0a27c24) -# but I suspect it's because they're duplicates. Our data files have the following mappings: -# SdKfz 10/5: 7140, 2775 -# SdKfz 10/4: 7146, 2772 -# but we can't just remove the now-missing GPID's, since any scenarios that use them -# will break. This kind of thing is going to happen again, so we provide a generic mechanism -# for dealing with this kind of thing... -GPID_REMAPPINGS = { - "7140": "2775", # SdKfz 10/5 - "7146": "2772", # SdKfz 10/4 -} - -def get_effective_gpid( gpid ): - """Return the effective GPID.""" - return GPID_REMAPPINGS.get( gpid, gpid ) diff --git a/vasl_templates/webapp/files.py b/vasl_templates/webapp/files.py index 0e13fec..d06ad10 100644 --- a/vasl_templates/webapp/files.py +++ b/vasl_templates/webapp/files.py @@ -9,7 +9,7 @@ import mimetypes from flask import send_file, send_from_directory, jsonify, redirect, url_for, abort from vasl_templates.webapp import app -from vasl_templates.webapp.file_server.vasl_mod import get_vasl_mod +from vasl_templates.webapp.vasl_mod import get_vasl_mod from vasl_templates.webapp.utils import resize_image_response, is_empty_file # --------------------------------------------------------------------- diff --git a/vasl_templates/webapp/tests/remote.py b/vasl_templates/webapp/tests/remote.py index 5c03942..de17f65 100644 --- a/vasl_templates/webapp/tests/remote.py +++ b/vasl_templates/webapp/tests/remote.py @@ -22,9 +22,8 @@ from vasl_templates.webapp.config.constants import DATA_DIR from vasl_templates.webapp import main as webapp_main from vasl_templates.webapp import snippets as webapp_snippets from vasl_templates.webapp import vo_notes as webapp_vo_notes -from vasl_templates.webapp.file_server import utils as webapp_file_server_utils -from vasl_templates.webapp.file_server.vasl_mod import set_vasl_mod -from vasl_templates.webapp.file_server import vasl_mod as vasl_mod_module +from vasl_templates.webapp.vasl_mod import set_vasl_mod +from vasl_templates.webapp import vasl_mod as vasl_mod_module _logger = logging.getLogger( "control_tests" ) @@ -116,8 +115,8 @@ class ControlTests: gpids = json.loads( gpids.replace( "'", '"' ) ) gpids = { str(k): v for k,v in gpids.items() } _logger.info( "Setting GPID remappings: %s", gpids ) - prev_gpid_mappings = webapp_file_server_utils.GPID_REMAPPINGS - webapp_file_server_utils.GPID_REMAPPINGS = gpids + prev_gpid_mappings = vasl_mod_module.GPID_REMAPPINGS + vasl_mod_module.GPID_REMAPPINGS = gpids return prev_gpid_mappings def _get_vasl_mods( self ): @@ -181,7 +180,7 @@ class ControlTests: def _get_vasl_extns( self ): #pylint: disable=no-self-use """Return the loaded VASL extensions.""" - from vasl_templates.webapp.file_server.vasl_mod import get_vasl_mod + from vasl_templates.webapp.vasl_mod import get_vasl_mod extns = get_vasl_mod().get_extns() _logger.debug( "Returning VASL extensions:\n%s", "\n".join( "- {}".format( e ) for e in extns ) diff --git a/vasl_templates/webapp/tests/test_counters.py b/vasl_templates/webapp/tests/test_counters.py index 6e3e370..54e9e64 100644 --- a/vasl_templates/webapp/tests/test_counters.py +++ b/vasl_templates/webapp/tests/test_counters.py @@ -9,8 +9,7 @@ import urllib.request import pytest import tabulate -from vasl_templates.webapp.file_server.vasl_mod import VaslMod -from vasl_templates.webapp.file_server.utils import get_vo_gpids +from vasl_templates.webapp.vasl_mod import VaslMod, get_vo_gpids from vasl_templates.webapp.config.constants import DATA_DIR from vasl_templates.webapp.tests.utils import init_webapp, select_tab, find_child, find_children from vasl_templates.webapp.tests.test_scenario_persistence import load_scenario diff --git a/vasl_templates/webapp/file_server/vasl_mod.py b/vasl_templates/webapp/vasl_mod.py similarity index 86% rename from vasl_templates/webapp/file_server/vasl_mod.py rename to vasl_templates/webapp/vasl_mod.py index 2a6361f..551d572 100644 --- a/vasl_templates/webapp/file_server/vasl_mod.py +++ b/vasl_templates/webapp/vasl_mod.py @@ -13,7 +13,6 @@ _logger = logging.getLogger( "vasl_mod" ) from vasl_templates.webapp import app from vasl_templates.webapp.config.constants import DATA_DIR -from vasl_templates.webapp.file_server.utils import get_vo_gpids, get_effective_gpid SUPPORTED_VASL_MOD_VERSIONS = [ "6.4.0", "6.4.1", "6.4.2", "6.4.3" ] SUPPORTED_VASL_MOD_VERSIONS_DISPLAY = "6.4.0-6.4.3" @@ -406,3 +405,64 @@ class VaslMod: return None return val[0] if len(val) == 1 else val return delistify(front_images), delistify(back_images) + +# --------------------------------------------------------------------- + +def get_vo_gpids( data_dir, extns ): #pylint: disable=too-many-locals,too-many-branches + """Get the GPID's for the vehicles/ordnance.""" + + gpids = set() + for vo_type in ("vehicles","ordnance"): #pylint: disable=too-many-nested-blocks + + # process each file + dname = os.path.join( data_dir, vo_type ) + for root,_,fnames in os.walk(dname): + for fname in fnames: + if os.path.splitext(fname)[1] != ".json": + continue + + # load the GPID's from the next file + # NOTE: We originally assumed that GPID's are integers, but the main VASL build file started + # to have non-numeric values, as do, apparently, extensions :-/ For back-compat, we support both. + entries = json.load( open( os.path.join(root,fname), "r" ) ) + for entry in entries: + entry_gpids = entry[ "gpid" ] + if not isinstance( entry_gpids, list ): + entry_gpids = [ entry_gpids ] + for gpid in entry_gpids: + if gpid: + gpids.add( get_effective_gpid( str(gpid) ) ) + + # process any extensions + if extns: #pylint: disable=too-many-nested-blocks + for extn in extns: + extn_info = extn[1] + for nat in extn_info: + if not isinstance( extn_info[nat], dict ): + continue + for vo_type in ("vehicles","ordnance"): + for piece in extn_info[ nat ].get( vo_type, [] ): + if isinstance( piece["gpid"], list ): + gpids.update( piece["gpid"] ) + else: + gpids.add( piece["gpid"] ) + + return gpids + +# --------------------------------------------------------------------- + +# VASL 6.4.3 removed several PieceSlot's. There's no comment for the commmit (0a27c24) +# but I suspect it's because they're duplicates. Our data files have the following mappings: +# SdKfz 10/5: 7140, 2775 +# SdKfz 10/4: 7146, 2772 +# but we can't just remove the now-missing GPID's, since any scenarios that use them +# will break. This kind of thing is going to happen again, so we provide a generic mechanism +# for dealing with this kind of thing... +GPID_REMAPPINGS = { + "7140": "2775", # SdKfz 10/5 + "7146": "2772", # SdKfz 10/4 +} + +def get_effective_gpid( gpid ): + """Return the effective GPID.""" + return GPID_REMAPPINGS.get( gpid, gpid ) diff --git a/vasl_templates/webapp/vassal.py b/vasl_templates/webapp/vassal.py index 062bbbc..b093a82 100644 --- a/vasl_templates/webapp/vassal.py +++ b/vasl_templates/webapp/vassal.py @@ -16,7 +16,7 @@ from flask import request from vasl_templates.webapp import app from vasl_templates.webapp.config.constants import BASE_DIR, IS_FROZEN -from vasl_templates.webapp.file_server.vasl_mod import get_vasl_mod +from vasl_templates.webapp.vasl_mod import get_vasl_mod from vasl_templates.webapp.utils import TempFile, SimpleError from vasl_templates.webapp.webdriver import WebDriver diff --git a/vasl_templates/webapp/vo.py b/vasl_templates/webapp/vo.py index 94eaf60..cdce4bb 100644 --- a/vasl_templates/webapp/vo.py +++ b/vasl_templates/webapp/vo.py @@ -8,7 +8,7 @@ from flask import request, render_template, jsonify, abort from vasl_templates.webapp import app from vasl_templates.webapp.config.constants import DATA_DIR -from vasl_templates.webapp.file_server.vasl_mod import get_vasl_mod +from vasl_templates.webapp.vasl_mod import get_vasl_mod # --------------------------------------------------------------------- diff --git a/vasl_templates/webapp/vo_notes.py b/vasl_templates/webapp/vo_notes.py index 2954f0f..89293bb 100644 --- a/vasl_templates/webapp/vo_notes.py +++ b/vasl_templates/webapp/vo_notes.py @@ -9,7 +9,7 @@ from collections import defaultdict from flask import render_template, jsonify, abort from vasl_templates.webapp import app -from vasl_templates.webapp.file_server.vasl_mod import get_vasl_mod +from vasl_templates.webapp.vasl_mod import get_vasl_mod from vasl_templates.webapp.files import FileServer from vasl_templates.webapp.utils import resize_image_response, is_image_file, is_empty_file