freemyipod r582 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r581‎ | r582 | r583 >
Date:21:31, 11 February 2011
Author:farthen
Status:new
Tags:
Comment:
emCORE tools: Partial Python 3 support. Some commands don't work yet.
Modified paths:
  • /emcore/trunk/tools/emcore.py (modified) (history)
  • /emcore/trunk/tools/libemcore.py (modified) (history)
  • /emcore/trunk/tools/misc.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -93,7 +93,7 @@
9494 to all functions decorated with @command
9595 """
9696 cls.cmddict = {}
97 - for attr, value in cls.__dict__.iteritems():
 97+ for attr, value in cls.__dict__.items():
9898 if getattr(value, 'func', False):
9999 if getattr(value.func, '_command', False):
100100 cls.cmddict[value.func.__name__] = value
@@ -125,17 +125,17 @@
126126 if func in self.cmddict:
127127 try:
128128 self.cmddict[func](*args)
129 - except (ArgumentError, libemcore.ArgumentError), e:
 129+ except (ArgumentError, libemcore.ArgumentError) as e:
130130 usage(e, specific=func)
131131 except (ArgumentError, libemcore.ArgumentError):
132132 usage("Syntax Error in function '" + func + "'", specific=func)
133 - except ArgumentTypeError, e:
 133+ except ArgumentTypeError as e:
134134 usage(e, specific=func)
135135 except NotImplementedError:
136136 self.logger.error("This function is not implemented yet!\n")
137 - except libemcore.DeviceError, e:
 137+ except libemcore.DeviceError as e:
138138 self.logger.error(str(e) + "\n")
139 - except TypeError, e:
 139+ except TypeError as e:
140140 # Only act on TypeErrors for the function we called, not on TypeErrors raised by another function.
141141 if str(e).split(" ", 1)[0] == func + "()":
142142 self.logger.error(usage("Argument Error in '%s': Wrong argument count" % func, specific=func))
Index: emcore/trunk/tools/misc.py
@@ -250,7 +250,9 @@
251251 # and split into a list of lines:
252252 lines = docstring.expandtabs().splitlines()
253253 # Determine minimum indentation (first line doesn't count):
254 - indent = sys.maxint
 254+ try: maxsize = sys.maxint
 255+ except AttributeError: maxsize = sys.maxsize
 256+ indent = maxsize
255257 for line in lines[1:]:
256258 stripped = line.lstrip()
257259 if stripped:
@@ -257,7 +259,7 @@
258260 indent = min(indent, len(line) - len(stripped))
259261 # Remove indentation (first line is special):
260262 trimmed = [lines[0].strip()]
261 - if indent < sys.maxint:
 263+ if indent < maxsize:
262264 for line in lines[1:]:
263265 trimmed.append(line[indent:].rstrip())
264266 # Strip off trailing and leading blank lines:
@@ -343,6 +345,8 @@
344346 """
345347 Converts quite everything into bool.
346348 """
 349+ try: long
 350+ except NameError: long = int
347351 if type(something) == bool:
348352 return something
349353 if something is None:
@@ -362,6 +366,8 @@
363367 This works for default arguments too, because it returns
364368 None when it found that it got a NoneType object.
365369 """
 370+ try: long
 371+ except NameError: long = int
366372 if type(something) == int or type(something) == long:
367373 return something
368374 elif something is None:
@@ -377,4 +383,12 @@
378384 except ValueError:
379385 raise ArgumentTypeError("integer", "'%s'" % something)
380386 else:
381 - raise ArgumentTypeError("integer", "'%s'" % something)
\ No newline at end of file
 387+ raise ArgumentTypeError("integer", "'%s'" % something)
 388+
 389+
 390+def majorver():
 391+ """
 392+ Returns the major version of python
 393+ """
 394+ import sys
 395+ return sys.hexversion // 0x1000000
\ No newline at end of file
Index: emcore/trunk/tools/libemcore.py
@@ -30,6 +30,7 @@
3131 import struct
3232 import ctypes
3333 import usb.core
 34+import base64
3435
3536 from libemcoredata import *
3637 from misc import Logger, Bunch, Error, ArgumentError, gethwname
@@ -222,7 +223,7 @@
223224 """
224225 cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
225226 din_maxsize = self.lib.dev.packetsizelimit.din
226 - data = ""
 227+ data = b""
227228 (headsize, bodysize, tailsize) = self._alignsplit(addr, size, cin_maxsize, 16)
228229 self.logger.debug("Downloading %d bytes from 0x%X, split as (%d/%d/%d)\n" % (size, addr, headsize, bodysize, tailsize))
229230 if headsize != 0:
@@ -288,12 +289,12 @@
289290 string = ""
290291 while (len(string) < maxlength or maxlength < 0):
291292 data = self._readmem(addr, min(maxlength - len(string), cin_maxsize))
292 - length = data.find("\0")
 293+ length = data.find(b"\0")
293294 if length >= 0:
294 - string += data[:length]
 295+ string += data[:length].decode("latin_1")
295296 break
296297 else:
297 - string += data
 298+ string += data.decode("latin_1")
298299 addr += cin_maxsize
299300 return string
300301
@@ -300,7 +301,7 @@
301302 @command()
302303 def i2cread(self, index, slaveaddr, startaddr, size):
303304 """ Reads data from an i2c slave """
304 - data = ""
 305+ data = b""
305306 for i in range(size):
306307 resp = self.lib.monitorcommand(struct.pack("<IBBBBII", 8, index, slaveaddr, startaddr + i, 1, 0, 0), "III1s", (None, None, None, "data"))
307308 data += resp.data
@@ -318,10 +319,10 @@
319320
320321 @command()
321322 def usbcread(self):
322 - """ Reads one packet with the maximal cin size """
 323+ """ Reads one packet with the maximal cin size from the console """
323324 cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
324325 resp = self.lib.monitorcommand(struct.pack("<IIII", 10, cin_maxsize, 0, 0), "III%ds" % cin_maxsize, ("validsize", "buffersize", "queuesize", "data"))
325 - resp.data = resp.data[:resp.validsize]
 326+ resp.data = resp.data[:resp.validsize].decode("latin_1")
326327 resp.maxsize = cin_maxsize
327328 return resp
328329
@@ -918,7 +919,7 @@
919920 self.connected = True
920921
921922 def monitorcommand(self, cmd, rcvdatatypes=None, rcvstruct=None):
922 - self.logger.debug("Sending monitorcommand [0x%s]\n" % cmd[3::-1].encode("hex"))
 923+ self.logger.debug("Sending monitorcommand [0x%s]\n" % base64.b16encode(cmd[3::-1]).decode("ascii"))
923924 writelen = self.dev.cout(cmd)
924925 if rcvdatatypes:
925926 rcvdatatypes = "I" + rcvdatatypes # add the response
@@ -1073,7 +1074,7 @@
10741075 from misc import gendoc
10751076 logger.write("Generating documentation\n")
10761077 cmddict = {}
1077 - for attr, value in Emcore.__dict__.iteritems():
 1078+ for attr, value in Emcore.__dict__.items():
10781079 if getattr(value, 'func', False):
10791080 if getattr(value.func, '_command', False):
10801081 cmddict[value.func.__name__] = value