Skip to content
This repository was archived by the owner on Apr 27, 2019. It is now read-only.

Commit 1c14b2c

Browse files
committed
[PEP8] Packets
1 parent 0d39435 commit 1c14b2c

2 files changed

Lines changed: 665 additions & 404 deletions

File tree

packets/data_types.py

Lines changed: 79 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import logging
2-
from construct import Construct, Struct, Byte, BFloat32, BFloat64, Flag, \
3-
String, Container, Field
2+
3+
from construct import (
4+
Construct,
5+
Struct,
6+
Byte,
7+
BFloat32,
8+
BFloat64,
9+
Flag,
10+
String,
11+
Container,
12+
Field
13+
)
414
from construct.core import _read_stream, _write_stream, Adapter
515

616

@@ -24,9 +34,9 @@ def _build(self, obj, stream, context):
2434
value = abs(obj * 2)
2535
if obj < 0:
2636
value -= 1
27-
VLQ("")._build(value, stream, context)
37+
VLQ('')._build(value, stream, context)
2838
except:
29-
self.logger.exception("Error building SignedVLQ.")
39+
self.logger.exception('Error building SignedVLQ.')
3040
raise
3141

3242

@@ -57,10 +67,11 @@ def _build(self, obj, stream, context):
5767
if len(result) > 1:
5868
result[0] |= 0x80
5969
result[-1] ^= 0x80
60-
_write_stream(stream, len(result), "".join([chr(x) for x in result]))
70+
_write_stream(stream, len(result), ''.join(map(chr, result)))
6171

6272

63-
star_string = lambda name="star_string": StarStringAdapter(star_string_struct(name))
73+
def star_string(name='star_string'):
74+
return StarStringAdapter(star_string_struct(name))
6475

6576

6677
class StarStringAdapter(Adapter):
@@ -70,65 +81,76 @@ def _encode(self, obj, context):
7081
def _decode(self, obj, context):
7182
return obj.string
7283

84+
7385
class Joiner(Adapter):
7486
def _encode(self, obj, context):
7587
return obj
88+
7689
def _decode(self, obj, context):
77-
return "".join(obj)
90+
return ''.join(obj)
91+
92+
93+
def star_string_struct(name='star_string'):
94+
return Struct(
95+
name,
96+
VLQ('length'),
97+
String('string', lambda ctx: ctx.length)
98+
)
7899

79-
star_string_struct = lambda name="star_string": Struct(name,
80-
VLQ("length"),
81-
String("string", lambda ctx: ctx.length))
82100

83101
class VariantVariant(Construct):
84102
def _parse(self, stream, context):
85-
l = VLQ("").parse_stream(stream)
86-
return [Variant("").parse_stream(stream) for _ in range(l)]
103+
l = VLQ('').parse_stream(stream)
104+
return [Variant('').parse_stream(stream) for _ in range(l)]
105+
87106

88107
class ChunkVariant(Construct):
89108
def _parse(self, stream, context):
90-
l = VLQ("").parse_stream(stream)
109+
l = VLQ('').parse_stream(stream)
91110
c = {}
92111
for x in range(l):
93-
junk1 = Byte("").parse_stream(stream)
94-
junk2 = Byte("").parse_stream(stream)
95-
junk3 = BFloat32("").parse_stream(stream)
96-
junk4 = Byte("").parse_stream(stream)
97-
junk5 = StarByteArray("").parse_stream(stream)
112+
junk1 = Byte('').parse_stream(stream)
113+
junk2 = Byte('').parse_stream(stream)
114+
junk3 = BFloat32('').parse_stream(stream)
115+
junk4 = Byte('').parse_stream(stream)
116+
junk5 = StarByteArray('').parse_stream(stream)
98117
return c
99118

