forked from heremaps/flatdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
149 lines (128 loc) · 3.62 KB
/
common.py
File metadata and controls
149 lines (128 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""Common testing helpers & data"""
from flatdata.lib.errors import MissingResourceError
INSTANCE_TEST_SCHEMA = """
namespace backward_compatibility {
struct SignedStruct {
a : i16 : 5;
b : u32 : 32;
c : i32 : 7;
d : u32 : 32;
}
archive Archive {
resource: SignedStruct;
}
}
"""
RESOURCE_PAYLOAD = (
b"\x0a\x00\x00\x00\x00\x00\x00\x00" # size of payload in bytes
b"\xff\xac\x68\x24\x00\x0b\x00\x00" # Payload
b"\x00\x00" # Payload
b"\x00\x00\x00\x00\x00\x00\x00\x00" # Padding
)
ARCHIVE_SIGNATURE_PAYLOAD = (
b"\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00"
)
MULTI_NAMESPACE_TEST_SCHEMA = """
namespace backward_compatibility {
struct SignedStruct {
a : i16 : 5;
b : u32 : 32;
c : i32 : 7;
d : u32 : 32;
}
archive Archive {
resource: SignedStruct;
}
}
namespace second {
}
"""
VECTOR_TEST_SCHEMA = """
namespace backward_compatibility {
struct SignedStruct {
a : i16 : 5;
b : u32 : 32;
c : i32 : 7;
d : u32 : 32;
}
archive Archive {
resource: vector< SignedStruct >;
}
}
"""
RESOURCE_VECTOR_PAYLOAD = (
b"\x14\x00\x00\x00\x00\x00\x00\x00" # Payload size in bytes
b"\xff\xac\x68\x24\x00\x0b\x00\x00" # Payload
b"\x00\x00\xff\xac\x68\x24\x00\x0b" # Payload
b"\x00\x00\x00\x00" # Payload
b"\x00\x00\x00\x00\x00\x00\x00\x00" # Padding
)
MULTIVECTOR_TEST_SCHEMA = """
namespace backward_compatibility {
struct SimpleStruct {
a : u32 : 32;
b : u32 : 32;
}
struct SignedStruct {
a : i16 : 5;
b : u32 : 32;
c : i32 : 7;
d : u32 : 32;
}
archive Archive {
resource: multivector< 33, SimpleStruct, SignedStruct >;
}
}
"""
MULTIVECTOR_RESOURCE_DATA = (
b"\x31\x00\x00\x00\x00\x00\x00\x00" # Payload size in bytes
b"\x01\xff\xac\x68\x24\x00\x0b\x00\x00\x00\x00" # Payload
b"\x00\xff\xff\xff\xff\xef\xbe\xad\xde" # Payload
b"\x00\xff\xff\xff\xff\xef\xbe\xad\xde" # Payload
b"\x01\xff\xac\x68\x24\x00\x0b\x00\x00\x00\x00" # Payload
b"\x00\xff\xff\xff\xff\xef\xbe\xad\xde" # Payload
b"\x00\x00\x00\x00\x00\x00\x00\x00" # Padding
)
MULTIVECTOR_RESOURCE_INDEX = (
b"\x19\x00\x00\x00\x00\x00\x00\x00" # Index size in bytes
b"\x00\x00\x00\x00\x00" # Data pointer 1
b"\x14\x00\x00\x00\x00" # Data pointer 2
b"\x14\x00\x00\x00\x00" # Data pointer 3
b"\x28\x00\x00\x00\x00" # Data pointer 4
b"\x31\x00\x00\x00\x00" # Sentinel (end of data 4)
b"\x00\x00\x00\x00\x00\x00\x00\x00" # Padding
)
RAW_DATA_TEST_SCHEMA = """
namespace backward_compatibility {
archive Archive {
resource: raw_data;
}
}
"""
RAW_DATA_RESOURCE_DATA = (
b"\x05\x00\x00\x00\x00\x00\x00\x00" # Payload size in bytes
b"\xff\xef\xbe\xad\xde" # Payload
b"\x00\x00\x00\x00\x00\x00\x00\x00" # Padding
)
# pylint: disable=too-few-public-methods,missing-docstring
class DictResourceStorage:
"""
Resource storage based on dict.
"""
def __init__(self, data=None):
self.data = data if data is not None else dict()
def get(self, key, is_optional=False):
if key not in self.data:
if not is_optional:
raise MissingResourceError(key)
else:
return None
value = self.data[key]
if isinstance(value, bytes):
class _Monkey(bytes):
def read(self):
return self
return _Monkey(value)
assert isinstance(value, dict)
return DictResourceStorage(data=value)