コード例 #1
0
    def test_extensions(self):
        m = MessageOfTypes()

        primitives = {extDouble: 123.4, extString: "string", NestedExtension.extInt: 4}

        for key, value in primitives.items():
            m.Extensions[key] = value
        m.Extensions[NestedExtension.extNested].req = "nested"

        # Confirm compatibility with JSON serialization
        res = json.loads(json.dumps(protobuf_to_dict(m)))
        expected = {u'___X': {u'100': 123.4,
                              u'101': u'string',
                              u'102': 4,
                              u'103': {u'req': u'nested'}}}
        self.assertEqual(expected, res)
        exts = res['___X']
        assert set(exts.keys()) == set([str(f.number) for f, _ in m.ListFields() if f.is_extension])
        for key, value in primitives.items():
            assert exts[str(key.number)] == value
        assert exts[str(NestedExtension.extNested.number)]['req'] == 'nested'

        deser = dict_to_protobuf(MessageOfTypes, res)
        assert deser
        for key, value in primitives.items():
            assert deser.Extensions[key] == m.Extensions[key]
        assert deser.Extensions[NestedExtension.extNested].req == m.Extensions[NestedExtension.extNested].req
コード例 #2
0
 def test_incomplete(self):
     m = self.populate_MessageOfTypes()
     d = protobuf_to_dict(m)
     d.pop('dubl')
     m2 = dict_to_protobuf(MessageOfTypes, d)
     assert m2.dubl == 0
     assert m != m2
コード例 #3
0
 def test_pass_instance(self):
     m = self.populate_MessageOfTypes()
     d = protobuf_to_dict(m)
     d['dubl'] = 1
     m2 = dict_to_protobuf(m, d)
     assert m is m2
     assert m.dubl == 1
コード例 #4
0
    def test_basics(self):
        m = self.populate_MessageOfTypes()
        d = protobuf_to_dict(m)
        self.compare(m, d, ['nestedRepeated'])

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2
コード例 #5
0
 def test_incomplete(self):
     m = self.populate_MessageOfTypes()
     d = protobuf_to_dict(m)
     d.pop('dubl')
     m2 = dict_to_protobuf(MessageOfTypes, d)
     assert m2.dubl == 0
     assert m != m2
コード例 #6
0
    def test_full_extensions(self):
        m = MessageOfTypes()

        primitives = {extDouble: 123.4, extString: "string", NestedExtension.extInt: 4}

        for key, value in primitives.items():
            m.Extensions[key] = value
        m.Extensions[NestedExtension.extNested].req = "nested"

        # Confirm compatibility with JSON serialization
        res = json.loads(json.dumps(protobuf_to_dict(m, extensions=FULL_EXTENSIONS)))
        expected = {u'tests.extDouble': 123.4,
                    u'tests.NestedExtension.extInt': 4,
                    u'tests.NestedExtension.extNested': {u'req': u'nested'},
                    u'tests.extString': u'string'}
        self.assertEqual(expected, res)
        self.assertEqual(set(res.keys()), set([str(f.full_name) for f, _ in m.ListFields() if f.is_extension]))
        for key, value in primitives.items():
            assert res[key.full_name] == value
        assert res[str(NestedExtension.extNested.full_name)]['req'] == 'nested'

        deser = dict_to_protobuf(MessageOfTypes, res)
        assert deser
        for key, value in primitives.items():
            assert deser.Extensions[key] == m.Extensions[key]
        assert deser.Extensions[NestedExtension.extNested].req == m.Extensions[NestedExtension.extNested].req
コード例 #7
0
 def test_pass_instance(self):
     m = self.populate_MessageOfTypes()
     d = protobuf_to_dict(m)
     d['dubl'] = 1
     m2 = dict_to_protobuf(m, d)
     assert m is m2
     assert m.dubl == 1
コード例 #8
0
    def test_basics(self):
        m = self.populate_MessageOfTypes()
        d = protobuf_to_dict(m)
        self.compare(m, d, ['nestedRepeated'])

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2
コード例 #9
0
 def test_strict(self):
     m = self.populate_MessageOfTypes()
     d = protobuf_to_dict(m)
     d['meow'] = 1
     with nose.tools.assert_raises(KeyError):
         m2 = dict_to_protobuf(MessageOfTypes, d)
     m2 = dict_to_protobuf(MessageOfTypes, d, strict=False)
     assert m == m2
コード例 #10
0
 def test_strict(self):
     m = self.populate_MessageOfTypes()
     d = protobuf_to_dict(m)
     d['meow'] = 1
     with nose.tools.assert_raises(KeyError):
         m2 = dict_to_protobuf(MessageOfTypes, d)
     m2 = dict_to_protobuf(MessageOfTypes, d, strict=False)
     assert m == m2
