From ba1fcd4e20ce6793548e62d2d95c6ac10550b0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Sat, 29 Oct 2016 10:32:08 +0200 Subject: [PATCH] Add version info. Use same prerelease convention as Nvim itself. --- neovim/__init__.py | 15 +++++++++++++-- neovim/api/nvim.py | 4 +++- neovim/util.py | 20 ++++++++++++++++++++ setup.py | 2 +- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/neovim/__init__.py b/neovim/__init__.py index d84b968b..a25b3035 100644 --- a/neovim/__init__.py +++ b/neovim/__init__.py @@ -12,14 +12,18 @@ stdio_session, tcp_session) from .plugin import (Host, autocmd, command, decode, encoding, function, plugin, rpc_export, shutdown_hook) +from .util import Version __all__ = ('tcp_session', 'socket_session', 'stdio_session', 'child_session', 'start_host', 'autocmd', 'command', 'encoding', 'decode', - 'function', 'plugin', 'rpc_export', 'Host', 'Nvim', + 'function', 'plugin', 'rpc_export', 'Host', 'Nvim', 'VERSION', 'shutdown_hook', 'attach', 'setup_logging', 'ErrorResponse') +VERSION = Version(major=0, minor=1, patch=11, prerelease="dev") + + def start_host(session=None): """Promote the current process into python plugin host for Nvim. @@ -64,7 +68,14 @@ def start_host(session=None): if not session: session = stdio_session() - host = Host(Nvim.from_session(session)) + nvim = Nvim.from_session(session) + + if nvim.version.api_level < 1: + sys.stderr.write("This version of the neovim python package " + "requires nvim 0.1.6 or later") + sys.exit(1) + + host = Host(nvim) host.start(plugins) diff --git a/neovim/api/nvim.py b/neovim/api/nvim.py index c5102a1a..e93da598 100644 --- a/neovim/api/nvim.py +++ b/neovim/api/nvim.py @@ -13,7 +13,7 @@ from .tabpage import Tabpage from .window import Window from ..compat import IS_PYTHON3 -from ..util import format_exc_skip +from ..util import Version, format_exc_skip __all__ = ('Nvim') @@ -74,6 +74,8 @@ def __init__(self, session, channel_id, metadata, types, self._session = session self.channel_id = channel_id self.metadata = metadata + version = metadata.get("version", {"api_level": 0}) + self.version = Version(**version) self.types = types self.api = RemoteApi(self, 'nvim_') self.vars = RemoteMap(self, 'nvim_get_var', 'nvim_set_var') diff --git a/neovim/util.py b/neovim/util.py index cd4e009f..798656db 100644 --- a/neovim/util.py +++ b/neovim/util.py @@ -10,3 +10,23 @@ def format_exc_skip(skip, limit=None): for i in range(skip): tb = tb.tb_next return ('\n'.join(format_exception(type, val, tb, limit))).rstrip() + + +# Taken from SimpleNamespace in python 3 +class Version: + + """Helper class for version info.""" + + def __init__(self, **kwargs): + """Create the Version object.""" + self.__dict__.update(kwargs) + + def __repr__(self): + """Return str representation of the Version.""" + keys = sorted(self.__dict__) + items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys) + return "{}({})".format(type(self).__name__, ", ".join(items)) + + def __eq__(self, other): + """Check if version is same as other.""" + return self.__dict__ == other.__dict__ diff --git a/setup.py b/setup.py index fab84e32..f3837036 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ install_requires.append('greenlet') setup(name='neovim', - version='0.1.10', + version='0.1.11dev', description='Python client to neovim', url='http://github.com/neovim/python-client', download_url='https://github.com/neovim/python-client/archive/0.1.10.tar.gz',