Exemplo n.º 1
0
 def iter_saved_editor_states(self):
     """Yield saved editor states"""
     state_path = os.path.join(self.profile_path, const.STATE_DIR)
     if not os.path.exists(state_path):
         if self.profile_path == os.path.expanduser(self.default_profile()):
             # TODO remove once all users have upraded
             for state in self._legacy_editor_states():
                 yield state
         return
     state_glob = os.path.join(state_path, const.EDITOR_STATE.format('*'))
     for path in sorted(glob.glob(state_glob)):
         try:
             with open(path) as f:
                 yield load_yaml(f)
         except Exception:
             log.error('cannot load %s', path, exc_info=True)
Exemplo n.º 2
0
 def iter_saved_editor_states(self):
     """Yield saved editor states"""
     state_path = os.path.join(self.profile_path, const.STATE_DIR)
     if not os.path.exists(state_path):
         if self.profile_path == os.path.expanduser(self.default_profile()):
             # TODO remove once all users have upraded
             for state in self._legacy_editor_states():
                 yield state
         return
     state_glob = os.path.join(state_path, const.EDITOR_STATE.format('*'))
     for path in sorted(glob.glob(state_glob)):
         try:
             with open(path) as f:
                 yield load_yaml(f)
         except Exception:
             log.error('cannot load %s', path, exc_info=True)
Exemplo n.º 3
0
 def iter_saved_window_states(self):
     """Yield saved window states"""
     state_dir = os.path.join(self.profile_path, const.STATE_DIR)
     if not os.path.exists(state_dir):
         if self.profile_path == os.path.expanduser(self.default_profile()):
             # TODO remove once all users have upgraded
             for state in self._legacy_window_states():
                 yield state
         return
     for x, path in enumerate_state_paths(state_dir):
         try:
             with open(path, encoding="utf-8") as f:
                 yield load_yaml(f)
         except Exception:
             log.error('cannot load %s', path, exc_info=True)
             yield StateLoadFailure(path)
Exemplo n.º 4
0
 def test(with_id=True, fail=False):
     with tempdir() as tmp:
         state_path = os.path.join(tmp, const.STATE_DIR)
         editor = TestConfig(state=[42], id=9)
         args = (editor.id,) if with_id else ()
         app = Application(tmp)
         state_name = app.save_editor_state(editor, *args)
         if fail:
             editor = editor(state="should not be written")
             def dump_fail(state, fh=None):
                 if fh is not None:
                     fh.write("should not be seen")
                 raise Exception("dump fail!")
             with replattr(mod, "dump_yaml", dump_fail, sigcheck=False):
                 state_name = app.save_editor_state(editor, *args)
         assert os.path.isdir(state_path), state_path
         with open(os.path.join(state_path, state_name)) as f:
             eq_(load_yaml(f), [42])
Exemplo n.º 5
0
 def create_window(self, state=None):
     from editxt.window import Window
     if state is None:
         state_dir = os.path.join(self.profile_path, const.STATE_DIR, const.CLOSED_DIR)
         for x, path in enumerate_state_paths(state_dir, reverse=True):
             try:
                 with open(path, encoding="utf-8") as f:
                     state = load_yaml(f)
             except Exception:
                 log.warn("cannot load editor state: %s", path)
                 continue
             try:
                 os.remove(path)
             except Exception:
                 log.exception('cannot remove %s', path)
             break
     window = Window(self, state)
     self.windows.append(window)
     window.show(self)
     return window
Exemplo n.º 6
0
    def test(with_id=True, fail=False):
        with tempdir() as tmp:
            state_path = os.path.join(tmp, const.STATE_DIR)
            editor = TestConfig(state=[42], id=9)
            args = (editor.id, ) if with_id else ()
            app = Application(tmp)
            state_name = app.save_editor_state(editor, *args)
            if fail:
                editor = editor(state="should not be written")

                def dump_fail(state, fh=None):
                    if fh is not None:
                        fh.write("should not be seen")
                    raise Exception("dump fail!")

                with replattr(mod, "dump_yaml", dump_fail, sigcheck=False):
                    state_name = app.save_editor_state(editor, *args)
            assert os.path.isdir(state_path), state_path
            with open(os.path.join(state_path, state_name)) as f:
                eq_(load_yaml(f), [42])