コード例 #11
0
    def test_nested_repeated(self):
        m = self.populate_MessageOfTypes()
        m.nestedRepeated.extend([MessageOfTypes.NestedType(req=str(i)) for i in range(10)])

        d = protobuf_to_dict(m)
        self.compare(m, d, exclude=['nestedRepeated'])
        assert d['nestedRepeated'] == [{'req': str(i)} for i in range(10)]

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2
コード例 #12
0
    def test_nested_repeated(self):
        m = self.populate_MessageOfTypes()
        m.nestedRepeated.extend(
            [MessageOfTypes.NestedType(req=str(i)) for i in range(10)])

        d = protobuf_to_dict(m)
        self.compare(m, d, exclude=['nestedRepeated'])
        assert d['nestedRepeated'] == [{'req': str(i)} for i in range(10)]

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2
コード例 #13
0
    def test_repeated_enum(self):
        m = self.populate_MessageOfTypes()
        d = protobuf_to_dict(m, use_enum_labels=True)
        self.compare(m, d, ['enm', 'enmRepeated', 'nestedRepeated'])
        assert d['enmRepeated'] == ['A', 'C']

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2

        d['enmRepeated'] = ['MEOW']
        with nose.tools.assert_raises(KeyError):
            dict_to_protobuf(MessageOfTypes, d)
コード例 #14
0
    def test_repeated_enum(self):
        m = self.populate_MessageOfTypes()
        d = protobuf_to_dict(m, use_enum_labels=True)
        self.compare(m, d, ['enm', 'enmRepeated', 'nestedRepeated'])
        assert d['enmRepeated'] == ['A', 'C']

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2

        d['enmRepeated'] = ['MEOW']
        with nose.tools.assert_raises(KeyError):
            dict_to_protobuf(MessageOfTypes, d)
コード例 #15
0
    def test_full_extensions(self):
        m = MessageOfTypes()

        primitives = {
            extDouble: 123.4,
            extString: "string",
            NestedExtension.extInt: 4
        }

        for key, value in primitives.items():
            m.Extensions[key] = value
        m.Extensions[NestedExtension.extNested].req = "nested"

        # Confirm compatibility with JSON serialization
        res = json.loads(
            json.dumps(protobuf_to_dict(m, extensions=FULL_EXTENSIONS)))
        expected = {
            u'tests.extDouble': 123.4,
            u'tests.NestedExtension.extInt': 4,
            u'tests.NestedExtension.extNested': {
                u'req': u'nested'
            },
            u'tests.extString': u'string'
        }
        self.assertEqual(expected, res)
        self.assertEqual(
            set(res.keys()),
            set([
                str(f.full_name) for f, _ in m.ListFields() if f.is_extension
            ]))
        for key, value in primitives.items():
            assert res[key.full_name] == value
        assert res[str(NestedExtension.extNested.full_name)]['req'] == 'nested'

        deser = dict_to_protobuf(MessageOfTypes, res)
        assert deser
        for key, value in primitives.items():
            assert deser.Extensions[key] == m.Extensions[key]
        assert deser.Extensions[NestedExtension.extNested].req == m.Extensions[
            NestedExtension.extNested].req
コード例 #16
0
    def test_extensions(self):
        m = MessageOfTypes()

        primitives = {
            extDouble: 123.4,
            extString: "string",
            NestedExtension.extInt: 4
        }

        for key, value in primitives.items():
            m.Extensions[key] = value
        m.Extensions[NestedExtension.extNested].req = "nested"

        # Confirm compatibility with JSON serialization
        res = json.loads(json.dumps(protobuf_to_dict(m)))
        expected = {
            u'___X': {
                u'100': 123.4,
                u'101': u'string',
                u'102': 4,
                u'103': {
                    u'req': u'nested'
                }
            }
        }
        self.assertEqual(expected, res)
        exts = res['___X']
        assert set(exts.keys()) == set(
            [str(f.number) for f, _ in m.ListFields() if f.is_extension])
        for key, value in primitives.items():
            assert exts[str(key.number)] == value
        assert exts[str(NestedExtension.extNested.number)]['req'] == 'nested'

        deser = dict_to_protobuf(MessageOfTypes, res)
        assert deser
        for key, value in primitives.items():
            assert deser.Extensions[key] == m.Extensions[key]
        assert deser.Extensions[NestedExtension.extNested].req == m.Extensions[
            NestedExtension.extNested].req
コード例 #17
0
 def test_reverse(self):
     m = self.populate_MessageOfTypes()
     m2 = dict_to_protobuf(MessageOfTypes, protobuf_to_dict(m))
     assert m == m2
     m2.dubl = 0
     assert m2 != m
コード例 #18
0
import json, os
import Proto import Tweet_pb2 as LogDataProto
import protobuf_to_dict import protobuf_to_dict

# text to json file
# input
output = os.popen('dir').read()
print(output)
filename=input("filename: ") # proto type

# read protobuf file
f = open(filename, "r")
try:
    logfile = LogDataProto.LogDataFile()
    logfile.ParseFromString(f.read())
except (IndexError, TypeError):
    print('index, type error')


# protobuf to json
logfile_dict = protobuf_to_dict(logfile)
json_string = json.dumps(logfile_dict, encoding='utf-8')

# write json file
new_filename = os.path.splittext(filename)[0] + '.json'
f = open(new_filename, 'w')
f.write(json_string.encode("utf-8"))
コード例 #19
0
 def test_reverse(self):
     m = self.populate_MessageOfTypes()
     m2 = dict_to_protobuf(MessageOfTypes, protobuf_to_dict(m))
     assert m == m2
     m2.dubl = 0
     assert m2 != m