from . import util
from . import autogenerate as autogen
+import editor
+
def list_templates(config):
"""List available templates"""
head = next(revisions)
try:
- util.open_in_editor(head.path)
- except OSError as exc:
+ editor.edit(head.path)
+ except Exception as exc:
raise util.CommandError('Error executing editor (%s)' % (exc,))
action="store",
help="Specify a revision range; "
"format is [start]:[end]")
- ),
+ )
}
positional_help = {
'directory': "location of scripts directory",
from .sqla_compat import ( # noqa
sqla_07, sqla_079, sqla_08, sqla_083, sqla_084, sqla_09, sqla_092,
sqla_094, sqla_094, sqla_099, sqla_100, sqla_105)
-from .os_helpers import ( # noqa
- open_in_editor)
class CommandError(Exception):
+++ /dev/null
-from os.path import join, exists
-from subprocess import check_call
-import os
-
-
-def open_in_editor(filename, environ=None):
- '''
- Opens the given file in a text editor. If the environment vaiable ``EDITOR``
- is set, this is taken as preference.
-
- Otherwise, a list of commonly installed editors is tried.
-
- If no editor matches, an :py:exc:`OSError` is raised.
-
- :param filename: The filename to open. Will be passed verbatim to the
- editor command.
- :param environ: An optional drop-in replacement for ``os.environ``. Used
- mainly for testing.
- '''
-
- environ = environ or os.environ
-
- # Look for an editor. Prefer the user's choice by env-var, fall back to most
- # commonly installed editor (nano/vim)
- candidates = [
- '/usr/bin/sensible-editor',
- '/usr/bin/nano',
- '/usr/bin/vim',
- ]
-
- if 'EDITOR' in environ:
- user_choice = environ['EDITOR']
- if '/' not in user_choice:
- # Assuming this is on the PATH, we need to determine it's absolute
- # location. Otherwise, ``check_call`` will fail
- for path in environ.get('PATH', '').split(os.pathsep):
- if exists(join(path, user_choice)):
- user_choice = join(path, user_choice)
- break
- candidates.insert(0, user_choice)
-
- for path in candidates:
- if exists(path):
- editor = path
- break
- else:
- raise OSError('No suitable editor found. Please set the '
- '"EDITOR" environment variable')
- check_call([editor, filename])
requires = [
'SQLAlchemy>=0.7.6',
'Mako',
+ 'python-editor',
]
# Hack to prevent "TypeError: 'NoneType' object is not callable" error
from alembic import command
-from mock import patch
from io import TextIOWrapper, BytesIO
from alembic.script import ScriptDirectory
from alembic.testing.fixtures import TestBase, capture_context_buffer
from alembic.testing import eq_, assert_raises_message
from alembic import util
+try:
+ from mock import patch
+except ImportError:
+ from unittest.mock import patch
+
class HistoryTest(TestBase):
def teardown_class(cls):
clear_staging_env()
- def test_edit_with_user_editor(self):
- expected_call_arg = '%s/scripts/versions/%s_revision_c.py' % (
- EditTest.cfg.config_args['here'],
- EditTest.c
- )
-
- with patch('alembic.util.os_helpers.check_call') as check_call, \
- patch('alembic.util.os_helpers.exists') as exists:
- exists.side_effect = lambda fname: fname == '/usr/bin/vim'
- command.edit(self.cfg)
- check_call.assert_called_with(['/usr/bin/vim', expected_call_arg])
-
- def test_edit_with_default_editor(self):
+ def test_edit_latest(self):
expected_call_arg = '%s/scripts/versions/%s_revision_c.py' % (
EditTest.cfg.config_args['here'],
EditTest.c
)
- with patch('alembic.util.os_helpers.check_call') as check_call, \
- patch('alembic.util.os_helpers.exists') as exists:
- exists.side_effect = lambda fname: fname == '/usr/bin/vim'
+ with patch('alembic.command.editor.edit') as edit:
command.edit(self.cfg)
- check_call.assert_called_with(['/usr/bin/vim', expected_call_arg])
+ edit.assert_called_with(expected_call_arg)
def test_edit_with_missing_editor(self):
- with patch('alembic.util.os_helpers.check_call'), \
- patch('alembic.util.os_helpers.exists') as exists:
- exists.return_value = False
+ with patch('alembic.command.editor.edit') as edit:
+ edit.side_effect = OSError('file not found')
assert_raises_message(
util.CommandError,
- 'EDITOR',
+ 'file not found',
command.edit,
self.cfg)
+++ /dev/null
-from alembic import util
-from alembic.testing import assert_raises_message
-from alembic.testing.fixtures import TestBase
-
-try:
- from unittest.mock import patch
-except ImportError:
- from mock import patch # noqa
-
-
-class TestHelpers(TestBase):
-
- def test_edit_with_user_editor(self):
- test_environ = {
- 'EDITOR': 'myvim',
- 'PATH': '/usr/bin'
- }
-
- with patch('alembic.util.os_helpers.check_call') as check_call, \
- patch('alembic.util.os_helpers.exists') as exists:
- exists.side_effect = lambda fname: fname == '/usr/bin/myvim'
- util.open_in_editor('myfile', test_environ)
- check_call.assert_called_with(['/usr/bin/myvim', 'myfile'])
-
- def test_edit_with_default_editor(self):
- test_environ = {}
-
- with patch('alembic.util.os_helpers.check_call') as check_call, \
- patch('alembic.util.os_helpers.exists') as exists:
- exists.side_effect = lambda fname: fname == '/usr/bin/vim'
- util.open_in_editor('myfile', test_environ)
- check_call.assert_called_with(['/usr/bin/vim', 'myfile'])
-
- def test_edit_with_missing_editor(self):
- test_environ = {}
- with patch('alembic.util.os_helpers.check_call'), \
- patch('alembic.util.os_helpers.exists') as exists:
- exists.return_value = False
- assert_raises_message(
- OSError,
- 'EDITOR',
- util.open_in_editor,
- 'myfile',
- test_environ)