def save(self, name, last_window=False, load_next_time=False): """Save a named session. Args: last_window: If set, saves the saved self._last_window_session instead of the currently open state. load_next_time: If set, prepares this session to be load next time. """ path = self._get_session_path(name) log.misc.debug("Saving session {} to {}...".format(name, path)) if last_window: data = self._last_window_session assert data is not None else: data = self._save_all() log.misc.vdebug("Saving data: {}".format(data)) try: with qtutils.savefile_open(path) as f: yaml.dump(data, f, Dumper=YamlDumper, default_flow_style=False, encoding='utf-8', allow_unicode=True) except (OSError, UnicodeEncodeError, yaml.YAMLError) as e: raise SessionError(e) else: self.update_completion.emit() if load_next_time: state_config = objreg.get('state-config') state_config['general']['session'] = name
def save(self): """Save the config file.""" if not os.path.exists(self._configdir): os.makedirs(self._configdir, 0o755) log.destroy.debug("Saving config to {}".format(self._configfile)) with qtutils.savefile_open(self._configfile) as f: self.write(f)
def test_binary(self, tmpdir): """Test with binary data.""" filename = tmpdir / 'foo' with qtutils.savefile_open(str(filename), binary=True) as f: f.write(b'\xde\xad\xbe\xef') assert tmpdir.listdir() == [filename] assert filename.read_binary() == b'\xde\xad\xbe\xef'
def _save(self) -> None: """Save the settings to the YAML file if they've changed.""" if not self._dirty: return settings = {} # type: _SettingsType for name, values in sorted(self._values.items()): if not values: continue settings[name] = {} for scoped in values: key = ('global' if scoped.pattern is None else str(scoped.pattern)) settings[name][key] = scoped.value data = {'config_version': self.VERSION, 'settings': settings} with qtutils.savefile_open(self._filename) as f: f.write( textwrap.dedent(""" # If a config.py file exists, this file is ignored unless it's explicitly loaded # via config.load_autoconfig(). For more information, see: # https://github.com/qutebrowser/qutebrowser/blob/master/doc/help/configuring.asciidoc#loading-autoconfigyml # DO NOT edit this file by hand, qutebrowser will overwrite it. # Instead, create a config.py - see :help for details. """.lstrip('\n'))) utils.yaml_dump(data, f) # type: ignore
def test_binary(self, tmp_path): """Test with binary data.""" filename = tmp_path / 'foo' with qtutils.savefile_open(str(filename), binary=True) as f: f.write(b'\xde\xad\xbe\xef') assert list(tmp_path.iterdir()) == [filename] assert filename.read_bytes() == b'\xde\xad\xbe\xef'
def save(self): """Save the config file.""" if self._configdir is None: return configfile = os.path.join(self._configdir, self._fname) log.destroy.debug("Saving config to {}".format(configfile)) with qtutils.savefile_open(configfile) as f: f.write(str(self))
def save(self): """Save the key config file.""" if self._configfile is None: return log.destroy.debug("Saving key config to {}".format(self._configfile)) with qtutils.savefile_open(self._configfile, encoding='utf-8') as f: data = str(self) f.write(data)
def test_utf8(self, data, tmp_path): """Test with UTF8 data.""" filename = tmp_path / 'foo' filename.write_text("Old data", encoding="utf-8") with qtutils.savefile_open(str(filename)) as f: f.write(data) assert list(tmp_path.iterdir()) == [filename] assert filename.read_text(encoding='utf-8') == data
def test_utf8(self, data, tmpdir): """Test with UTF8 data.""" filename = tmpdir / 'foo' filename.write("Old data") with qtutils.savefile_open(str(filename)) as f: f.write(data) assert tmpdir.listdir() == [filename] assert filename.read_text(encoding='utf-8') == data
def save(self): """Save the config file.""" limit = config.get(*self._limit) if limit == 0: return self._prepare_save() with qtutils.savefile_open(self._configfile, self._binary) as f: self._write(f, self.data[-limit:])
def test_failing_flush(self, tmpdir): """Test with the file being closed before flushing.""" filename = tmpdir / 'foo' with pytest.raises(ValueError, match="IO operation on closed device!"): with qtutils.savefile_open(str(filename), binary=True) as f: f.write(b'Hello') f.dev.commit() # provoke failing flush assert tmpdir.listdir() == [filename]
def test_failing_commit(self, tmpdir): """Test with the file being closed before committing.""" filename = tmpdir / 'foo' with pytest.raises(OSError, match='Commit failed!'): with qtutils.savefile_open(str(filename), binary=True) as f: f.write(b'Hello') f.dev.cancelWriting() # provoke failing commit assert tmpdir.listdir() == []
def test_mock_exception(self, qsavefile_mock): """Test with a mock and an exception in the block.""" qsavefile_mock.open.return_value = True with pytest.raises(SavefileTestException): with qtutils.savefile_open('filename'): raise SavefileTestException qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly) qsavefile_mock.cancelWriting.assert_called_once_with()
def test_exception(self, tmpdir): """Test with an exception in the block.""" filename = tmpdir / 'foo' filename.write("Old content") with pytest.raises(SavefileTestException): with qtutils.savefile_open(str(filename)) as f: f.write("Hello World!") raise SavefileTestException assert tmpdir.listdir() == [filename] assert filename.read_text(encoding='utf-8') == "Old content"
def test_failing_commit(self, tmpdir): """Test with the file being closed before committing.""" filename = tmpdir / 'foo' with pytest.raises(OSError) as excinfo: with qtutils.savefile_open(str(filename), binary=True) as f: f.write(b'Hello') f.dev.commit() # provoke failing "real" commit assert str(excinfo.value) == "Commit failed!" assert tmpdir.listdir() == [filename]
def test_failing_commit(self, tmpdir): """Test with the file being closed before comitting.""" filename = tmpdir / 'foo' with pytest.raises(OSError) as excinfo: with qtutils.savefile_open(str(filename), binary=True) as f: f.write(b'Hello') f.dev.commit() # provoke failing "real" commit assert str(excinfo.value) == "Commit failed!" assert tmpdir.listdir() == [filename]
def test_failing_flush(self, tmpdir): """Test with the file being closed before flushing.""" filename = tmpdir / "foo" with pytest.raises(ValueError) as excinfo: with qtutils.savefile_open(str(filename), binary=True) as f: f.write(b"Hello") f.dev.commit() # provoke failing flush assert str(excinfo.value) == "IO operation on closed device!" assert tmpdir.listdir() == [filename]
def test_existing_dir(self, tmpdir): """Test with the filename already occupied by a directory.""" filename = tmpdir / "foo" filename.mkdir() with pytest.raises(OSError) as excinfo: with qtutils.savefile_open(str(filename)): pass errors = ["Filename refers to a directory", "Commit failed!"] # Qt >= 5.4 # older Qt versions assert str(excinfo.value) in errors assert tmpdir.listdir() == [filename]
def _save(self): """Save the changed settings to the YAML file.""" data = {'config_version': self.VERSION, 'global': self.values} with qtutils.savefile_open(self._filename) as f: f.write(textwrap.dedent(""" # DO NOT edit this file by hand, qutebrowser will overwrite it. # Instead, create a config.py - see :help for details. """.lstrip('\n'))) utils.yaml_dump(data, f)
def save(self): """Save the config file.""" limit = -1 if self._limit is None else config.get(*self._limit) if limit == 0: return if not os.path.exists(self._configdir): os.makedirs(self._configdir, 0o755) log.destroy.debug("Saving config to {}".format(self._configfile)) with qtutils.savefile_open(self._configfile, self._binary) as f: self.write(f, limit)
def save(self): """Save the key config file.""" log.destroy.debug("Saving key config to {}".format(self._configfile)) try: with qtutils.savefile_open(self._configfile, encoding='utf-8') as f: data = str(self) f.write(data) except OSError as e: message.error("Could not save key config: {}".format(e))
def save(self): """Save the config file.""" limit = config.get(*self._limit) if limit == 0: return do_save = self._prepare_save() if not do_save: return assert self._configfile is not None with qtutils.savefile_open(self._configfile, self._binary) as f: self._write(f, self.data[-limit:])
def test_mock_open_error(self, qsavefile_mock): """Test with a mock and a failing open().""" qsavefile_mock.open.return_value = False qsavefile_mock.errorString.return_value = "Hello World" with pytest.raises(OSError, match="Hello World"): with qtutils.savefile_open('filename'): pass qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly) qsavefile_mock.cancelWriting.assert_called_once_with()
def test_existing_dir(self, tmpdir): """Test with the filename already occupied by a directory.""" filename = tmpdir / 'foo' filename.mkdir() with pytest.raises(OSError) as excinfo: with qtutils.savefile_open(str(filename)): pass msg = "Filename refers to a directory: {!r}".format(str(filename)) assert str(excinfo.value) == msg assert tmpdir.listdir() == [filename]
def test_existing_dir(self, tmpdir): """Test with the filename already occupied by a directory.""" filename = tmpdir / 'foo' filename.mkdir() with pytest.raises(OSError) as excinfo: with qtutils.savefile_open(str(filename)): pass errors = ["Filename refers to a directory", # Qt >= 5.4 "Commit failed!"] # older Qt versions assert str(excinfo.value) in errors assert tmpdir.listdir() == [filename]
def test_mock_commit_failed(self, qsavefile_mock): """Test with a mock and an exception in the block.""" qsavefile_mock.open.return_value = True qsavefile_mock.commit.return_value = False with pytest.raises(OSError, match="Commit failed!"): with qtutils.savefile_open('filename'): pass qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly) assert not qsavefile_mock.cancelWriting.called assert not qsavefile_mock.errorString.called
def test_line_endings(self, tmpdir): """Make sure line endings are translated correctly. See https://github.com/qutebrowser/qutebrowser/issues/309 """ filename = tmpdir / 'foo' with qtutils.savefile_open(str(filename)) as f: f.write('foo\nbar\nbaz') data = filename.read_binary() if os.name == 'nt': assert data == b'foo\r\nbar\r\nbaz' else: assert data == b'foo\nbar\nbaz'
def test_line_endings(self, tmpdir): """Make sure line endings are translated correctly. See https://github.com/The-Compiler/qutebrowser/issues/309 """ filename = tmpdir / 'foo' with qtutils.savefile_open(str(filename)) as f: f.write('foo\nbar\nbaz') data = filename.read_binary() if os.name == 'nt': assert data == b'foo\r\nbar\r\nbaz' else: assert data == b'foo\nbar\nbaz'
def save(self): """Save the config file.""" limit = config.instance.get(self._limit) if limit == 0: return do_save = self._prepare_save() if not do_save: return if self._configfile is None: raise AssertionError with qtutils.savefile_open(self._configfile, self._binary) as f: self._write(f, self.data[-limit:]) self._after_save()
def save(self): """Save the config file.""" if self._opened: raise IOError("Refusing to double-open AppendLineParser.") do_save = self._prepare_save() if not do_save: return self._opened = True try: assert self._configfile is not None with qtutils.savefile_open(self._configfile, self._binary) as f: self._write(f, self.data) finally: self._opened = False
def test_mock_successful(self, qsavefile_mock): """Test with a mock and a successful write.""" qsavefile_mock.open.return_value = True qsavefile_mock.errorString.return_value = "Hello World" qsavefile_mock.commit.return_value = True qsavefile_mock.write.side_effect = len qsavefile_mock.isOpen.return_value = True with qtutils.savefile_open('filename') as f: f.write("Hello World") qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly) assert not qsavefile_mock.cancelWriting.called qsavefile_mock.write.assert_called_once_with(b"Hello World")
def save(self, name, last_window=False, load_next_time=False, only_window=None, with_private=False): """Save a named session. Args: name: The name of the session to save, or the 'default' sentinel object. last_window: If set, saves the saved self._last_window_session instead of the currently open state. load_next_time: If set, prepares this session to be load next time. only_window: If set, only tabs in the specified window is saved. with_private: Include private windows. Return: The name of the saved session. """ name = self._get_session_name(name) path = self._get_session_path(name) log.sessions.debug("Saving session {} to {}...".format(name, path)) if last_window: data = self._last_window_session if data is None: log.sessions.error("last_window_session is None while saving!") return else: data = self._save_all(only_window=only_window, with_private=with_private) log.sessions.vdebug("Saving data: {}".format(data)) try: with qtutils.savefile_open(path) as f: yaml.dump(data, f, Dumper=YamlDumper, default_flow_style=False, encoding='utf-8', allow_unicode=True) except (OSError, UnicodeEncodeError, yaml.YAMLError) as e: raise SessionError(e) else: self.update_completion.emit() if load_next_time: state_config = objreg.get('state-config') state_config['general']['session'] = name return name
def save(self, name, last_window=False, load_next_time=False): """Save a named session. Args: name: The name of the session to save, or the 'default' sentinel object. last_window: If set, saves the saved self._last_window_session instead of the currently open state. load_next_time: If set, prepares this session to be load next time. Return: The name of the saved session. """ if name is default: name = config.get('general', 'session-default-name') if name is None: if self._current is not None: name = self._current else: name = 'default' path = self._get_session_path(name) if path is None: raise SessionError("No data storage configured.") log.sessions.debug("Saving session {} to {}...".format(name, path)) if last_window: data = self._last_window_session assert data is not None else: data = self._save_all() log.sessions.vdebug("Saving data: {}".format(data)) try: with qtutils.savefile_open(path) as f: yaml.dump(data, f, Dumper=YamlDumper, default_flow_style=False, encoding='utf-8', allow_unicode=True) except (OSError, UnicodeEncodeError, yaml.YAMLError) as e: raise SessionError(e) else: self.update_completion.emit() if load_next_time: state_config = objreg.get('state-config') state_config['general']['session'] = name return name
def save(self, name, last_window=False, load_next_time=False, only_window=None, with_private=False): """Save a named session. Args: name: The name of the session to save, or the 'default' sentinel object. last_window: If set, saves the saved self._last_window_session instead of the currently open state. load_next_time: If set, prepares this session to be load next time. only_window: If set, only tabs in the specified window is saved. with_private: Include private windows. Return: The name of the saved session. """ name = self._get_session_name(name) path = self._get_session_path(name) log.sessions.debug("Saving session {} to {}...".format(name, path)) if last_window: data = self._last_window_session if data is None: log.sessions.error("last_window_session is None while saving!") return None else: data = self._save_all(only_window=only_window, with_private=with_private) log.sessions.vdebug( # type: ignore[attr-defined] "Saving data: {}".format(data)) try: with qtutils.savefile_open(path) as f: utils.yaml_dump(data, f) except (OSError, UnicodeEncodeError, yaml.YAMLError) as e: raise SessionError(e) if load_next_time: configfiles.state['general']['session'] = name return name
def save(self, name, last_window=False, load_next_time=False): """Save a named session. Args: name: The name of the session to save, or the 'default' sentinel object. last_window: If set, saves the saved self._last_window_session instead of the currently open state. load_next_time: If set, prepares this session to be load next time. Return: The name of the saved session. """ if name is default: name = config.get('general', 'session-default-name') if name is None: if self._current is not None: name = self._current else: name = 'default' path = self._get_session_path(name) log.sessions.debug("Saving session {} to {}...".format(name, path)) if last_window: data = self._last_window_session assert data is not None else: data = self._save_all() log.sessions.vdebug("Saving data: {}".format(data)) try: with qtutils.savefile_open(path) as f: yaml.dump(data, f, Dumper=YamlDumper, default_flow_style=False, encoding='utf-8', allow_unicode=True) except (OSError, UnicodeEncodeError, yaml.YAMLError) as e: raise SessionError(e) else: self.update_completion.emit() if load_next_time: state_config = objreg.get('state-config') state_config['general']['session'] = name return name
def _save(self) -> None: """Save the settings to the YAML file if they've changed.""" if not self._dirty: return settings = {} # type: YamlConfig._SettingsType for name, values in sorted(self._values.items()): if not values: continue settings[name] = {} for scoped in values: key = ('global' if scoped.pattern is None else str(scoped.pattern)) settings[name][key] = scoped.value data = {'config_version': self.VERSION, 'settings': settings} with qtutils.savefile_open(self._filename) as f: f.write(textwrap.dedent(""" # DO NOT edit this file by hand, qutebrowser will overwrite it. # Instead, create a config.py - see :help for details. """.lstrip('\n'))) utils.yaml_dump(data, f)
def save(self): """Save the config file.""" self._prepare_save() with qtutils.savefile_open(self._configfile, self._binary) as f: self._write(f, self.data)