def test(expect, old_value=None): if isinstance(expect, Exception): exc = type(expect) exc_val = expect elif expect == old_value: exc = IOError exc_val = IOError("fail") else: exc = exc_val = None with tempdir() as tmp: path = os.path.join(tmp, "file.txt") if old_value is not None: with open(path, "x") as fh: fh.write(old_value) with assert_raises(exc, msg=exc_val): with mod.atomicfile(path) as fh: fh.write("new") if exc_val is not None: raise exc_val files = os.listdir(tmp) if exc is None: with open(path) as fh: eq_(fh.read(), expect) assert files == ["file.txt"], files elif old_value is not None: assert files == ["file.txt"], files with open(path) as fh: eq_(fh.read(), old_value) else: assert not files, files
def save_editor_state(self, editor, ident=None): """Save a single editor's state :param editor: The editor with state to be saved. :param ident: The identifier to use when saving editor state. It is assumed that the profile has been setup when this argument is provided; ``editor.id`` will be used when not provided. :returns: The name of the state file. """ if ident is None: raise NotImplementedError ident = editor.id self.setup_profile(editors=True) state_name = const.EDITOR_STATE.format(ident) state_file = os.path.join(self.profile_path, const.STATE_DIR, state_name) state = editor.state try: with atomicfile(state_file, encoding="utf-8") as fh: dump_yaml(state, fh) except Exception: log.error('cannot write %s\n%s\n', state_file, state, exc_info=True) return state_name
def save_window_state(self, window, state_dir, ident): """Save a single window's state :param window: The window with state to be saved. :param state_dir: The directory in which to write the state file. :param ident: The identifier to use when saving window state. :returns: The name of the state file. """ state_name = const.EDITOR_STATE.format(ident) state_file = os.path.join(state_dir, state_name) state = window.state try: with atomicfile(state_file, encoding="utf-8") as fh: dump_yaml(state, fh) except Exception: log.error('cannot write %s\n%s\n', state_file, state, exc_info=True) return state_name
def save_editor_state(self, editor, ident=None): """Save a single editor's state :param editor: The editor with state to be saved. :param ident: The identifier to use when saving editor state. It is assumed that the profile has been setup when this argument is provided; ``editor.id`` will be used when not provided. :returns: The name of the state file. """ if ident is None: raise NotImplementedError ident = editor.id self.setup_profile(editors=True) state_name = const.EDITOR_STATE.format(ident) state_file = os.path.join( self.profile_path, const.STATE_DIR, state_name) state = editor.state try: with atomicfile(state_file, encoding="utf-8") as fh: dump_yaml(state, fh) except Exception: log.error('cannot write %s\n%s\n', state_file, state, exc_info=True) return state_name