コード例 #1
0
 def test_import(self):
     comp = DynamicCompiler([self.tmpdir])
     self.tmpdir.join("p.capnp").write("""
     @0xbf5147cbbecf40c1;
     struct Point {
         x @0 :Int64;
         y @1 :Int64;
     }
     enum Color {
         blue @0;
         yellow @1;
     }
     """)
     self.tmpdir.join("tmp.capnp").write("""
     @0xbf5147cbbecf40c2;
     using P = import "/p.capnp";
     struct Rectangle {
         a @0 :P.Point;
         b @1 :P.Point;
         c @2 :List(P.Point);
         d @3 :List(P.Color);
     }
     """)
     mod_tmp = comp.load_schema(importname="/tmp.capnp", pyx=self.pyx)
     self.check_pyx(mod_tmp)
     self.check_pyx(mod_tmp._p_capnp)
     a = mod_tmp._p_capnp.Point(1, 2)
     b = mod_tmp._p_capnp.Point(3, 4)
     blue = mod_tmp._p_capnp.Color.blue
     yellow = mod_tmp._p_capnp.Color.yellow
     rec = mod_tmp.Rectangle(a, b, c=[a, b], d=[blue, yellow])
     assert rec.b.x == 3
     assert rec.c[1].x == 3
     assert rec.d[1] == yellow == 1
コード例 #2
0
    def test_extended(self, monkeypatch):
        myschema = self.tmpdir.join('myschema.capnp')
        myschema_extended = self.tmpdir.join('myschema_extended.py')

        comp = DynamicCompiler([self.tmpdir])
        myschema.write("""
        @0xbf5147cbbecf40c1;
        struct Point {
            x @0 :Int64;
            y @1 :Int64;
        }
        """)
        myschema_extended.write(
            textwrap.dedent("""
        @Point.__extend__
        class Point:
            foo = 'foo'
            def x2(self):
                return self.x * 2
        """))
        #
        monkeypatch.setattr(capnpy, 'mycompiler', comp, raising=False)
        monkeypatch.syspath_prepend(self.tmpdir)
        mod = comp.load_schema('myschema', pyx=self.pyx)
        assert mod.Point.foo == 'foo'
        #
        p = mod.Point(5, 6)
        assert p.x == 5
        assert p.x2() == 10
コード例 #3
0
 def compile(self, s, **kwargs):
     # root is needed to be able to import capnpy/py.capnp
     root = py.path.local(capnpy.__file__).dirpath('..')
     comp = DynamicCompiler([root, self.tmpdir])
     comp.annotate = self.annotate
     tmp_capnp = self.tmpdir.join('tmp.capnp')
     tmp_capnp.write(s)
     options = annotate.Options.from_dict(kwargs)
     schema = comp.load_schema(importname='/tmp.capnp',
                               pyx=self.pyx,
                               options=options)
     return schema
コード例 #4
0
 def test_parse_schema(self, tmpdir):
     schema = """
     @0xbf5147cbbecf40c1;
     struct Point {
         x @0 :Int64;
         y @1 :Int64;
     }
     """
     filename = tmpdir.join('foo.capnp')
     filename.write(schema)
     comp = DynamicCompiler([])
     req = comp.parse_schema(filename=filename)
     assert req.requestedFiles[0].filename == str(filename)
コード例 #5
0
 def test_parse_schema(self, tmpdir):
     schema = """
     @0xbf5147cbbecf40c1;
     struct Point {
         x @0 :Int64;
         y @1 :Int64;
     }
     """
     filename = tmpdir.join('foo.capnp')
     filename.write(schema)
     comp = DynamicCompiler([])
     req = comp.parse_schema(filename=filename)
     # from capnproto 0.7.0, RequestedFile.filename no longer contains an
     # absolute path: https://github.com/capnproto/capnproto/issues/881
     #
     # For the purpose of this test, it is enough to check that the
     # basename is the same
     reqname = req.requestedFiles[0].filename.decode('utf-8')
     assert py.path.local(reqname).basename == filename.basename
