def test_parse(self): conf = ConfigParser() conf.add("FIELD", False) schema_item = conf.add("RSH_HOST", False) self.assertIsInstance(schema_item, SchemaItem) test_path = self.createTestPath("local/config/simple_config") content = conf.parse(test_path, unrecognized=UnrecognizedEnum.CONFIG_UNRECOGNIZED_IGNORE) self.assertTrue(content.isValid()) content_item = content["RSH_HOST"] self.assertIsInstance(content_item, ContentItem) self.assertEqual(len(content_item), 1) with self.assertRaises(TypeError): content_item["BJARNE"] with self.assertRaises(IndexError): content_item[10] content_node = content_item[0] self.assertIsInstance(content_node, ContentNode) self.assertEqual(len(content_node), 2) self.assertEqual(content_node[1], "be-lx633214:2") self.assertEqual(content_node.content(sep=","), "be-lx655082:2,be-lx633214:2") self.assertEqual(content_node.content(), "be-lx655082:2 be-lx633214:2") content_item = content["FIELD"] self.assertEqual(len(content_item), 5) with self.assertRaises(IOError): conf.parse("DoesNotExits")
def test_parse_invalid(self): conf = ConfigParser() conf.add("INT", value_type=ContentTypeEnum.CONFIG_INT) with TestAreaContext("config/parse2"): with open("config","w") as fileH: fileH.write("INT xx\n") with self.assertRaises(ValueError): conf.parse("config") content = conf.parse("config", validate=False) self.assertFalse(content.isValid()) self.assertEqual(len(content.getErrors()), 1)
def test_parse(self): config_file = """ RSH_HOST some-hostname:2 other-hostname:2 FIELD PRESSURE DYNAMIC FIELD SWAT DYNAMIC MIN:0 MAX:1 FIELD SGAS DYNAMIC MIN:0 MAX:1 FIELD RS DYNAMIC MIN:0 FIELD RV DYNAMIC MIN:0.0034""" with open("simple_config", "w") as fout: fout.write(config_file) conf = ConfigParser() conf.add("FIELD", False) schema_item = conf.add("RSH_HOST", False) self.assertIsInstance(schema_item, SchemaItem) content = conf.parse( "simple_config", unrecognized=UnrecognizedEnum.CONFIG_UNRECOGNIZED_IGNORE) self.assertTrue(content.isValid()) content_item = content["RSH_HOST"] self.assertIsInstance(content_item, ContentItem) self.assertEqual(len(content_item), 1) with self.assertRaises(TypeError): content_item["BJARNE"] with self.assertRaises(IndexError): content_item[10] content_node = content_item[0] self.assertIsInstance(content_node, ContentNode) self.assertEqual(len(content_node), 2) self.assertEqual(content_node[1], "other-hostname:2") self.assertEqual(content_node.content(sep=","), "some-hostname:2,other-hostname:2") self.assertEqual(content_node.content(), "some-hostname:2 other-hostname:2") content_item = content["FIELD"] self.assertEqual(len(content_item), 5) with self.assertRaises(IOError): conf.parse("DoesNotExits")
def test_parse_deprecated(self): conf = ConfigParser() item = conf.add("INT", value_type=ContentTypeEnum.CONFIG_INT) msg = "ITEM INT IS DEPRECATED" item.setDeprecated(msg) with TestAreaContext("config/parse2"): with open("config","w") as fileH: fileH.write("INT 100\n") content = conf.parse("config" ) self.assertTrue(content.isValid()) warnings = content.getWarnings() self.assertEqual(len(warnings), 1) self.assertEqual(warnings[0], msg)
def test_item_types(self): with TestAreaContext("config/types") as test_area: with open("config", "w") as f: f.write("TYPE_ITEM 10 3.14 TruE String file\n") conf = ConfigParser() self.assertEqual(0, len(conf)) schema_item = conf.add("TYPE_ITEM", False) schema_item.iset_type(0, ContentTypeEnum.CONFIG_INT) schema_item.iset_type(1, ContentTypeEnum.CONFIG_FLOAT) schema_item.iset_type(2, ContentTypeEnum.CONFIG_BOOL) schema_item.iset_type(3, ContentTypeEnum.CONFIG_STRING) schema_item.iset_type(4, ContentTypeEnum.CONFIG_PATH) self.assertEqual(1, len(conf)) self.assertNotIn("TYPE_XX", conf) self.assertIn("TYPE_ITEM", conf) content = conf.parse("config") type_item = content["TYPE_ITEM"][0] int_value = type_item[0] self.assertEqual(int_value, 10) self.assertEqual(type_item.igetString(0), "10") float_value = type_item[1] self.assertEqual(float_value, 3.14) self.assertEqual(type_item.igetString(1), "3.14") bool_value = type_item[2] self.assertEqual(bool_value, True) self.assertEqual(type_item.igetString(2), "TruE") string_value = type_item[3] self.assertEqual(string_value, "String") self.assertEqual(type_item.igetString(3), "String") path_value = type_item[4] self.assertEqual(path_value, "file") self.assertEqual(type_item.igetString(4), "file") # test __getitem__ self.assertTrue(conf['TYPE_ITEM']) with self.assertRaises(KeyError): _ = conf['TYPE_XX'] self.assertIn('ConfigParser', repr(conf)) self.assertIn('size=1', repr(conf))
def test_parse_dotdot_relative(self): conf = ConfigParser() schema_item = conf.add("EXECUTABLE", False) schema_item.iset_type(0, ContentTypeEnum.CONFIG_PATH) with TestAreaContext("config/parse_dotdot"): os.makedirs("cwd/jobs") os.makedirs("eclipse/bin") script_path = os.path.join(os.getcwd(), "eclipse/bin/script.sh") with open(script_path, "w") as f: f.write("This is a test script") with open("cwd/jobs/JOB", "w") as fileH: fileH.write("EXECUTABLE ../../eclipse/bin/script.sh\n") os.makedirs("cwd/ert") os.chdir("cwd/ert") content = conf.parse("../jobs/JOB") item = content["EXECUTABLE"] node = item[0] self.assertEqual(script_path, node.getPath())
def test_parser_content(self): conf = ConfigParser() conf.add("KEY2", False) schema_item = conf.add("KEY", False) schema_item.iset_type(2, ContentTypeEnum.CONFIG_INT) schema_item.iset_type(3, ContentTypeEnum.CONFIG_BOOL) schema_item.iset_type(4, ContentTypeEnum.CONFIG_FLOAT) schema_item.iset_type(5, ContentTypeEnum.CONFIG_PATH) schema_item = conf.add("NOT_IN_CONTENT", False) with TestAreaContext("config/parse2"): with open("config","w") as fileH: fileH.write("KEY VALUE1 VALUE2 100 True 3.14 path/file.txt\n") cwd0 = os.getcwd() os.makedirs("tmp") os.chdir("tmp") content = conf.parse("../config") d = content.as_dict() self.assertTrue(content.isValid()) self.assertTrue("KEY" in content) self.assertFalse("NOKEY" in content) self.assertEqual( cwd0, content.get_config_path( )) keys = content.keys() self.assertEqual(len(keys), 1) self.assertIn("KEY", keys) d = content.as_dict() self.assertIn("KEY", d) item_list = d["KEY"] self.assertEqual(len(item_list), 1) l = item_list[0] self.assertEqual(l[0], "VALUE1") self.assertEqual(l[1], "VALUE2") self.assertEqual(l[2], 100) self.assertEqual(l[3], True) self.assertEqual(l[4], 3.14) self.assertEqual(l[5], "../path/file.txt") self.assertFalse("NOT_IN_CONTENT" in content) item = content["NOT_IN_CONTENT"] self.assertEqual(len(item), 0) with self.assertRaises(KeyError): content["Nokey"] item = content["KEY"] self.assertEqual(len(item), 1) line = item[0] with self.assertRaises(TypeError): line.getPath(4) with self.assertRaises(TypeError): line.getPath() rel_path = line.getPath(index=5, absolute=False) self.assertEqual(rel_path, "../path/file.txt") get = line[5] self.assertEqual(get, "../path/file.txt") abs_path = line.getPath(index=5) self.assertEqual(abs_path, os.path.join(cwd0, "path/file.txt")) rel_path = line.getPath(index=5, absolute=False, relative_start="../") self.assertEqual(rel_path, "path/file.txt") with self.assertRaises(IndexError): item[10] node = item[0] self.assertEqual(len(node), 6) with self.assertRaises(IndexError): node[6] self.assertEqual(node[0], "VALUE1") self.assertEqual(node[1], "VALUE2") self.assertEqual(node[2], 100) self.assertEqual(node[3], True) self.assertEqual(node[4], 3.14) self.assertEqual(content.getValue("KEY", 0, 1), "VALUE2") self.assertEqual(_iget(content, "KEY", 0, 1), "VALUE2") self.assertEqual(content.getValue("KEY", 0, 2), 100) self.assertEqual(_iget_as_int(content, "KEY", 0, 2), 100) self.assertEqual(content.getValue("KEY", 0, 3), True) self.assertEqual(_iget_as_bool(content, "KEY", 0, 3), True) self.assertEqual(content.getValue("KEY", 0, 4), 3.14) self.assertEqual(_iget_as_double(content, "KEY", 0, 4), 3.14) self.assertIsNone(_safe_iget(content, "KEY2", 0, 0)) self.assertEqual( _get_occurences(content, "KEY2"), 0) self.assertEqual( _get_occurences(content, "KEY"), 1) self.assertEqual( _get_occurences(content, "MISSING-KEY"), 0)
def parse(fname): """Takes a configuration object containing grid file path, restart file path and a list of wellpath-files and their restart dates. Returns a SnapWell object, containing this info. """ conf = ConfigParser() SnapConfig.addConfigItemPath(conf, 'GRID') SnapConfig.addConfigItemPath(conf, 'RESTART') SnapConfig.addConfigItemPath(conf, 'INIT') SnapConfig.addConfigItemPath(conf, 'OUTPUT') overwrite_item = conf.add("OVERWRITE", False) # the last OVERWRITE specified counts overwrite_item.iset_type(0, ContentTypeEnum.CONFIG_STRING) SnapConfig.addConfigItemFloat(conf, 'OWC_OFFSET') SnapConfig.addConfigItemFloat(conf, 'DELTA_Z') owc_def_item = conf.add( 'OWC_DEFINITION') # e.g. OWC_DEFINITION SWAT 0.7 owc_def_item.iset_type(0, ContentTypeEnum.CONFIG_STRING) owc_def_item.iset_type(1, ContentTypeEnum.CONFIG_FLOAT) log_item = conf.add('LOG') log_item.iset_type(0, ContentTypeEnum.CONFIG_STRING) wellpath_item = conf.add("WELLPATH", True) # a series of WELLPATH/DATE pairs wellpath_item.iset_type(0, ContentTypeEnum.CONFIG_PATH) wellpath_item.iset_type(1, ContentTypeEnum.CONFIG_STRING) content = conf.parse(fname) # Grid gridFile = tryGetPath(content, 'GRID', 0, 0) if not gridFile: logging.info('No GRID file specified?') raise ValueError( 'Could not load GRID file from Snapwell config file.') # Restart restartFile = tryGetPath(content, 'RESTART', 0, 0) if not restartFile: logging.info('No RESTART file specified?') raise ValueError( 'Could not load RESTART file from Snapwell config file.') # init initFile = None if "INIT" in content: initFile = tryGetPath(content, 'INIT', 0, 0) s = SnapConfig(gridFile, restartFile, initFile) # output outputPath = None if "OUTPUT" in content: outputPath = tryGetPath(content, 'OUTPUT', 0, 0) if path.exists(outputPath): if path.isfile(outputPath): raise ValueError( 'Provided output folder is a file. Either delete file, or set a different output folder: %s' % outputPath) else: s.setOutput(outputPath) else: makedirs(outputPath) s.setOutput(outputPath) # overwrite overwrite = False if 'OVERWRITE' in content: overwrite = tryGet(content, 'OVERWRITE', 0, 0) try: if overwrite.strip().lower() == 'true': overwrite = True except Exception as err: logging.warning('Ill specified overwrite flag: "%s".' % err) s.setOverwrite(overwrite) SnapConfig.tryset(s.setDeltaZ, 'DELTA_Z', content) SnapConfig.tryset(s.setOwcOffset, 'OWC_OFFSET', content) # Loading OWC_DEFINITION if 'OWC_DEFINITION' in content: try: if len(content['OWC_DEFINITION'][0]) != 2: logging.warning( 'Wrong number of arguments in OWC_DEFINITION. Needs to be e.g. SWAT 0.7, got %s' % str(content['OWC_DEFINITION'][0])) else: owc_kw = tryGet(content, 'OWC_DEFINITION', 0, 0) owc_val = tryGetFloat(content, 'OWC_DEFINITION', 0, 1) s.setOwcDefinition((owc_kw, owc_val)) except Exception as err: logging.warning('Ill specified OWC_DEFINITION keyword: "%s".' % err) else: logging.info('Using OWC definition %s' % str(s.owcDefinition())) if 'LOG' in content: for i in range(len(content['LOG'])): line = content['LOG'][i] num_tokens = len(line) if num_tokens < 1: raise ValueError('Missing data in LOG %d: "%s".' % (i + 1, str(line))) s.addLogKeyword(tryGet(content, 'LOG', i, 0)) # Loading the wellpath file names if 'WELLPATH' not in content: logging.warning('No wellpaths provided in Snapwell config file.') return s for i in range(len(content['WELLPATH'])): wp_line = content['WELLPATH'][i] num_tokens = len(wp_line) if num_tokens < 2: raise ValueError('Missing data in WELLPATH %d: "%s".' % (i + 1, str(wp_line))) fname = tryGetPath(content, 'WELLPATH', i, 0) date_str = tryGet(content, 'WELLPATH', i, 1) if num_tokens > 2 and num_tokens != 4: raise ValueError( 'WELLPATH format error. Need 2 or 4 tokens, got %d tokens: %s' % (num_tokens, str(wp_line))) depth_type = None depth = -1 if num_tokens == 4: depth_type = tryGet(content, 'WELLPATH', i, 2) depth = tryGetFloat(content, 'WELLPATH', i, 3) date = None try: date = parse_date(date_str) except ValueError as err: logging.info(str(err)) if date is None: raise ValueError( 'Could not read date from wellpath %d. Got date string "%s".' % (i + 1, str(date_str))) if depth_type: s.append((fname, date, depth_type, depth)) else: s.append((fname, date)) return s