def _handle_postprocess(self, value): # Run in the rsopt calling directory, not worker directories module_path, function_name = value module = pkrunpy.run_path_as_module(module_path) function = getattr(module, function_name) self._postprocess.append(function) return True
def __init__(self, script, lib_dir, user_filename, arguments, optics_func_name='set_optics'): self.lib_dir = lib_dir self.initial_lib_dir = lib_dir self.list_of_files = None self.optics_func_name = optics_func_name m = pkrunpy.run_path_as_module(script) varParam = getattr(m, 'varParam') if arguments: import shlex arguments = shlex.split(arguments) self.var_param = srwl_uti_parse_options(varParam, use_sys_argv=False, args=arguments) self.get_files() if self.initial_lib_dir: self.replace_mirror_files() self.replace_image_files() try: self.optics = getattr(m, self.optics_func_name)(self.var_param) except ValueError as e: if re.search('could not convert string to float', e.message): self.replace_mirror_files('mirror_2d.dat') self.optics = getattr(m, self.optics_func_name)(self.var_param) self.data = _parsed_dict(self.var_param, self.optics) self.data.models.simulation.name = _name(user_filename)
def test_run_path_as_module(): import sys from pykern import pkunit from pykern import pkrunpy m = pkrunpy.run_path_as_module(pkunit.data_dir().join('f1.py')) assert m.func1() == sys.modules, \ 'When imported, should be able to call function within module'
def get_objective_function(self): if len(self.objective_function) == 2: module_path, function = self.objective_function module = pkrunpy.run_path_as_module(module_path) function = getattr(module, function) else: function = None return function
def function(self): if self.setup.get('input_file'): # libEnsemble workers change active directory - sys.path will not record locally available modules sys.path.append('.') module = pkrunpy.run_path_as_module(self.setup['input_file']) function = getattr(module, self.setup['function']) return function return self.setup['function']
def _coalesce_values(): """Coalesce config files loaded from `cfg.load_path` Sets up load_path and channel then reads in base modules and home files. Finally imports os.environ. Returns: dict: nested values, top level is packages in load_path """ global _raw_values global cfg if _raw_values: return _raw_values #TODO(robnagler) sufficient to set package and rely on HOME_FILE? append_load_path(os.getenv(LOAD_PATH_ENV_NAME, LOAD_PATH_DEFAULT)) # Use current channel as the default in case called twice #TODO(robnagler) channel comes from file or environ #TODO(robnagler) import all modules then evaluate values # code may initialize channel or load path #TODO(robnagler) append_load_path needs to be allowed in modules so # reread path after each file/module load #TODO(robnagler) cache _values(), because need to be consistent channel = os.getenv(CHANNEL_ENV_NAME, CHANNEL_DEFAULT) assert channel in VALID_CHANNELS, \ '{}: invalid ${}; must be {}'.format( channel, CHANNEL_ENV_NAME, VALID_CHANNELS) values = {} for p in _load_path: try: # base_pkconfig used to be required, import if available m = importlib.import_module(BASE_MODULE.format(p)) _values_flatten(values, getattr(m, channel)()) except ImportError: pass for p in _load_path: fname = os.path.expanduser(HOME_FILE.format(p)) # The module itself may throw an exception so can't use try, because # interpretation of the exception doesn't make sense. It would be # better if run_path() returned a special exception when the file # does not exist. if os.path.isfile(fname): m = pkrunpy.run_path_as_module(fname) _values_flatten(values, getattr(m, channel)()) env = _clean_environ() _values_flatten(values, env) values[CHANNEL_ENV_NAME] = channel values[LOAD_PATH_ENV_NAME] = list(_load_path) _raw_values = values _init_parsed_values(env) cfg = init( _caller_module=sys.modules[__name__], load_path=Required(list, 'list of packages to configure'), channel=Required(str, 'which (stage) function returns config'), ) return _raw_values
def __init__(self, script, lib_dir, user_filename): self.lib_dir = lib_dir self.initial_lib_dir = lib_dir self.list_of_files = None m = pkrunpy.run_path_as_module(script) self.var_param = Struct(**list2dict(getattr(m, 'varParam'))) self.optics = getattr(m, 'set_optics')(self.var_param) self.get_files() if self.initial_lib_dir: self.replace_files() self.data = parsed_dict(self.var_param, self.optics) self.data['models']['simulation']['name'] = _name(user_filename)
def __init__(self, script, user_filename, arguments, optics_func_name='set_optics'): m = pkrunpy.run_path_as_module(script) if arguments: import shlex arguments = shlex.split(arguments) self.var_param = srwl_bl.srwl_uti_parse_options(m.varParam, use_sys_argv=False, args=arguments) self.replace_mirror_files() self.replace_image_files() try: self.optics = getattr(m, optics_func_name)(self.var_param) except ValueError as e: if re.search('could not convert string to float', str(e.args)): self.replace_mirror_files('mirror_2d.dat') self.optics = getattr(m, optics_func_name)(self.var_param) self.data = _parsed_dict(self.var_param, self.optics) self.data.models.simulation.name = _name(user_filename)
def __init__(self, script, lib_dir, user_filename, arguments, optics_func_name='set_optics'): self.lib_dir = lib_dir self.initial_lib_dir = lib_dir self.list_of_files = None self.optics_func_name = optics_func_name m = pkrunpy.run_path_as_module(script) varParam = getattr(m, 'varParam') if arguments: import shlex arguments = shlex.split(arguments) self.var_param = srwl_uti_parse_options(varParam, use_sys_argv=False, args=arguments) self.get_files() if self.initial_lib_dir: self.replace_mirror_files() self.replace_image_files() try: self.optics = getattr(m, self.optics_func_name)(self.var_param) except ValueError as e: if re.search('could not convert string to float', e.message): self.replace_mirror_files('mirror_2d.dat') self.optics = getattr(m, self.optics_func_name)(self.var_param) self.data = _parsed_dict(self.var_param, self.optics) self.data.models.simulation.name = _name(user_filename)
def get_file_def_module(self): module_path = os.path.join(self._BASE_RUN_PATH, self.setup['file_definitions']) module = pkrunpy.run_path_as_module(module_path) return module
def test_run_path_as_module(): m = pkrunpy.run_path_as_module(pkunit.data_dir().join('f1.py')) assert m.func1() == sys.modules, \ 'When imported, should be able to call function within module'