Skip to content

Commit 6c5a8d5

Browse files
author
gabriel pettier
committed
cleanup of osc.py - cont.
1 parent 090b3b5 commit 6c5a8d5

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

kivy/lib/osc/OSC.py

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -93,38 +93,38 @@ def rawAppend(self, data):
9393

9494
def getBinary(self):
9595
"""Returns the binary message (so far) with typetags."""
96-
address = OSCArgument(self.address)[1]
96+
address = OSCArgument(self.address)[1]
9797
typetags = OSCArgument(self.typetags)[1]
9898
return address + typetags + self.message
9999

100100
def __repr__(self):
101101
return self.getBinary()
102102

103+
103104
def readString(data):
104-
length = string.find(data,"\0")
105+
length = data.find("\0")
105106
nextData = int(math.ceil((length+1) / 4.0) * 4)
106107
return (data[0:length], data[nextData:])
107108

108109

109110
def readBlob(data):
110-
length = struct.unpack(">i", data[0:4])[0]
111+
length = struct.unpack(">i", data[0:4])[0]
111112
nextData = int(math.ceil((length) / 4.0) * 4) + 4
112113
return (data[4:length+4], data[nextData:])
113114

114115

115116
def readInt(data):
116-
if(len(data)<4):
117+
if(len(data) < 4):
117118
print("Error: too few bytes for int", data, len(data))
118119
rest = data
119120
integer = 0
120121
else:
121122
integer = struct.unpack(">i", data[0:4])[0]
122-
rest = data[4:]
123+
rest = data[4:]
123124

124125
return (integer, rest)
125126

126127

127-
128128
def readLong(data):
129129
"""Tries to interpret the next 8 bytes of the data
130130
as a 64-bit signed integer."""
@@ -143,30 +143,29 @@ def readDouble(data):
143143
return (big, rest)
144144

145145

146-
147146
def readFloat(data):
148-
if(len(data)<4):
147+
if(len(data) < 4):
149148
print("Error: too few bytes for float", data, len(data))
150149
rest = data
151150
float = 0
152151
else:
153152
float = struct.unpack(">f", data[0:4])[0]
154-
rest = data[4:]
153+
rest = data[4:]
155154

156155
return (float, rest)
157156

158157

159-
def OSCBlob(next):
158+
def OSCBlob(data):
160159
"""Convert a string into an OSC Blob,
161160
returning a (typetag, data) tuple."""
162161

163-
if type(next) == type(""):
164-
length = len(next)
165-
padded = math.ceil((len(next)) / 4.0) * 4
166-
binary = struct.pack(">i%ds" % (padded), length, next)
167-
tag = 'b'
162+
if isinstance(data, string_types):
163+
length = len(data)
164+
padded = math.ceil((len(data)) / 4.0) * 4
165+
binary = struct.pack(">i%ds" % (padded), length, data)
166+
tag = 'b'
168167
else:
169-
tag = ''
168+
tag = ''
170169
binary = ''
171170

172171
return (tag, binary)
@@ -215,10 +214,12 @@ def parseArgs(args):
215214
return parsed
216215

217216

218-
219217
def decodeOSC(data):
220218
"""Converts a typetagged OSC message to a Python list."""
221-
table = { "i" : readInt, "f" : readFloat, "s" : readString, "b" : readBlob, "d" : readDouble }
219+
table = {
220+
"i": readInt, "f": readFloat, "s": readString, "b": readBlob,
221+
"d": readDouble
222+
}
222223
decoded = []
223224
address, rest = readString(data)
224225
typetags = ""
@@ -227,7 +228,7 @@ def decodeOSC(data):
227228
time, rest = readLong(rest)
228229
# decoded.append(address)
229230
# decoded.append(time)
230-
while len(rest)>0:
231+
while len(rest) > 0:
231232
length, rest = readInt(rest)
232233
decoded.append(decodeOSC(rest[:length]))
233234
rest = rest[length:]
@@ -257,19 +258,19 @@ def __init__(self):
257258
self.callbacks = {}
258259
self.add(self.unbundler, "#bundle")
259260

260-
def handle(self, data, source = None):
261+
def handle(self, data, source=None):
261262
"""Given OSC data, tries to call the callback with the
262263
right address."""
263264
decoded = decodeOSC(data)
264265
self.dispatch(decoded, source)
265266

266-
def dispatch(self, message, source = None):
267+
def dispatch(self, message, source=None):
267268
"""Sends decoded OSC data to an appropriate calback"""
268-
if type(message[0]) == list :
269+
if isinstance(message[0], list):
269270
# smells like nested messages
270-
for msg in message :
271+
for msg in message:
271272
self.dispatch(msg, source)
272-
elif type(message[0]) == str :
273+
elif isinstance(message[0], string_types):
273274
# got a single message
274275
try:
275276
address = message[0]
@@ -297,7 +298,7 @@ def add(self, callback, name):
297298
"""Adds a callback to our set of callbacks,
298299
or removes the callback with name if callback
299300
is None."""
300-
if callback == None:
301+
if callback is None:
301302
del self.callbacks[name]
302303
else:
303304
self.callbacks[name] = callback
@@ -308,13 +309,6 @@ def unbundler(self, messages):
308309
for message in messages[2:]:
309310
self.dispatch(message)
310311

311-
312-
313-
314-
315-
316-
317-
318312
if __name__ == "__main__":
319313
hexDump("Welcome to the OSC testing program.")
320314
print()
@@ -337,7 +331,7 @@ def unbundler(self, messages):
337331
strings.append(14.5)
338332
strings.append(-400)
339333

340-
raw = strings.getBinary()
334+
raw = strings.getBinary()
341335

342336
hexDump(raw)
343337

@@ -363,12 +357,12 @@ def unbundler(self, messages):
363357
print("Testing Blob types.")
364358

365359
blob = OSCMessage()
366-
blob.append("","b")
367-
blob.append("b","b")
368-
blob.append("bl","b")
369-
blob.append("blo","b")
370-
blob.append("blob","b")
371-
blob.append("blobs","b")
360+
blob.append("", "b")
361+
blob.append("b", "b")
362+
blob.append("bl", "b")
363+
blob.append("blo", "b")
364+
blob.append("blob", "b")
365+
blob.append("blobs", "b")
372366
blob.append(42)
373367

374368
hexDump(blob.getBinary())

0 commit comments

Comments
 (0)