def _save_options(opt): if opt.output: out_path = Path(opt.output).parent out_path.mkdir(exist_ok=True, parents=True) save_options_to_config(str(out_path / formats.get_config_filename(__file__)), OrderedDict(sorted(opt.items())) )
def test_save_cli_options(): opt, unknown = paramtest_function( ["--name", "myname", "--int", "3", "--list", "4", "5", "6", "--other"]) with tempfile.TemporaryDirectory() as cwd: cfg_file = os.path.join(cwd, "config.ini") save_options_to_config(cfg_file, opt, unknown) opt_load, unknown_load = paramtest_function(entry_cfg=cfg_file) _assert_dicts(opt, opt_load) assert len(unknown_load) == 0
def test_save_and_load_cfg_with_none_explicit(tmp_path): opt, unknown = paramtest_function(name=None, int=None, list=None) cfg_file = tmp_path / "config.ini" save_options_to_config(cfg_file, opt, unknown) opt_load, unknown_load = paramtest_function(entry_cfg=cfg_file) _assert_dicts_equal(opt, opt_load) assert len(unknown) == 0 assert len(unknown_load) == 0 assert all([val is None for val in opt.values()])
def test_path_cfg(tmp_path): @entrypoint(EntryPointParameters(dict(path={'type': Path})), strict=True) def fun(opt): return opt cfg_file = tmp_path / "config.ini" opt = fun(path=tmp_path) save_options_to_config(cfg_file, opt) opt_load = fun(entry_cfg=cfg_file) _assert_dicts_equal(opt, opt_load)
def test_string_with_break_cfg(tmp_path): @entrypoint(EntryPointParameters(dict(name={'type': str})), strict=True) def fun(opt): return opt opt = fun(name="this is\nmystring") cfg_file = tmp_path / "config.ini" save_options_to_config(cfg_file, opt) opt_load = fun(entry_cfg=cfg_file) assert opt_load.name == opt.name
def test_save_and_cfg_load_with_none(tmp_path): # use cli_args in case we run in test-wrapper (e.g. pycharm) with cli_args(): # name, int, list = None opt, unknown = paramtest_function() cfg_file = tmp_path / "config.ini" save_options_to_config(cfg_file, opt, unknown) opt_load, unknown_load = paramtest_function(entry_cfg=cfg_file) _assert_dicts_equal(opt, opt_load) assert len(unknown) == 0 assert len(unknown_load) == 0 assert all([val is None for val in opt.values()])
def test_save_options(tmp_path): opt, unknown = paramtest_function( name="myname", int=3, list=[4, 5, 6], unknown="myfinalargument", unknoown=10, ) cfg_file = tmp_path / "config.ini" save_options_to_config(cfg_file, opt, unknown) opt_load, unknown_load = paramtest_function(entry_cfg=cfg_file) _assert_dicts_equal(opt, opt_load) _assert_dicts_equal(unknown, unknown_load)
def test_save_cli_options_cfg(tmp_path): opt, unknown = paramtest_function( ["--name", "myname", "--int", "3", "--list", "4", "5", "6", "--other"]) cfg_file = tmp_path / "config.ini" save_options_to_config(cfg_file, opt, unknown) opt_load, unknown_load = paramtest_function(entry_cfg=cfg_file) with open(cfg_file, 'r') as f: content = f.read() assert 'Unknown' in content assert '--other' in content _assert_dicts_equal(opt, opt_load) assert len(unknown_load) == 0
def save_config(output_dir: Path, opt: dict, script: str): """ Quick wrapper for save_options_to_config. Args: output_dir (Path): Path to the output directory (does not need to exist) opt (dict): opt-structure to be saved script (str): path/name of the invoking script (becomes name of the .ini) usually ``__file__`` """ output_dir.mkdir(parents=True, exist_ok=True) opt = remove_none_dict_entries(opt) # temporary fix (see docstring) opt = convert_paths_in_dict_to_strings(opt) save_options_to_config(output_dir / formats.get_config_filename(script), dict(sorted(opt.items())) )
def test_save_options(): opt, unknown = paramtest_function( name="myname", int=3, list=[4, 5, 6], unknown="myfinalargument", unknoown=10, ) with tempfile.TemporaryDirectory() as cwd: cfg_file = os.path.join(cwd, "config.ini") save_options_to_config(cfg_file, opt, unknown) opt_load, unknown_load = paramtest_function(entry_cfg=cfg_file) _assert_dicts(opt, opt_load) _assert_dicts(unknown, unknown_load)
def test_list_cfg(tmp_path): @entrypoint(EntryPointParameters(dict(lst={ 'type': int, 'nargs': "*" })), strict=True) def fun(opt): return opt cfg_file = tmp_path / "config.ini" opt = fun(lst=[1, 2, 3, 4]) save_options_to_config(cfg_file, opt) opt_load = fun(entry_cfg=cfg_file) _assert_dicts_equal(opt, opt_load)
def save_config(output_dir: Path, opt: dict, script: str): """ Quick wrapper for ``save_options_to_config``. Args: output_dir (Path): Path to the output directory (does not need to exist). opt (dict): opt-structure to be saved. script (str): path/name of the invoking script (becomes name of the .ini) usually ``__file__``. """ output_dir.mkdir(parents=True, exist_ok=True) opt = convert_paths_in_dict_to_strings(opt) opt = escape_percentage_signs(opt) time = datetime.utcnow().strftime(TIME) save_options_to_config(output_dir / f"{script:s}_{time:s}.ini", dict(sorted(opt.items())))
def converter_entrypoint(opt): """ Converts turn-by-turn files from various formats to ``LHC`` binary SDDS files. Optionally can replicate files with added noise. Converter Kwargs: - **files**: TbT files to convert Flags: **--files** Required: ``True`` - **outputdir**: Output directory. Flags: **--outputdir** Required: ``True`` - **tbt_datatype** *(str)*: Choose datatype from which to import (e.g LHC binary SDDS). Flags: **--tbt_datatype** Default: ``lhc`` - **realizations** *(int)*: Number of copies with added noise. Flags: **--realizations** Default: ``1`` - **noise_levels** *(float)*: Sigma of added Gaussian noise. Flags: **--noise_levels** Default: ``None`` - **use_average** *(bool)*: If set, returned sdds only contains the average over all particle/bunches. Flags: **--use_average** Default: ``False`` - **drop_elements**: Names of elements to drop from the input file during conversion. Flags: **--drop_elements** Default: ``None`` """ if opt.realizations < 1: raise ValueError("Number of realizations lower than 1.") iotools.create_dirs(opt.outputdir) save_options_to_config( str( Path(opt.outputdir) / DEFAULT_CONFIG_FILENAME.format( time=datetime.utcnow().strftime(formats.TIME))), dict(sorted(opt.items())), ) _read_and_write_files(opt)
def _write_config_file(harpy_opt, optics_opt, accelerator_opt): """Write the parsed options into a config file for later use.""" all_opt = OrderedDict() if harpy_opt is not None: all_opt["harpy"] = True all_opt.update(OrderedDict(sorted(harpy_opt.items()))) if optics_opt is not None: optics_opt = OrderedDict(sorted(optics_opt.items())) optics_opt.pop('accelerator') all_opt["optics"] = True all_opt.update(optics_opt) all_opt.update(sorted(accelerator_opt.items())) out_dir = all_opt["outputdir"] file_name = DEFAULT_CONFIG_FILENAME.format( time=datetime.utcnow().strftime(formats.TIME)) iotools.create_dirs(out_dir) save_options_to_config(os.path.join(out_dir, file_name), all_opt)
def converter_entrypoint(opt: DotDict) -> None: """ Looks for expected ``BetaBeat.src`` output files in the provided input directory, converts them to the format used in ``omc3`` and writes the converted files in the provided output directory. *--Required--* - **inputdir** *(str)*: Directory with BetaBeat.src output files. - **outputdir** *(str)*: Output directory for converted files. *--Optional--* - **suffix** *(str)*: AC dipole compensation suffix used in the provided BetaBeat.src output ('_free' for compensation by equation, '_free2' by model). choices: ``('', '_free', '_free2')`` default: ``_free`` """ iotools.create_dirs(Path(opt.outputdir)) save_options_to_config( Path(opt.outputdir) / DEFAULT_CONFIG_FILENAME.format( time=datetime.utcnow().strftime(formats.TIME)), OrderedDict(sorted(opt.items())), ) LOGGER.warning( "Be aware that the input units in the BetaBeat.src outputs may not be SI" ) convert_old_directory_to_new(opt)
def _save_options(opt: DotDict) -> None: if opt.output: os.makedirs(opt.output, exist_ok=True) save_options_to_config( Path(opt.output) / formats.get_config_filename(__file__), OrderedDict(sorted(opt.items())))
def _save_options(opt): if opt.output: os.makedirs(opt.output, exist_ok=True) save_options_to_config(os.path.join(opt.output, formats.get_config_filename(__file__)), OrderedDict(sorted(opt.items())) )
def _save_options_to_config(opt): os.makedirs(opt.output_dir, exist_ok=True) save_options_to_config(os.path.join(opt.output_dir, _get_ini_filename()), OrderedDict(sorted(opt.items())))
def _save_options_to_config(opt): with suppress(IOError): save_options_to_config(formats.get_config_filename(__file__), OrderedDict(sorted(opt.items())))