Improve the capabilities for launching editors for text.
Review Request #10668 — Created Aug. 28, 2019 and submitted
RBTools sometimes needs to invoke editors (to create commit messages,
for instance). The code we had before failed on Python 3, due to string
type mismatches when reading in the resulting edited file content, but
even with that fixed, the editing experience was error-prone and far
from ideal.For instance, if things went wrong during
edit_text()
, we'd get some
generic exception, likeOSError
. We now have a dedicated error for
editing-related failures,EditorError
.Another limitation was that we had no control over the filename of the
edited file. This meant we couldn't influence the editor -- no syntax
highlighting or other editor settings would apply, creating a subpar
experience. This is currently most important for editing commit
messages.To address this, we needed to provide a filename during the editing
process, but it wasn't as simple as just a standard filename. We needed
to support two different ways of doing this: The Git way (using a fixed
filename for the file), and the Mercurial way (using a generated temp
filename with a prefix and a suffix).
edit_text()
has been updated to make the Git approach easier. It now
takes a filename, which will be passed tomake_tempfile()
to generate
a specific filename for the content. It will also immediately delete
that file once it's been read back in, just to keep the filesystem a bit
more tidy.To support the Mercurial approach, a new method was added:
edit_file()
. This contains the core logic ofedit_text()
, but
operates on an existing filename. This means that for more advanced
cases, like with Mercurial commit messages, we can generate a commit
filename elsewhere and then pass it toedit_file()
.These new editing capabilities have one additional benefit: The
configured editor can now be specified asRBTOOLS_EDITOR
, letting us
define an editor that isn't dependent on theVISUAL
orEDITOR
environment variables. This is mostly useful for testing for now, but
can be useful if someone wants an explicit editor to be used for RBTools
(or a wrapper script that processes text in some way).A dummy editor has been added inside
rbtools/testing/scripts/
for
testing purposes, allowing us to have full test coverage of these
methods. It simply uppercases the whole file.
Unit tests pass.
Manually tested the new editor errors and capabilities alongside an
upcoming change to leverage all this in Git.
Description | From | Last Updated |
---|---|---|
Docstring? |
david | |
This doesn't need to be split across lines. |
david | |
Don't keys in os.environ have to be bytestrings? |
david | |
E202 whitespace before ')' |
reviewbot | |
should be str('RBTOOLS_EDITOR') and str(self.default_text_editor) |
david | |
Do we need to force_unicode(editor)? |
david |
- Change Summary:
-
- Consolidated two lines into one.
- Switched to native strings for
os.environ
access.
- Commit:
-
2d5685deb26fd82cce43150a6bdb053bd433d57cb062840d650a0620995afd865f7f00de72bf1f1c
- Change Summary:
-
Fixed a whitespace issue.
- Commit:
-
b062840d650a0620995afd865f7f00de72bf1f1cf877972c54a5e25d7e069df513c0b4af5721a0a6