Ejemplo n.º 1
0
    def test_enum_with_missing_lbrace_raises_exception_with_correct_message(self):
        s = "enum MissingLBrace    NO = 0;    YES = 1;}"
        parser = Parser()

        try:
            parser.parse(s)
        except Exception as e:
            self.assertEqual("missing opening paren at pos 22: 'NO = 0;   '", str(e))
Ejemplo n.º 2
0
    def test_message_with_missing_lbrace_raises_exception_with_correct_message(self):
        s = "message MissingLBrace    required int32 i = 1;    optional int32 j = 2;}"
        parser = Parser()

        try:
            parser.parse(s)
        except Exception as e:
            self.assertEqual("missing opening paren at pos 25: 'required i'", str(e))
Ejemplo n.º 3
0
    def test_message_with_missing_lbrace_raises_exception_with_correct_message(
            self):
        s = "message MissingLBrace    required int32 i = 1;    optional int32 j = 2;}"
        parser = Parser()

        try:
            parser.parse(s)
        except Exception as e:
            self.assertEqual("missing opening paren at pos 25: 'required i'",
                             str(e))
Ejemplo n.º 4
0
    def test_enum_with_missing_lbrace_raises_exception_with_correct_message(
            self):
        s = "enum MissingLBrace    NO = 0;    YES = 1;}"
        parser = Parser()

        try:
            parser.parse(s)
        except Exception as e:
            self.assertEqual("missing opening paren at pos 22: 'NO = 0;   '",
                             str(e))
Ejemplo n.º 5
0
def add_pyrobuf_module(dist, pyrobuf_module):
    parser = Parser()

    env = Environment(loader=PackageLoader('pyrobuf.protobuf', 'templates'))
    templ_pxd = env.get_template('proto_pxd.tmpl')
    templ_pyx = env.get_template('proto_pyx.tmpl')

    dir_name = "pyrobuf/_" + pyrobuf_module

    os.makedirs(dir_name, exist_ok=True)

    if os.path.isdir(pyrobuf_module):
        for spec in glob.glob(os.path.join(pyrobuf_module, '*.proto')):
            generate(spec, dir_name, parser, templ_pxd, templ_pyx)

        _, name = os.path.split(pyrobuf_module)
        pyx = os.path.join(dir_name, '*.pyx')

    else:
        name, _ = os.path.splitext(os.path.basename(pyrobuf_module))
        if not name:
            print("not a .proto file")
            return

        generate(pyrobuf_module, dir_name, parser, templ_pxd, templ_pyx)

        pyx = os.path.join(dir_name, "%s_proto.pyx" % name)

    if dist.ext_modules is None:
        dist.ext_modules = []
    dist.ext_modules.extend(cythonize([pyx],
                            include_path=[os.path.join(HERE, 'src'), dir_name]))
Ejemplo n.º 6
0
    def test_oneof(self):
        result = Parser(proto_def).parse()
        message = result['messages'][0]

        # assert that all field are listed in the message
        assert [f.name for f in message.fields.values()] == ['vip', 'url', 'name']

        # assert that the oneofs list their child field names
        assert len(message.oneofs) == 1
        assert 'ServiceName' in message.oneofs
        assert message.oneofs['ServiceName'].fields == ['url', 'name']
Ejemplo n.º 7
0
def gen_message(fname,
                out="out",
                build="build",
                install=False,
                proto3=False,
                force=False):

    if proto3:
        parser = Proto3Parser()
    else:
        parser = Parser()

    env = Environment(loader=PackageLoader('pyrobuf.protobuf', 'templates'))
    templ_pxd = env.get_template('proto_pxd.tmpl')
    templ_pyx = env.get_template('proto_pyx.tmpl')
    generated = set()
    pyx_files = []

    try:
        os.makedirs(out)
    except _FileExistsError:
        pass

    script_args = ['build', '--build-base={0}'.format(build)]
    if install:
        script_args.append('install')

    if force:
        script_args.append('--force')

    if os.path.isdir(fname):
        for spec in glob.glob(os.path.join(fname, '*.proto')):
            generate(spec, out, parser, templ_pxd, templ_pyx, generated,
                     pyx_files)

        _, name = os.path.split(fname)

    else:
        name, _ = os.path.splitext(os.path.basename(fname))
        if not name:
            print("not a .proto file")
            return

        generate(fname, out, parser, templ_pxd, templ_pyx, generated,
                 pyx_files)

    setup(name=name,
          ext_modules=cythonize(pyx_files,
                                include_path=[os.path.join(HERE, 'src'), out]),
          script_args=script_args)
Ejemplo n.º 8
0
 def parsing_result(string):
     return Parser(string).parse()
Ejemplo n.º 9
0
 def test_syntax3_raises_exception(self):
     parser = Parser()
     self.assertRaises(Exception, parser.parse, syntax3_explicit)
Ejemplo n.º 10
0
 def test_field_index_collision(self):
     with self.assertRaises(AssertionError):
         Parser(index_collision_proto).parse()
Ejemplo n.º 11
0
 def test_zero_index_field(self):
     with self.assertRaises(AssertionError):
         Parser(zero_index_proto).parse()
Ejemplo n.º 12
0
 def test_recursive_message_to_field_collision(self):
     with self.assertRaises(AssertionError):
         Parser(recursive_message_to_field_collision_proto).parse()
Ejemplo n.º 13
0
 def test_global_enum_to_message_collision(self):
     with self.assertRaises(AssertionError):
         Parser(global_enum_to_message_collision_proto).parse()
Ejemplo n.º 14
0
 def test_syntax3(self):
     parser = Parser(syntax3_explicit)
     parser.parse()
Ejemplo n.º 15
0
 def test_syntax2(self):
     parser = Parser(syntax2_implicit)
     parser.parse()
     parser = Parser(syntax2_explicit)
     parser.parse()
Ejemplo n.º 16
0
 def test_enum_nonzero_first_value_in_proto3(self):
     with self.assertRaises(AssertionError):
         Parser(nonzero_first_value_proto3).parse()
Ejemplo n.º 17
0
    def test_message_with_missing_lbrace_raises_exception(self):
        s = "message MissingLBrace    required int32 i = 1;    optional int32 j = 2;}"
        parser = Parser()

        self.assertRaises(Exception, parser.parse, s)
Ejemplo n.º 18
0
 def test_enum_name_collision(self):
     with self.assertRaises(AssertionError):
         Parser(enum_name_collision_proto).parse()
Ejemplo n.º 19
0
    def test_enum_with_missing_lbrace_raises_exception(self):
        s = "enum MissingLBrace    NO = 0;    YES = 1;}"
        parser = Parser()

        self.assertRaises(Exception, parser.parse, s)
Ejemplo n.º 20
0
 def test_syntax2(self):
     parser = Parser()
     parser.parse(syntax2_implicit)
     parser.parse(syntax2_explicit)