本文整理汇总了Python中qutebrowser.utils.qtutils.savefile_open函数的典型用法代码示例。如果您正苦于以下问题:Python savefile_open函数的具体用法?Python savefile_open怎么用?Python savefile_open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了savefile_open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save
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
示例2: save
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)
示例3: test_binary
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'
示例4: save
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)
示例5: test_utf8
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
示例6: save
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:])
示例7: save
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))
示例8: test_failing_commit
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() == []
示例9: test_failing_flush
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]
示例10: _save
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)
示例11: test_mock_exception
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()
示例12: save
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)
示例13: test_failing_flush
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]
示例14: test_existing_dir
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]
示例15: test_failing_commit
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]