From 57dc3aa9fef0a6f5379db76b435b1e65b22855b5 Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Sun, 25 Aug 2013 17:56:59 -0700 Subject: [PATCH 1/6] Fix version detection --- docs/conf.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 764c8fd..15aa9d8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -48,19 +48,10 @@ copyright = u'2013, Author' # built documents. # # The short X.Y version. -def extract_version(): - """extract version from version.py, so it's not multiply defined""" - with open(os.path.join('..', 'capnp', 'version.py')) as f: - line = f.readline() - while not line.startswith("version"): - line = f.readline() - print line - exec(line) - return version +import capnp -vs = extract_version() +vs = capnp.__version__ # The short X.Y version. -import string version = vs.rstrip(string.letters) # The full version, including alpha/beta/rc tags. release = vs From 56d4646c01737fc3c86af3cc3921dd329e0bf372 Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Mon, 26 Aug 2013 00:57:07 -0700 Subject: [PATCH 2/6] Add C++ library version check --- capnp/fixMaybe.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/capnp/fixMaybe.h b/capnp/fixMaybe.h index be56680..a1dfac6 100644 --- a/capnp/fixMaybe.h +++ b/capnp/fixMaybe.h @@ -1,6 +1,8 @@ #include "kj/common.h" #include +static_assert(CAPNP_VERSION >= 3000, "Version of Cap'n Proto C++ Library is too old. Please upgrade to a version >= 0.3 and then re-install this python library"); + template T fixMaybe(::kj::Maybe val) { KJ_IF_MAYBE(new_val, val) { From a4d2bd6020fa05dd4f38a53214bb856e3751c026 Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Mon, 26 Aug 2013 10:07:54 -0700 Subject: [PATCH 3/6] Add some docstrings --- capnp/__init__.py | 35 +++++++++++++++++++++++++ capnp/capnp.pyx | 66 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 83 insertions(+), 18 deletions(-) diff --git a/capnp/__init__.py b/capnp/__init__.py index 6044b55..d63cf33 100644 --- a/capnp/__init__.py +++ b/capnp/__init__.py @@ -1,2 +1,37 @@ +"""A python library wrapping the Cap'n Proto C++ library + +Example Usage:: + + import capnp + + addressbook = capnp.load('addressbook.capnp') + + # Building + message = capnp.MallocMessageBuilder() + addressBook = message.initRoot(addressbook.AddressBook) + people = addressBook.init('people', 2) + + alice = people[0] + alice.id = 123 + alice.name = 'Alice' + alice.email = 'alice@example.com' + alicePhone = alice.init('phones', 1)[0] + alicePhone.type = 'mobile' + + f = open('example.bin', 'w') + capnp.writePackedMessageToFd(f.fileno(), message) + f.close() + + # Reading + f = open('example.bin') + message = capnp.PackedFdMessageReader(f.fileno()) + + addressBook = message.getRoot(addressbook.AddressBook) + + for person in addressBook.people: + print(person.name, ':', person.email) + for phone in person.phones: + print(phone.type, ':', phone.number) +""" from .version import version as __version__ from .capnp import * diff --git a/capnp/capnp.pyx b/capnp/capnp.pyx index e219fa6..870b147 100644 --- a/capnp/capnp.pyx +++ b/capnp/capnp.pyx @@ -508,25 +508,55 @@ def writePackedMessageToFd(int fd, MessageBuilder m): from types import ModuleType import os -def _load(nodeSchema, module): - module._nodeSchema = nodeSchema - nodeProto = nodeSchema.getProto() - module._nodeProto = nodeProto - - for node in nodeProto.nestedNodes: - local_module = ModuleType(node.name) - module.__dict__[node.name] = local_module - - schema = nodeSchema.getNested(node.name) - proto = schema.getProto() - if proto.isStruct: - local_module.Schema = schema.asStruct() - elif proto.isConst: - module.__dict__[node.name] = schema.asConstValue() - - _load(schema, local_module) - def load(file_name, display_name=None, imports=[]): + """load a Cap'n Proto schema from a file + + You will have to load a schema before you can begin doing anything + meaningful with this library. Loading a schema is much like Loading + a Python module (and load even returns a ModuleType). Once it's been + loaded, you use it much like any other Module:: + + addressbook = capnp.load('addressbook.capnp') + print addressbook.qux # qux is a top level constant + # 123 + message = capnp.MallocMessageBuilder() + person = message.initRoot(addressbook.Person) + + :type file_name: str + :param file_name: A relative or absolute path to a Cap'n Proto schema + + :type display_name: str + :param display_name: The name internally used by the Cap'n Proto library + for the loaded schema. By default, it's just os.path.basename(file_name) + + :type imports: list + :param imports: A list of str directories to add to the import path. + + :rtype: ModuleType + :return: A module corresponding to the loaded schema. You can access + parsed schemas and constants with . syntax + + :Raises: :exc:`exceptions.ValueError` if `file_name` doesn't exist + + """ + def _load(nodeSchema, module): + module._nodeSchema = nodeSchema + nodeProto = nodeSchema.getProto() + module._nodeProto = nodeProto + + for node in nodeProto.nestedNodes: + local_module = ModuleType(node.name) + module.__dict__[node.name] = local_module + + schema = nodeSchema.getNested(node.name) + proto = schema.getProto() + if proto.isStruct: + local_module.Schema = schema.asStruct() + elif proto.isConst: + module.__dict__[node.name] = schema.asConstValue() + + _load(schema, local_module) + if display_name is None: display_name = os.path.basename(file_name) module = ModuleType(display_name) From 42cc239e568957a935d93896f6d0bf91c34f4462 Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Mon, 26 Aug 2013 10:08:16 -0700 Subject: [PATCH 4/6] Add intersphinx for linking to python docs --- docs/conf.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 15aa9d8..08eed2d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,7 +25,7 @@ import sys, os, string # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode', 'sphinx.ext.intersphinx'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -287,3 +287,5 @@ epub_copyright = u'2013, Author' # Allow duplicate toc entries. #epub_tocdup = True + +intersphinx_mapping = {'http://docs.python.org/': None} From 5001726e0273c58076513918ebab8a416f26823c Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Mon, 26 Aug 2013 10:08:34 -0700 Subject: [PATCH 5/6] Add requirements.txt to MANIFEST. Also add version checking for setuptools to setup.py. --- MANIFEST.in | 1 + README.md | 4 +++- setup.py | 8 ++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index bb3ec5f..3d387c3 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1 +1,2 @@ include README.md +include requirements.txt \ No newline at end of file diff --git a/README.md b/README.md index 3ce31e9..141093b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ #capnpc-python-cpp +More thorough docs are available at [http://jparyani.github.io/capnpc-python-cpp/](http://jparyani.github.io/capnpc-python-cpp/). + ## Requirements -First you need a system-wide installation of the Capnproto C++ library >= 0.3. Unfortunately, as of now, that means you have to build from the HEAD of Cap'n Proto. Follow these instructions to do so: +First you need a system-wide installation of the Cap'n Proto C++ library >= 0.3. Unfortunately, as of now, that means you have to build from the HEAD of Cap'n Proto. Follow these instructions to do so: ```bash wget https://github.com/kentonv/capnproto/archive/master.zip diff --git a/setup.py b/setup.py index 6451e72..c415983 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,12 @@ except ImportError: raise RuntimeError('No cython installed. Please run `pip install cython`') if Cython.__version__ < '0.19.1': - raise RuntimeError('Old cython installed. Please run `pip install -U cython`') + raise RuntimeError('Old cython installed (%s). Please run `pip install -U cython`' % Cython.__version__) + +import pkg_resources +setuptools_version = pkg_resources.get_distribution("setuptools").version +if setuptools_version < '0.8': + raise RuntimeError('Old setuptools installed (%s). Please run `pip install -U setuptools`. Running `pip install capnp` will not work alone, since setuptools needs to be upgraded before installing anything else.' % setuptools_version) from distutils.core import setup import os @@ -16,7 +21,6 @@ MINOR = 3 MICRO = 5 VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO) - def write_version_py(filename=None): cnt = """\ version = '%s' From dc90acd57a9846128bcd105e052125cdf4568baa Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Mon, 26 Aug 2013 10:09:53 -0700 Subject: [PATCH 6/6] Bump version for v0.3.6 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index c415983..5ef3ae1 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ import os MAJOR = 0 MINOR = 3 -MICRO = 5 +MICRO = 6 VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO) def write_version_py(filename=None):