コード例 #6
0
 def test_import_enum(self):
     comp = DynamicCompiler([self.tmpdir])
     self.tmpdir.join("p.capnp").write("""
     @0xbf5147cbbecf40c1;
     enum Color {
         blue @0;
         yellow @1;
     }
     """)
     self.tmpdir.join("tmp.capnp").write("""
     @0xbf5147cbbecf40c2;
     using P = import "/p.capnp";
     struct Rectangle {
         x @0 :P.Color;
     }
     """)
     mod_tmp = comp.load_schema(importname="/tmp.capnp", pyx=self.pyx)
     blue = mod_tmp._p_capnp.Color.blue
     rec = mod_tmp.Rectangle(blue)
     assert rec.x == blue
コード例 #7
0
 def test_import(self):
     comp = DynamicCompiler([self.tmpdir])
     self.tmpdir.join("p.capnp").write("""
     @0xbf5147cbbecf40c1;
     struct Point {
         x @0 :Int64;
         y @1 :Int64;
     }
     """)
     self.tmpdir.join("tmp.capnp").write("""
     @0xbf5147cbbecf40c2;
     using P = import "/p.capnp";
     struct Rectangle {
         a @0 :P.Point;
         b @1 :P.Point;
     }
     """)
     mod = comp.load_schema(importname="/tmp.capnp", pyx=self.pyx)
     self.check_pyx(mod)
     self.check_pyx(mod._p_capnp)
コード例 #8
0
    def test_import_absolute(self):
        one = self.tmpdir.join('one').ensure(dir=True)
        two = self.tmpdir.join('two').ensure(dir=True)

        comp = DynamicCompiler([self.tmpdir])
        one.join("p.capnp").write("""
        @0xbf5147cbbecf40c1;
        struct Point {
            x @0 :Int64;
            y @1 :Int64;
        }
        """)
        two.join("tmp.capnp").write("""
        @0xbf5147cbbecf40c2;
        using P = import "/one/p.capnp";
            struct Rectangle {
            a @0 :P.Point;
            b @1 :P.Point;
        }
        """)
        mod = comp.load_schema(importname="/two/tmp.capnp", pyx=self.pyx)
コード例 #9
0
 def test_load_schema_dont_load_twice(self):
     schema = """
     @0xbf5147cbbecf40c1;
     struct Point {
         x @0 :Int64;
         y @1 :Int64;
     }
     """
     mypkg = self.tmpdir.join("mypkg").ensure(dir=True)
     myschema = mypkg.join("myschema.capnp")
     myschema.write(schema)
     comp = DynamicCompiler([self.tmpdir])
     mod1 = comp.load_schema(modname="mypkg.myschema", pyx=self.pyx)
     mod2 = comp.load_schema(modname="mypkg.myschema", pyx=self.pyx)
     assert mod1 is mod2
     #
     mod3 = comp.load_schema(importname="/mypkg/myschema.capnp",
                             pyx=self.pyx)
     assert mod3 is mod1
     #
     mod4 = comp.load_schema(filename=myschema, pyx=self.pyx)
     assert mod4 is mod1
コード例 #10
0
 def test_import(self):
     comp = DynamicCompiler([self.tmpdir])
     self.tmpdir.join("p.capnp").write("""
     @0xbf5147cbbecf40c1;
     struct Point {
         x @0 :Int64;
         y @1 :Int64;
     }
     """)
     self.tmpdir.join("tmp.capnp").write("""
     @0xbf5147cbbecf40c2;
     using P = import "/p.capnp";
     struct Rectangle {
         a @0 :P.Point;
         b @1 :P.Point;
     }
     """)
     mod_tmp = comp.load_schema(importname="/tmp.capnp", pyx=self.pyx)
     self.check_pyx(mod_tmp)
     self.check_pyx(mod_tmp._p_capnp)
     a = mod_tmp._p_capnp.Point(1, 2)
     b = mod_tmp._p_capnp.Point(3, 4)
     rec = mod_tmp.Rectangle(a, b)
     assert rec.b.x == 3
コード例 #11
0
import sys
import pkg_resources

from capnpy.compiler.compiler import DynamicCompiler
from capnpy.compiler.distutils import capnpify
from capnpy.message import load, loads, load_all, dumps, dump


try:
    __version__ = pkg_resources.get_distribution('capnpy').version
except Exception:
    __version__ = 'unknown'

_compiler = DynamicCompiler(sys.path)
load_schema = _compiler.load_schema
parse_schema = _compiler.parse_schema