119+
100120
class DictVariant(Construct):
101121
def _parse(self, stream, context):
102-
l = VLQ("").parse_stream(stream)
122+
l = VLQ('').parse_stream(stream)
103123
c = {}
104124
for x in range(l):
105-
key = star_string("").parse_stream(stream)
106-
value = Variant("").parse_stream(stream)
125+
key = star_string('').parse_stream(stream)
126+
value = Variant('').parse_stream(stream)
107127
c[key] = value
108128
return c
109129

130+
110131
class WarpVariant(Construct):
111-
# Not all variants have been properly treated!
132+
# Not all variants have been properly treated!
112133
def _parse(self, stream, context):
113-
x = Byte("").parse_stream(stream)
134+
x = Byte('').parse_stream(stream)
114135
if x == 0:
115136
return None
116137
elif x == 1:
117138
return star_string().parse_stream(stream)
118139
elif x == 2:
119140
return None
120141
elif x == 3:
121-
flag = Flag("").parse_stream(stream)
122-
return Field("", 16).parse_stream(stream).encode("hex")
142+
flag = Flag('').parse_stream(stream)
143+
return Field('', 16).parse_stream(stream).encode('hex')
123144
elif x == 4:
124145
return star_string().parse_stream(stream)
146+
125147
def _build(self, obj, stream, context):
126148
if len(obj) == 32:
127149
_write_stream(stream, 1, chr(3))
128150
_write_stream(stream, 1, chr(1))
129-
_write_stream(stream, len(obj.decode("hex")), obj.decode("hex"))
151+
_write_stream(stream, len(obj.decode('hex')), obj.decode('hex'))
130152
return
131-
elif obj is "outpost":
153+
elif obj is 'outpost':
132154
_write_stream(stream, 1, chr(1))
133155
star_string()._build(obj, stream, context)
134156
return
@@ -137,28 +159,40 @@ def _build(self, obj, stream, context):
137159
_write_stream(stream, 1, chr(0))
138160
return
139161

162+
140163
class Variant(Construct):
164+
RESPONSES = {
165+
2: BFloat64('').parse_stream,
166+
3: Flag('').parse_stream,
167+
4: SignedVLQ('').parse_stream,
168+
5: star_string().parse_stream,
169+
6: VariantVariant('').parse_stream,
170+
7: DictVariant('').parse_stream
171+
}
172+
141173
def _parse(self, stream, context):
142-
x = Byte("").parse_stream(stream)
143-
if x == 1:
144-
return None
145-
elif x == 2:
146-
return BFloat64("").parse_stream(stream)
147-
elif x == 3:
148-
return Flag("").parse_stream(stream)
149-
elif x == 4:
150-
return SignedVLQ("").parse_stream(stream)
151-
elif x == 5:
152-
return star_string().parse_stream(stream)
153-
elif x == 6:
154-
return VariantVariant("").parse_stream(stream)
155-
elif x == 7:
156-
return DictVariant("").parse_stream(stream)
174+
x = Byte('').parse_stream(stream)
175+
return self.RESPONSES.get(x, lambda x: None)(stream)
176+
# if x == 1:
177+
# return None
178+
# elif x == 2:
179+
# return BFloat64('').parse_stream(stream)
180+
# elif x == 3:
181+
# return Flag('').parse_stream(stream)
182+
# elif x == 4:
183+
# return SignedVLQ('').parse_stream(stream)
184+
# elif x == 5:
185+
# return star_string().parse_stream(stream)
186+
# elif x == 6:
187+
# return VariantVariant('').parse_stream(stream)
188+
# elif x == 7:
189+
# return DictVariant('').parse_stream(stream)
190+
157191

158192
class StarByteArray(Construct):
159193
def _parse(self, stream, context):
160-
l = VLQ("").parse_stream(stream)
194+
l = VLQ('').parse_stream(stream)
161195
return _read_stream(stream, l)
162-
def _build(self, obj, stream, context):
163-
_write_stream(stream, len(obj), VLQ("").build(len(obj))+obj)
164196

197+
def _build(self, obj, stream, context):
198+
_write_stream(stream, len(obj), VLQ('').build(len(obj))+obj)

0 commit comments

Comments
 (0)