Make editor config configurable
This commit is contained in:
parent
66a09f6993
commit
c826db7e03
1
doc/TODO
1
doc/TODO
@ -55,7 +55,6 @@ Downloads
|
|||||||
Improvements / minor features
|
Improvements / minor features
|
||||||
=============================
|
=============================
|
||||||
|
|
||||||
- Make editor encoding configurable
|
|
||||||
- Sane default for editor (maybe via QDesktopServices or so?)
|
- Sane default for editor (maybe via QDesktopServices or so?)
|
||||||
- We should have something like utils.debug.qenum_key for QFlags.
|
- We should have something like utils.debug.qenum_key for QFlags.
|
||||||
- Honour icognito mode for cookies etc.
|
- Honour icognito mode for cookies etc.
|
||||||
|
@ -194,6 +194,10 @@ DATA = OrderedDict([
|
|||||||
"Use `{}` for the filename. The value gets split like in a shell, so "
|
"Use `{}` for the filename. The value gets split like in a shell, so "
|
||||||
"you can use `\"` or `'` to quote arguments."),
|
"you can use `\"` or `'` to quote arguments."),
|
||||||
|
|
||||||
|
('editor-encoding',
|
||||||
|
SettingValue(types.Encoding(), 'utf-8'),
|
||||||
|
"Encoding to use for editor."),
|
||||||
|
|
||||||
('private-browsing',
|
('private-browsing',
|
||||||
SettingValue(types.Bool(), 'false'),
|
SettingValue(types.Bool(), 'false'),
|
||||||
"Do not record visited pages in the history or store web page "
|
"Do not record visited pages in the history or store web page "
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
|
import codecs
|
||||||
import os.path
|
import os.path
|
||||||
from sre_constants import error as RegexError
|
from sre_constants import error as RegexError
|
||||||
|
|
||||||
@ -1060,6 +1061,24 @@ class KeyBinding(Command):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Encoding(BaseType):
|
||||||
|
|
||||||
|
"""Setting for a python encoding."""
|
||||||
|
|
||||||
|
typestr = 'encoding'
|
||||||
|
|
||||||
|
def validate(self, value):
|
||||||
|
if not value:
|
||||||
|
if self.none_ok:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
raise ValidationError(value, "may not be empty!")
|
||||||
|
try:
|
||||||
|
codecs.lookup(value)
|
||||||
|
except LookupError:
|
||||||
|
raise ValidationError(value, "is not a valid encoding!")
|
||||||
|
|
||||||
|
|
||||||
class WebSettingsFile(File):
|
class WebSettingsFile(File):
|
||||||
|
|
||||||
"""QWebSettings file."""
|
"""QWebSettings file."""
|
||||||
|
@ -1826,7 +1826,7 @@ class AutoSearchTests(unittest.TestCase):
|
|||||||
self.assertIsNone(self.t.transform(''))
|
self.assertIsNone(self.t.transform(''))
|
||||||
|
|
||||||
|
|
||||||
class IgnoreCase(unittest.TestCase):
|
class IgnoreCaseTests(unittest.TestCase):
|
||||||
|
|
||||||
"""Test IgnoreCase."""
|
"""Test IgnoreCase."""
|
||||||
|
|
||||||
@ -1876,5 +1876,42 @@ class IgnoreCase(unittest.TestCase):
|
|||||||
self.assertIsNone(self.t.transform(''))
|
self.assertIsNone(self.t.transform(''))
|
||||||
|
|
||||||
|
|
||||||
|
class EncodingTests(unittest.TestCase):
|
||||||
|
|
||||||
|
"""Test Encoding."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.t = conftypes.Encoding()
|
||||||
|
|
||||||
|
def test_validate_empty(self):
|
||||||
|
"""Test validate with empty string and none_ok = False."""
|
||||||
|
with self.assertRaises(conftypes.ValidationError):
|
||||||
|
self.t.validate('')
|
||||||
|
|
||||||
|
def test_validate_empty_none_ok(self):
|
||||||
|
"""Test validate with empty string and none_ok = True."""
|
||||||
|
t = conftypes.Encoding(none_ok=True)
|
||||||
|
t.validate('')
|
||||||
|
|
||||||
|
def test_validate_valid(self):
|
||||||
|
"""Test validate with valid values."""
|
||||||
|
for val in ('utf-8', 'UTF-8', 'iso8859-1'):
|
||||||
|
with self.subTest(val=val):
|
||||||
|
self.t.validate(val)
|
||||||
|
|
||||||
|
def test_validate_invalid(self):
|
||||||
|
"""Test validate with an invalid value."""
|
||||||
|
with self.assertRaises(conftypes.ValidationError):
|
||||||
|
self.t.validate('blubber')
|
||||||
|
|
||||||
|
def test_transform(self):
|
||||||
|
"""Test if transform doesn't alter the value."""
|
||||||
|
self.assertEqual(self.t.transform('utf-8'), 'utf-8')
|
||||||
|
|
||||||
|
def test_transform_empty(self):
|
||||||
|
"""Test transform with none_ok = False and an empty value."""
|
||||||
|
self.assertIsNone(self.t.transform(''))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
@ -61,11 +61,11 @@ class ConfigStub:
|
|||||||
|
|
||||||
def get(self, sect, opt):
|
def get(self, sect, opt):
|
||||||
"""Get a value from the config."""
|
"""Get a value from the config."""
|
||||||
sect = self.data[sect]
|
data = self.data[sect]
|
||||||
try:
|
try:
|
||||||
return sect[opt]
|
return data[opt]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise self.NoOptionError
|
raise self.NoOptionError('{} -> {}'.format(sect, opt))
|
||||||
|
|
||||||
|
|
||||||
class FakeKeyEvent:
|
class FakeKeyEvent:
|
||||||
|
@ -56,21 +56,24 @@ class ArgTests(unittest.TestCase):
|
|||||||
|
|
||||||
def test_simple_start_args(self):
|
def test_simple_start_args(self):
|
||||||
"""Test starting editor without arguments."""
|
"""Test starting editor without arguments."""
|
||||||
editorutils.config = ConfigStub({'general': {'editor': ['bin']}})
|
editorutils.config = ConfigStub(
|
||||||
|
{'general': {'editor': ['bin'], 'editor-encoding': 'utf-8'}})
|
||||||
self.editor.edit("")
|
self.editor.edit("")
|
||||||
self.editor.proc.start.assert_called_with("bin", [])
|
self.editor.proc.start.assert_called_with("bin", [])
|
||||||
|
|
||||||
def test_start_args(self):
|
def test_start_args(self):
|
||||||
"""Test starting editor with static arguments."""
|
"""Test starting editor with static arguments."""
|
||||||
editorutils.config = ConfigStub(
|
editorutils.config = ConfigStub(
|
||||||
{'general': {'editor': ['bin', 'foo', 'bar']}})
|
{'general': {'editor': ['bin', 'foo', 'bar'],
|
||||||
|
'editor-encoding': 'utf-8'}})
|
||||||
self.editor.edit("")
|
self.editor.edit("")
|
||||||
self.editor.proc.start.assert_called_with("bin", ["foo", "bar"])
|
self.editor.proc.start.assert_called_with("bin", ["foo", "bar"])
|
||||||
|
|
||||||
def test_placeholder(self):
|
def test_placeholder(self):
|
||||||
"""Test starting editor with placeholder argument."""
|
"""Test starting editor with placeholder argument."""
|
||||||
editorutils.config = ConfigStub(
|
editorutils.config = ConfigStub(
|
||||||
{'general': {'editor': ['bin', 'foo', '{}', 'bar']}})
|
{'general': {'editor': ['bin', 'foo', '{}', 'bar'],
|
||||||
|
'editor-encoding': 'utf-8'}})
|
||||||
self.editor.edit("")
|
self.editor.edit("")
|
||||||
filename = self.editor.filename
|
filename = self.editor.filename
|
||||||
self.editor.proc.start.assert_called_with("bin",
|
self.editor.proc.start.assert_called_with("bin",
|
||||||
@ -79,7 +82,8 @@ class ArgTests(unittest.TestCase):
|
|||||||
def test_in_arg_placeholder(self):
|
def test_in_arg_placeholder(self):
|
||||||
"""Test starting editor with placeholder argument inside argument."""
|
"""Test starting editor with placeholder argument inside argument."""
|
||||||
editorutils.config = ConfigStub(
|
editorutils.config = ConfigStub(
|
||||||
{'general': {'editor': ['bin', 'foo{}bar']}})
|
{'general': {'editor': ['bin', 'foo{}bar'],
|
||||||
|
'editor-encoding': 'utf-8'}})
|
||||||
self.editor.edit("")
|
self.editor.edit("")
|
||||||
self.editor.proc.start.assert_called_with("bin", ["foo{}bar"])
|
self.editor.proc.start.assert_called_with("bin", ["foo{}bar"])
|
||||||
|
|
||||||
@ -97,7 +101,8 @@ class FileHandlingTests(unittest.TestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.editor = editorutils.ExternalEditor()
|
self.editor = editorutils.ExternalEditor()
|
||||||
editorutils.config = ConfigStub({'general': {'editor': ['']}})
|
editorutils.config = ConfigStub(
|
||||||
|
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}})
|
||||||
|
|
||||||
def test_file_handling_closed_ok(self):
|
def test_file_handling_closed_ok(self):
|
||||||
"""Test file handling when closing with an exitstatus == 0."""
|
"""Test file handling when closing with an exitstatus == 0."""
|
||||||
@ -136,7 +141,8 @@ class TextModifyTests(unittest.TestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.editor = editorutils.ExternalEditor()
|
self.editor = editorutils.ExternalEditor()
|
||||||
self.editor.editing_finished = Mock()
|
self.editor.editing_finished = Mock()
|
||||||
editorutils.config = ConfigStub({'general': {'editor': ['']}})
|
editorutils.config = ConfigStub(
|
||||||
|
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}})
|
||||||
|
|
||||||
def _write(self, text):
|
def _write(self, text):
|
||||||
"""Write a text to the file opened in the fake editor.
|
"""Write a text to the file opened in the fake editor.
|
||||||
@ -204,7 +210,8 @@ class ErrorMessageTests(unittest.TestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.editor = editorutils.ExternalEditor()
|
self.editor = editorutils.ExternalEditor()
|
||||||
editorutils.config = ConfigStub({'general': {'editor': ['']}})
|
editorutils.config = ConfigStub(
|
||||||
|
{'general': {'editor': [''], 'editor-encoding': 'utf-8'}})
|
||||||
|
|
||||||
def test_proc_error(self):
|
def test_proc_error(self):
|
||||||
"""Test on_proc_error."""
|
"""Test on_proc_error."""
|
||||||
|
@ -72,7 +72,8 @@ class ExternalEditor(QObject):
|
|||||||
message.error("Editor did quit abnormally (status {})!".format(
|
message.error("Editor did quit abnormally (status {})!".format(
|
||||||
exitcode))
|
exitcode))
|
||||||
return
|
return
|
||||||
with open(self.filename, 'r', encoding='utf-8') as f:
|
encoding = config.get('general', 'editor-encoding')
|
||||||
|
with open(self.filename, 'r', encoding=encoding) as f:
|
||||||
text = ''.join(f.readlines())
|
text = ''.join(f.readlines())
|
||||||
logger.debug("Read back: {}".format(text))
|
logger.debug("Read back: {}".format(text))
|
||||||
self.editing_finished.emit(text)
|
self.editing_finished.emit(text)
|
||||||
@ -110,7 +111,8 @@ class ExternalEditor(QObject):
|
|||||||
self.text = text
|
self.text = text
|
||||||
self.oshandle, self.filename = mkstemp(text=True)
|
self.oshandle, self.filename = mkstemp(text=True)
|
||||||
if text:
|
if text:
|
||||||
with open(self.filename, 'w', encoding='utf-8') as f:
|
encoding = config.get('general', 'editor-encoding')
|
||||||
|
with open(self.filename, 'w', encoding=encoding) as f:
|
||||||
f.write(text)
|
f.write(text)
|
||||||
self.proc = QProcess(self)
|
self.proc = QProcess(self)
|
||||||
self.proc.finished.connect(self.on_proc_closed)
|
self.proc.finished.connect(self.on_proc_closed)
|
||||||
|
Loading…
Reference in New Issue
Block a user