Git Product home page Git Product logo

python-editor's Introduction

python-editor is a library that provides the editor module for programmatically interfacing with your system's $EDITOR.

Examples

import editor
commit_msg = editor.edit(contents=b"# Enter commit message here")

Opens an editor, prefilled with the contents, # Enter commit message here. When the editor is closed, returns the contents (bytes) in variable commit_msg. Note that the argument to contents needs to be a bytes object on Python 3.

editor.edit(file="README.txt")

Opens README.txt in an editor. Changes are saved in place. If there is a contents argument then the file contents will be overwritten.

editor.edit(..., use_tty=True)

Opens the editor in a TTY. This is usually done in programs which output is piped to other programs. In this case the TTY is used as the editor's stdout, allowing interactive usage.

How it Works

editor first looks for the ${EDITOR} environment variable. If set, it uses the value as-is, without fallbacks.

If no $EDITOR is set, editor will search through a list of known editors, and use the first one that exists on the system.

For example, on Linux, editor will look for the following editors in order:

  • vim
  • emacs
  • nano

When calling editor.edit, an editor will be opened in a subprocess, inheriting the parent process's stdin, stdout.

python-editor's People

Contributors

branchvincent avatar chkumar246 avatar elsonrodriguez avatar eugene-eeo avatar fmoo avatar hugovk avatar josh-gree avatar nackjicholson avatar philipbergen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-editor's Issues

Using "nano" opens an empty file before loading the selected file.

When using nano as editor, python-editor first opens an empty file. After closing that file, the given file is opened.

Test script:

from editor import edit
from os import unlink
from tempfile import NamedTemporaryFile


testfile = NamedTemporaryFile(prefix='editest', delete=False)
testfile.write('Hello World!')
testfile.close()


edit(testfile.name)


unlink(testfile.name)

Executing this as follows, works as expected:

EDITOR=vim python testscript.py

However, the following does not open the test file immediately:

EDITOR=nano python testscript.py

Please add support for /usr/bin/editor on Debian

In Debian, we have a system called alternatives which allows the user to select the default applications for several tasks, including the editor. This system provides an /usr/bin/editor symlink pointing to user's command-line editor of choice. It would be nice if you could add support for this.

Our Policy recommends the following algorithm:

Thus, every program that launches an editor or pager must use the EDITOR or PAGER environment variable to determine the editor or pager the user wishes to use. If these variables are not set, the programs /usr/bin/editor and /usr/bin/pager should be used, respectively.

PermissionError: [Errno 13] on Windows 10

On Windows I get an error. This is because tmp = tempfile.NamedTemporaryFile(suffix=suffix) (line 84) opens and locks the file.
This can be fixed by calling tmp.close() directly afterwards but I don't know if this breaks anything on other OSes.

>>> editor.edit(contents="hello")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Max\AppData\Local\pypoetry\Cache\virtualenvs\bulletin-3-N3PFQvT7-py3.10\lib\site-packages\editor.py", line 92, in edit
    with open(filename, mode='wb') as f:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Max\\AppData\\Local\\Temp\\tmp55cimtei'

module 'editor' has no attribute 'edit' with python3

import editor
commit_msg = editor.edit(contents=b"# Enter commit message here")

This works in the python3 command line (while exhibiting bug #1), but when called as a script I get the following error.

$ python3 /home/leggewie/.PyCharmCE2019.2/config/scratches/editor.py

Traceback (most recent call last):
File "/home/leggewie/.PyCharmCE2019.2/config/scratches/editor.py", line 1, in
import editor
File "/home/leggewie/.PyCharmCE2019.2/config/scratches/editor.py", line 3, in
commit_msg = editor.edit(contents=b"# Enter commit message here")
AttributeError: module 'editor' has no attribute 'edit'

python3 -c 'import editor; editor.edit(contents=b"# Enter commit message here")' works fine and does not bring about #1, either.

pytest: deprecation warning: imp module is deprecated.

When I use pytest, I have this warning:

.../ve/lib/python3.6/distutils/__init__.py:4
  .../ve/lib/python3.6/distutils/__init__.py:4: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
    import imp

It seems related to this line:

from distutils.spawn import find_executable

distutils imports imp.

shutil.which seems to be an alternative supported since Python 3.3.

Having an $EDITOR with arguments raises an exception

editor.edit('file') fails when the defined $EDITOR contains spaces:

$ touch /tmp/file
$ export EDITOR='vim -v'
$ python -c 'import editor; editor.edit("/tmp/file")' 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "editor.py", line 103, in edit
    proc = subprocess.Popen(args, close_fds=True, stdout=stdout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

After removing the space, it works:

$ export EDITOR='vim'
$ python -c 'import editor; editor.edit("/tmp/file")'

Originally (and improperly) reported here as an Alembic issue.

Thanks

Not a "real" issue, just thought about dropping by to say thank you for such an easy-to-use module. ๐Ÿ‘

0.5 Release Tag

Hey @fmoo, would you mind adding a release tag for 0.5 if the current master is ready?

Open editor with line number?

Great work BTW...

I'm hoping to use this for a nose plugin I'm writing, but id like to pass a line number through to the editor command... Any chance you could expose that? or better still allow arbitrary args to be passed through?

e.g. emacs +123

Thanks

[noeol] 1L, 3C Error

Trying to get a message doing the following:

msg = editor.edit(contents=b"PO# ")

instead the editor hangs and my Mac's fans kick in. It works when running as admin but right now I'm running in my non-admin desktop.

DeprecationWarning: The distutils package is deprecated

This line seems to trigger a deprecation warning on Python 3.10. This happens both for the latest released version (1.0.4) as well as the latest master.

Minimal working example

$ python -W default -c 'import editor; editor.get_editor()'
/Users/alex/my-project/venv/lib/python3.10/site-packages/editor.py:11: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.spawn import find_executable

Impact

  • Noisy test output from pytest test suite. Explanation: DeprecationWarnings are silenced by default but pytest shows them anyway. Projects that use editor get warnings in their test output.
  • This functionality will eventually break in Python 3.12.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.