Exemplo n.º 7
0
 def reload(self):
     if exists(self.path):
         try:
             with open(self.path) as f:
                 data = load_yaml(f)
         except Exception as err:
             log.error("cannot load %s: %s", self.path, err)
             return
         else:
             if data is None:
                 data = {}
             elif not isinstance(data, dict):
                 log.error("cannot load %s: root object is a %s, "
                     "expected a dict", self.path, type(data).__name__)
                 return
         log.info("loaded config: %s", self.path)
         self.data = data
     else:
         self.data = {}
     self.valid = {}
     self.errors = {}
Exemplo n.º 8
0
 def reload(self):
     if self.path and exists(self.path):
         try:
             with open(self.path, encoding="utf-8") as f:
                 data = load_yaml(f)
         except Exception as err:
             log.error("cannot load %s: %s", self.path, err)
             return
         else:
             if data is None:
                 data = {}
             elif not isinstance(data, dict):
                 log.error("cannot load %s: root object is a %s, "
                     "expected a dict", self.path, type(data).__name__)
                 return
         self.transform_deprecations()
         log.info("loaded %s", self.path)
         self.data = data
     else:
         self.data = {}
     self.valid = {}
     self.errors = {}
Exemplo n.º 9
0
    def test(c):
        with tempdir() as tmp:
            state_path = os.path.join(tmp, const.STATE_DIR)
            if c.previous:
                # setup previous state
                m = Mocker()
                app = Application(tmp)
                mock_editors(m.method(app.iter_editors), c.previous)
                with m:
                    app.save_editor_states()
                assert os.listdir(state_path), state_path

            m = Mocker()
            app = Application(tmp)
            mock_editors(m.method(app.iter_editors), c.editors)
            with m:
                app.save_editor_states()
            assert os.path.isdir(state_path), state_path
            states = sorted(os.listdir(state_path))
            eq_(len(states), len(c.editors), states)
            for ident, state in zip(c.editors, states):
                with open(os.path.join(state_path, state)) as f:
                    eq_(load_yaml(f), [ident])
Exemplo n.º 10
0
 def reload(self):
     if exists(self.path):
         try:
             with open(self.path) as f:
                 data = load_yaml(f)
         except Exception as err:
             log.error("cannot load %s: %s", self.path, err)
             return
         else:
             if data is None:
                 data = {}
             elif not isinstance(data, dict):
                 log.error(
                     "cannot load %s: root object is a %s, "
                     "expected a dict", self.path,
                     type(data).__name__)
                 return
         log.info("loaded config: %s", self.path)
         self.data = data
     else:
         self.data = {}
     self.valid = {}
     self.errors = {}
Exemplo n.º 11
0
    def test(c):
        with tempdir() as tmp:
            state_path = os.path.join(tmp, const.STATE_DIR)
            if c.previous:
                # setup previous state
                m = Mocker()
                app = Application(tmp)
                mock_windows(m.method(app.iter_windows), c.previous)
                with m:
                    app.save_window_states()
                assert os.listdir(state_path), state_path

            m = Mocker()
            app = Application(tmp)
            mock_windows(m.method(app.iter_windows), c.windows)
            with m:
                app.save_window_states()
            assert os.path.isdir(state_path), state_path
            states = sorted(set(os.listdir(state_path)) - {const.CLOSED_DIR})
            eq_(len(states), len(c.windows), states)
            for ident, state in zip(c.windows, states):
                with open(os.path.join(state_path, state)) as f:
                    eq_(load_yaml(f), [ident])
Exemplo n.º 12
0
 def yaml(data):
     return mod.load_yaml(StringIO(data))