def update(self, new_input): """ Update multiple entries of a ``UserCommands`` dictionary ``update(new_dict)`` takes either a normal python ``dict`` object or a ``UserCommands`` object. Only keywords that match those in the ``UserCommands`` object will be updated. Misspelled keywords raise an error. To update single items in the dictionary, it is recommended to simply call the key and update the value - i.e ``<UserCommands>[key] = value``. Parameters ---------- new_input : str, dict, ``UserCommands``, file path Raises ------ KeyError If a parameter is not found in ``self.cmds``. Examples -------- View the default tests_commands:: >>> import scopesim >>> my_cmds = scopesim.UserCommands() >>> print(my_cmds.cmds) Change a single command:: >>> my_cmds["OBS_EXPTIME"] = 60 Change a series of tests_commands at once:: >>> new_cmds = {"OBS_EXPTIME" : 60 , "OBS_NDIT" : 10} >>> my_cmds.update(new_cmds) .. todo:: Add the new functionality to docs: - can update with strings, dicts, filenames, or UserCommands Remove documentation references to cmd.cmds. This bypasses: - check to see if key is official keyword - converting str to python type """ if isinstance(new_input, UserCommands): tmp_cmds = new_input.cmds elif isinstance(new_input, dict): tmp_cmds = new_input elif isinstance(new_input, str): tmp_cmds = cutils.read_config(new_input) else: raise ValueError("Cannot update with type: " + type(new_input)) for key in tmp_cmds: self[key] = tmp_cmds[key]
def test_passes_for_correctly_formatted_multiline_string(self): multiline_string = """ LINE_ONE hello # this is the first line LINE_TWO world! # this is the second line """ dic = cmd_utils.read_config(multiline_string) assert dic["LINE_ONE"] == "hello" assert dic["LINE_TWO"] == "world!"
def update(self, new_input): if isinstance(new_input, UserCommands): tmp_cmds = new_input.cmds elif isinstance(new_input, dict): tmp_cmds = new_input elif isinstance(new_input, str): fname = find_file(new_input) if fname is not None: new_input = fname tmp_cmds = cutils.read_config(new_input) else: raise ValueError("Cannot update with type: " + type(new_input)) # Use self.update so that we reject all the invalid keywords for key in tmp_cmds: self[key] = tmp_cmds[key]
def test_raise_exception_if_input_is_not_string(self): with pytest.raises(ValueError): cmd_utils.read_config(["hello", "world"])
def test_raises_exception_if_filename_doesnt_exist(self): with pytest.raises(ValueError): cmd_utils.read_config("bogus.txt")
def test_passes_when_given_filename_that_exist(self): rc_file = os.path.join(rc.__pkg_dir__, "OLD.scopesimrc") rc_dict = cmd_utils.read_config(rc_file) assert rc_dict["SIM_SUB_PIXEL_FLAG"] is False
def test_raises_exception_for_incorrectly_formatted_multiline_string(self): dodgy_multiline_string = """ LINE_ONE # this lines is missing a value """ with pytest.raises(ValueError): cmd_utils.read_config(dodgy_multiline_string)