def get_invest_validate(): """Gets the return value of an InVEST model's validate function. Body (JSON string): model_module: string (e.g. natcap.invest.carbon) args: JSON string of InVEST model args keys and values Accepts a `language` query parameter which should be an ISO 639-1 language code. Validation messages will be translated to the requested language if translations are available, or fall back to English otherwise. Returns: A JSON string. """ payload = request.get_json() LOGGER.debug(payload) try: limit_to = payload['limit_to'] except KeyError: limit_to = None set_locale(request.args.get('language', 'en')) importlib.reload(natcap.invest.validation) model_module = importlib.reload( importlib.import_module(name=payload['model_module'])) results = model_module.validate(json.loads(payload['args']), limit_to=limit_to) LOGGER.debug(results) return json.dumps(results)
def test_translate_formatted_string(self): """Translation: test that f-string can be translated.""" set_locale(TEST_LANG) importlib.reload(validation) importlib.reload(carbon) args = {'n_workers': 'not a number'} validation_messages = carbon.validate(args) self.assertIn( TEST_MESSAGES[not_a_number_msg].format(value=args['n_workers']), str(validation_messages))
def reset_locale(): """Reset affected parts of the global context.""" set_locale('en') # "unimport" the modules being translated # NOTE: it would be better to run each test in a new process, # but that's difficult on windows: https://stackoverflow.com/a/48310939 importlib.reload(natcap.invest) importlib.reload(validation) importlib.reload(cli) importlib.reload(carbon) importlib.reload(ui_server)
def get_invest_models(): """Gets a list of available InVEST models. Accepts a `language` query parameter which should be an ISO 639-1 language code. Model names will be translated to the requested language if translations are available, or fall back to English otherwise. Returns: A JSON string """ LOGGER.debug('get model list') set_locale(request.args.get('language', 'en')) importlib.reload(natcap.invest) return cli.build_model_list_json()
def get_invest_getspec(): """Gets the ARGS_SPEC dict from an InVEST model. Body (JSON string): "carbon" Accepts a `language` query parameter which should be an ISO 639-1 language code. Spec 'about' and 'name' values will be translated to the requested language if translations are available, or fall back to English otherwise. Returns: A JSON string. """ set_locale(request.args.get('language', 'en')) target_model = request.get_json() target_module = MODEL_METADATA[target_model].pyname model_module = importlib.reload( importlib.import_module(name=target_module)) return spec_utils.serialize_args_spec(model_module.ARGS_SPEC)