def assert_log_config_load(self, log_file, exp_log_file, log_level, exp_log_level, write_abs_path=False): with TestAreaContext("log_config_test") as work_area: work_area.copy_directory(self.case_directory) # Append config file with open(self.config_file, 'a') as cf: if log_file: if write_abs_path: log_file = os.path.join( os.path.abspath( os.path.split(self.config_file)[0]), log_file) cf.write("\nLOG_FILE %s\n" % log_file) if log_level: cf.write("\nLOG_LEVEL %s\n" % log_level) log_config = LogConfig(self.config_file) self.assertTrue(os.path.isabs(log_config.log_file)) self.assertEqual(os.path.normpath(log_config.log_file), os.path.normpath(os.path.abspath(exp_log_file))) self.assertEqual(log_config.log_level, exp_log_level)
def _alloc_from_dict(self, config_dict, throw_on_error=True): # treat the default config dir config_dir = os.getcwd() if ConfigKeys.CONFIG_DIRECTORY in config_dict: config_dir = config_dict[ConfigKeys.CONFIG_DIRECTORY] config_dict[ConfigKeys.CONFIG_DIRECTORY] = config_dir subst_config = SubstConfig(config_dict=config_dict) site_config = SiteConfig(config_dict=config_dict) rng_config = RNGConfig(config_dict=config_dict) analysis_config = AnalysisConfig(config_dict=config_dict) ecl_config = EclConfig(config_dict=config_dict) log_config = LogConfig(config_dict=config_dict) queue_config = QueueConfig(config_dict=config_dict) ert_workflow_list = ErtWorkflowList( subst_list=subst_config.subst_list, config_dict=config_dict ) hook_manager = HookManager( workflow_list=ert_workflow_list, config_dict=config_dict ) ert_templates = ErtTemplates( parent_subst=subst_config.subst_list, config_dict=config_dict ) ensemble_config = EnsembleConfig( grid=ecl_config.getGrid(), refcase=ecl_config.getRefcase(), config_dict=config_dict, ) model_config = ModelConfig( data_root=config_dir, joblist=site_config.get_installed_jobs(), last_history_restart=ecl_config.getLastHistoryRestart(), refcase=ecl_config.getRefcase(), config_dict=config_dict, ) return [ subst_config, site_config, rng_config, analysis_config, ert_workflow_list, hook_manager, ert_templates, ecl_config, ensemble_config, model_config, log_config, queue_config, ], config_dir
def _alloc_from_content(self, user_config_file=None, config=None, throw_on_error=True): if user_config_file is not None: # initialize configcontent if user_file provided parser = ConfigParser() config_content = self._alloc_config_content( user_config_file, parser) config_dir = config_content.getValue(ConfigKeys.CONFIG_DIRECTORY) else: config_dir = os.getcwd() config_content = self._build_config_content(config) if self.errors and throw_on_error: raise ValueError("Error loading configuration: " + str(self._errors)) subst_config = SubstConfig(config_content=config_content) site_config = SiteConfig(config_content=config_content) rng_config = RNGConfig(config_content=config_content) analysis_config = AnalysisConfig(config_content=config_content) ecl_config = EclConfig(config_content=config_content) log_config = LogConfig(config_content=config_content) queue_config = QueueConfig(config_content=config_content) ert_workflow_list = ErtWorkflowList(subst_list=subst_config.subst_list, config_content=config_content) hook_manager = HookManager(workflow_list=ert_workflow_list, config_content=config_content) ert_templates = ErtTemplates(parent_subst=subst_config.subst_list, config_content=config_content) ensemble_config = EnsembleConfig(config_content=config_content, grid=ecl_config.getGrid(), refcase=ecl_config.getRefcase()) model_config = ModelConfig( data_root=config_dir, joblist=site_config.get_installed_jobs(), last_history_restart=ecl_config.getLastHistoryRestart(), refcase=ecl_config.getRefcase(), config_content=config_content) return [ subst_config, site_config, rng_config, analysis_config, ert_workflow_list, hook_manager, ert_templates, ecl_config, ensemble_config, model_config, log_config, queue_config ], config_dir
def assert_log_config_load(self, log_file, exp_log_file, log_level, exp_log_level, write_abs_path=False): with TestAreaContext("log_config_test") as work_area: work_area.copy_directory(self.case_directory) # Append config file with open(self.config_file, 'a') as cf: if log_file: if write_abs_path: log_file = os.path.join( os.path.abspath( os.path.split(self.config_file)[0]), log_file) cf.write("\nLOG_FILE %s\n" % log_file) if log_level: level = log_level if sys.version_info[0] >= 3: if not log_level.isalpha(): level = int(float(level)) cf.write("\nLOG_LEVEL %s\n" % level) log_config = LogConfig(self.config_file) self.assertTrue(os.path.isabs(log_config.log_file)) self.assertEqual(os.path.normpath(log_config.log_file), os.path.normpath(os.path.abspath(exp_log_file))) if isinstance(log_config.log_level, int): level = MessageLevelEnum.to_enum(log_config.log_level) else: level = log_config.log_level self.assertEqual(level, exp_log_level)
def assert_log_config_load(self, log_file, exp_log_file, log_level, exp_log_level, write_abs_path=False): with TestAreaContext("log_config_test") as work_area: work_area.copy_directory(self.case_directory) config_dict = {} # Append config file with open(self.config_file, 'a') as cf: if log_file: config_dict[ConfigKeys.LOG_FILE] = os.path.realpath( os.path.join( os.path.abspath( os.path.split(self.config_file)[0]), log_file)) if write_abs_path: log_file = config_dict[ConfigKeys.LOG_FILE] cf.write("\nLOG_FILE %s\n" % log_file) else: config_dict[ConfigKeys.LOG_FILE] = os.path.realpath( os.path.join( os.path.abspath( os.path.split(self.config_file)[0]), "log.txt")) if log_level: level = log_level if sys.version_info[0] >= 3: if not log_level.isalpha(): level = int(float(level)) cf.write("\nLOG_LEVEL %s\n" % level) if type(level) is str and level.isdigit(): config_dict[ ConfigKeys.LOG_LEVEL] = MessageLevelEnum.to_enum( eval(level)) elif type(level) is str: config_dict[ConfigKeys. LOG_LEVEL] = MessageLevelEnum.from_string( "LOG_" + level) else: config_dict[ ConfigKeys.LOG_LEVEL] = MessageLevelEnum.to_enum( level) else: config_dict[ ConfigKeys.LOG_LEVEL] = MessageLevelEnum.LOG_WARNING log_config = LogConfig(user_config_file=self.config_file) res_config = ResConfig(self.config_file) log_config_dict = LogConfig(config_dict=config_dict) self.assertEqual(log_config, log_config_dict) self.assertEqual(log_config, res_config.log_config) self.assertTrue(os.path.isabs(log_config.log_file)) self.assertEqual(os.path.normpath(log_config.log_file), os.path.normpath(os.path.abspath(exp_log_file))) if isinstance(log_config.log_level, int): level = MessageLevelEnum.to_enum(log_config.log_level) else: level = log_config.log_level self.assertEqual(level, exp_log_level)
def __init__(self, user_config_file=None, config=None, throw_on_error=True): self._errors, self._failed_keys = None, None self._assert_input(user_config_file, config, throw_on_error) if config is not None: config_content = self._build_config_content(config) elif user_config_file is not None: parser = ConfigParser() config_content = self._alloc_config_content( user_config_file, parser) else: raise ValueError("No config provided") if self.errors and throw_on_error: raise ValueError("Error loading configuration: " + str(self._errors)) config_dir = config_content.getValue(ConfigKeys.CONFIG_DIRECTORY) subst_config = SubstConfig(config_content=config_content) site_config = SiteConfig(config_content=config_content) rng_config = RNGConfig(config_content=config_content) analysis_config = AnalysisConfig(config_content=config_content) ecl_config = EclConfig(config_content=config_content) log_config = LogConfig(config_content=config_content) queue_config = QueueConfig(config_content=config_content) ert_workflow_list = ErtWorkflowList( ert_workflow_list=subst_config.subst_list, config_content=config_content) hook_manager = HookManager(workflow_list=ert_workflow_list, config_content=config_content) ert_templates = ErtTemplates(parent_subst=subst_config.subst_list, config_content=config_content) ensemble_config = EnsembleConfig(config_content=config_content, grid=ecl_config.getGrid(), refcase=ecl_config.getRefcase()) model_config = ModelConfig( config_content=config_content, data_root=config_dir, joblist=site_config.get_installed_jobs(), last_history_restart=ecl_config.getLastHistoryRestart(), sched_file=ecl_config._get_sched_file(), refcase=ecl_config.getRefcase()) configs = [ subst_config, site_config, rng_config, analysis_config, ert_workflow_list, hook_manager, ert_templates, ecl_config, ensemble_config, model_config, log_config, queue_config ] c_ptr = None for conf in configs: conf.convertToCReference(None) c_ptr = self._alloc_full(config_dir, user_config_file, *configs) if c_ptr: super(ResConfig, self).__init__(c_ptr) else: raise ValueError( 'Failed to construct ResConfig instance from %r.' % (user_config_file if user_config_file else config))