def test_get_datastack_info_logfile_iui_style(self): """Datastack: test get datastack info logfile iui style.""" from natcap.invest import datastack logfile_path = os.path.join(self.workspace, 'logfile.txt') with open(logfile_path, 'w') as logfile: logfile.write( textwrap.dedent(""" Arguments: suffix foo some_int 1 some_float 2.33 workspace_dir some_workspace_dir some other logging here. """)) expected_args = { 'suffix': 'foo', 'some_int': 1, 'some_float': 2.33, 'workspace_dir': 'some_workspace_dir', } stack_type, stack_info = datastack.get_datastack_info(logfile_path) self.assertEqual(stack_type, 'logfile') self.assertEqual( stack_info, datastack.ParameterSet(expected_args, datastack.UNKNOWN, datastack.UNKNOWN))
def test_get_datatack_info_parameter_set(self): import natcap.invest from natcap.invest import datastack params = { 'a': 1, 'b': u'hello there', 'c': 'plain bytestring', 'd': '', } json_path = os.path.join(self.workspace, 'archive.invs.json') datastack.build_parameter_set(params, 'sample_model', json_path) stack_type, stack_info = datastack.get_datastack_info(json_path) self.assertEqual(stack_type, 'json') self.assertEqual(stack_info, datastack.ParameterSet( params, 'sample_model', natcap.invest.__version__))
def test_get_datastack_info_logfile_new_style(self): import natcap.invest from natcap.invest import datastack args = { 'a': 1, 'b': 2.7, 'c': [1, 2, 3.55], 'd': 'hello, world!', 'e': False, } logfile_path = os.path.join(self.workspace, 'logfile.txt') with open(logfile_path, 'w') as logfile: logfile.write(datastack.format_args_dict(args, 'some_modelname')) stack_type, stack_info = datastack.get_datastack_info(logfile_path) self.assertEqual(stack_type, 'logfile') self.assertEqual(stack_info, datastack.ParameterSet( args, 'some_modelname', natcap.invest.__version__))
def post_datastack_file(): """Extracts InVEST model args from json, logfiles, or datastacks. Body (JSON string): path to file Returns: A JSON string. """ filepath = request.get_json() stack_type, stack_info = datastack.get_datastack_info(filepath) model_name = PYNAME_TO_MODEL_NAME_MAP[stack_info.model_name] result_dict = { 'type': stack_type, 'args': stack_info.args, 'module_name': stack_info.model_name, 'model_run_name': model_name, 'model_human_name': MODEL_METADATA[model_name].model_title, 'invest_version': stack_info.invest_version } return json.dumps(result_dict)
def test_get_datastack_info_archive(self): """Datastacks: verify we can get info from an archive.""" import natcap.invest from natcap.invest import datastack params = { 'a': 1, 'b': u'hello there', 'c': 'plain bytestring', 'd': '', } archive_path = os.path.join(self.workspace, 'archive.invs.tar.gz') datastack.build_datastack_archive(params, 'sample_model', archive_path) stack_type, stack_info = datastack.get_datastack_info(archive_path) self.assertEqual(stack_type, 'archive') self.assertEqual(stack_info, datastack.ParameterSet( params, 'sample_model', natcap.invest.__version__))
def test_get_datastack_info_parameter_set(self): """Datastack: test get datastack info parameter set.""" import natcap.invest from natcap.invest import datastack params = { 'a': 1, 'b': 'hello there', 'c': 'plain bytestring', 'd': '', } test_module_name = 'test_datastack_modules.simple_parameters' json_path = os.path.join(self.workspace, 'archive.invs.json') datastack.build_parameter_set(params, test_module_name, json_path) stack_type, stack_info = datastack.get_datastack_info(json_path) self.assertEqual(stack_type, 'json') self.assertEqual( stack_info, datastack.ParameterSet(params, test_module_name, natcap.invest.__version__))