From: <lu...@us...> - 2010-06-06 16:15:54
|
Revision: 439 http://pyscard.svn.sourceforge.net/pyscard/?rev=439&view=rev Author: ludov Date: 2010-06-06 16:15:48 +0000 (Sun, 06 Jun 2010) Log Message: ----------- make pep8 happy Modified Paths: -------------- trunk/pyscard/src/smartcard/util/__init__.py Modified: trunk/pyscard/src/smartcard/util/__init__.py =================================================================== --- trunk/pyscard/src/smartcard/util/__init__.py 2010-06-06 16:09:59 UTC (rev 438) +++ trunk/pyscard/src/smartcard/util/__init__.py 2010-06-06 16:15:48 UTC (rev 439) @@ -23,12 +23,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """ -PACK=1 -HEX=2 -UPPERCASE=4 -COMMA=8 +PACK = 1 +HEX = 2 +UPPERCASE = 4 +COMMA = 8 -def padd( bytelist, length, padding='FF' ): + +def padd(bytelist, length, padding='FF'): """ Padds a byte list with a constant byte value (default is x0FF) bytelist: the byte list to padd length: the total length of the resulting byte list; @@ -37,19 +38,19 @@ returns the padded bytelist example: - padd( toBytes(\"3B 65 00 00 9C 11 01 01 03\"), 16 ) returns [0x3B, 0x65, 0, 0, 0x9C, 0x11, 1, 1, 3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] - padd( toBytes(\"3B 65 00 00 9C 11 01 01 03\"), 8 ) returns [0x3B, 0x65, 0, 0, 0x9C, 0x11, 1, 1, 3 ] + padd(toBytes(\"3B 65 00 00 9C 11 01 01 03\"), 16) returns [0x3B, 0x65, 0, 0, 0x9C, 0x11, 1, 1, 3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] + padd(toBytes(\"3B 65 00 00 9C 11 01 01 03\"), 8) returns [0x3B, 0x65, 0, 0, 0x9C, 0x11, 1, 1, 3 ] """ - if len( bytelist ) < length: - for index in range(length-len(bytelist)): - bytelist.append(eval('0x'+padding)) + if len(bytelist) < length: + for index in range(length - len(bytelist)): + bytelist.append(eval('0x' + padding)) return bytelist -def toASCIIBytes( stringtoconvert ): +def toASCIIBytes(stringtoconvert): """Returns a list of ASCII bytes from a string. stringtoconvert: the string to convert into a byte list @@ -59,13 +60,13 @@ toASCIIBytes() is the reverse of toASCIIString() example: - toASCIIBytes( "Number 101" ) returns[ 0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0x20, 0x31, 0x30, 0x31 ] + toASCIIBytes("Number 101") returns[ 0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0x20, 0x31, 0x30, 0x31 ] """ - return map( ord, list(stringtoconvert) ) + return map(ord, list(stringtoconvert)) -def toASCIIString( bytelist ): +def toASCIIString(bytelist): """Returns a string representing a list of ASCII bytes. bytelist: list of ASCII bytes to convert into a string @@ -76,67 +77,70 @@ example: - toASCIIString( [ 0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0x20, 0x31, 0x30, 0x31 ] ) returns "Number 101" ) + toASCIIString([ 0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0x20, 0x31, 0x30, 0x31 ]) returns "Number 101") """ - return ''.join( map( chr, bytelist ) ) + return ''.join(map(chr, bytelist)) -def toBytes( bytestring ): + +def toBytes(bytestring): """Returns a list of bytes from a byte string bytestring: a byte string of the format \"3B 65 00 00 9C 11 01 01 03\" or \"3B6500009C11010103\" or \"3B6500 009C1101 0103\" """ from struct import unpack import re - packedstring = ''.join( re.split( '\W+', bytestring ) ) + packedstring = ''.join(re.split('\W+', bytestring)) try: - return reduce( lambda x, y: x+[int(y,16)], unpack( '2s' * (len(packedstring)/2) , packedstring ), [] ) + return reduce(lambda x, y: x + [int(y, 16)], unpack('2s' * (len(packedstring) / 2), packedstring), []) except: raise TypeError, 'not a string representing a list of bytes' """GSM3.38 character conversion table.""" __dic_GSM_3_38__ = { - '@':0x00, # @ At symbol - '\xA3':0x01, # \xA3 Britain pound symbol - '$':0x02, # $ Dollar symbol - chr(0xA5):0x03, # \xA5 Yen symbol - '\xE8':0x04, # \xE8 e accent grave - '\xE9':0x05, # \xE9 e accent aigu - '\xF9':0x06, # \xF9 u accent grave - chr(0xEC):0x07, # \xEC i accent grave - chr(0xF2):0x08, # \xF2 o accent grave - chr(0xC7):0x09, # \xC7 C majuscule cedille - chr(0x0A):0x0A, # LF Line Feed - chr(0xD8):0x0B, # \xD8 O majuscule barr\xE9 - chr(0xF8):0x0C, # \xF8 o minuscule barr\xE9 - chr(0x0D):0x0D, # CR Carriage Return - chr(0xC5):0x0E, # \xC5 Angstroem majuscule - chr(0xE5):0x0F, # \xE5 Angstroem minuscule - '_':0x11, # underscore - chr(0xC6):0x1C, # \xC6 majuscule ae - chr(0xE6):0x1D, # \xE6 minuscule ae - chr(0xDF):0x1E, # \xDF s dur allemand - chr(0xC9):0x1F, # \xC9 majuscule \xE9 - ' ':0x20, '!':0x21, - '\"':0x22, # guillemet - '#':0x23, - '\xA4':0x24, # \xA4 carr\xE9 - chr(0xA1):0x40, # \xA1 point d'exclamation renvers\xE9 - chr(0xC4):0x5B, # \xC4 majuscule A trema - chr(0xE4):0x7B, # \xE4 minuscule a trema - chr(0xD6):0x5C, # \xD6 majuscule O trema - chr(0xF6):0x7C, # \xF6 minuscule o trema - chr(0xD1):0x5D, # \xD1 majuscule N tilda espagnol - chr(0xF1):0x7D, # \xF1 minuscule n tilda espagnol - chr(0xDC):0x5E, # \xDC majuscule U trema - chr(0xFC):0x7E, # \xFC minuscule u trema - chr(0xA7):0x5F, # \xA7 signe paragraphe - chr(0xBF):0x60, # \xBF point interrogation renvers\xE9 - '\xE0':0x7F # a accent grave + '@': 0x00, # @ At symbol + '\xA3': 0x01, # \xA3 Britain pound symbol + '$': 0x02, # $ Dollar symbol + chr(0xA5): 0x03, # \xA5 Yen symbol + '\xE8': 0x04, # \xE8 e accent grave + '\xE9': 0x05, # \xE9 e accent aigu + '\xF9': 0x06, # \xF9 u accent grave + chr(0xEC): 0x07, # \xEC i accent grave + chr(0xF2): 0x08, # \xF2 o accent grave + chr(0xC7): 0x09, # \xC7 C majuscule cedille + chr(0x0A): 0x0A, # LF Line Feed + chr(0xD8): 0x0B, # \xD8 O majuscule barr\xE9 + chr(0xF8): 0x0C, # \xF8 o minuscule barr\xE9 + chr(0x0D): 0x0D, # CR Carriage Return + chr(0xC5): 0x0E, # \xC5 Angstroem majuscule + chr(0xE5): 0x0F, # \xE5 Angstroem minuscule + '_': 0x11, # underscore + chr(0xC6): 0x1C, # \xC6 majuscule ae + chr(0xE6): 0x1D, # \xE6 minuscule ae + chr(0xDF): 0x1E, # \xDF s dur allemand + chr(0xC9): 0x1F, # \xC9 majuscule \xE9 + ' ': 0x20, + '!': 0x21, + '\"': 0x22, # guillemet + '#': 0x23, + '\xA4': 0x24, # \xA4 carr\xE9 + chr(0xA1): 0x40, # \xA1 point d'exclamation renvers\xE9 + chr(0xC4): 0x5B, # \xC4 majuscule A trema + chr(0xE4): 0x7B, # \xE4 minuscule a trema + chr(0xD6): 0x5C, # \xD6 majuscule O trema + chr(0xF6): 0x7C, # \xF6 minuscule o trema + chr(0xD1): 0x5D, # \xD1 majuscule N tilda espagnol + chr(0xF1): 0x7D, # \xF1 minuscule n tilda espagnol + chr(0xDC): 0x5E, # \xDC majuscule U trema + chr(0xFC): 0x7E, # \xFC minuscule u trema + chr(0xA7): 0x5F, # \xA7 signe paragraphe + chr(0xBF): 0x60, # \xBF point interrogation renvers\xE9 + '\xE0': 0x7F # a accent grave } -def toGSM3_38Bytes( stringtoconvert ): + +def toGSM3_38Bytes(stringtoconvert): """Returns a list of bytes from a string using GSM 3.38 conversion table. stringtoconvert: string to convert @@ -144,10 +148,10 @@ returns a list of bytes example: - toGSM3_38Bytes( "@\xF9Pascal" ) returns [ 0x00, 0x06, 0x50, 0x61, 0x73, 0x63, 0x61, 0x6C ] + toGSM3_38Bytes("@\xF9Pascal") returns [ 0x00, 0x06, 0x50, 0x61, 0x73, 0x63, 0x61, 0x6C ] """ - bytes =[] + bytes = [] for char in stringtoconvert: if ((char >= "%") and (char <= "?")): bytes.append(ord(char)) @@ -160,7 +164,7 @@ return bytes -def toHexString( bytes=[], format=0 ): +def toHexString(bytes=[], format=0): """Returns an hex string representing bytes bytes: a list of bytes to stringify, e.g. [59, 22, 148, 32, 2, 1, 0, 0, 13] @@ -173,16 +177,16 @@ example: bytes = [ 0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03 ] - toHexString( bytes ) returns 3B 65 00 00 9C 11 01 01 03 + toHexString(bytes) returns 3B 65 00 00 9C 11 01 01 03 - toHexString( bytes, COMMA ) returns 3B, 65, 00, 00, 9C, 11, 01, 01, 03 - toHexString( bytes, HEX ) returns 0x3B 0x65 0x00 0x00 0x9C 0x11 0x01 0x01 0x03 - toHexString( bytes, HEX | COMMA ) returns 0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03 + toHexString(bytes, COMMA) returns 3B, 65, 00, 00, 9C, 11, 01, 01, 03 + toHexString(bytes, HEX) returns 0x3B 0x65 0x00 0x00 0x9C 0x11 0x01 0x01 0x03 + toHexString(bytes, HEX | COMMA) returns 0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03 - toHexString( bytes, PACK ) returns 3B6500009C11010103 + toHexString(bytes, PACK) returns 3B6500009C11010103 - toHexString( bytes, HEX | UPPERCASE ) returns 0X3B 0X65 0X00 0X00 0X9C 0X11 0X01 0X01 0X03 - toHexString( bytes, HEX | UPPERCASE | COMMA) returns 0X3B, 0X65, 0X00, 0X00, 0X9C, 0X11, 0X01, 0X01, 0X03 + toHexString(bytes, HEX | UPPERCASE) returns 0X3B 0X65 0X00 0X00 0X9C 0X11 0X01 0X01 0X03 + toHexString(bytes, HEX | UPPERCASE | COMMA) returns 0X3B, 0X65, 0X00, 0X00, 0X9C, 0X11, 0X01, 0X01, 0X03 """ from string import rstrip @@ -193,36 +197,40 @@ if type(bytes) is not list: raise TypeError, 'not a list of bytes' - if bytes==None or bytes==[]: + if bytes == None or bytes == []: return "" else: - pformat="%-0.2X" + pformat = "%-0.2X" if COMMA & format: - pformat = pformat+"," - pformat = pformat+" " + pformat = pformat + "," + pformat = pformat + " " if PACK & format: - pformat=rstrip( pformat ) + pformat = rstrip(pformat) if HEX & format: if UPPERCASE & format: - pformat = "0X"+pformat + pformat = "0X" + pformat else: - pformat = "0x"+pformat - return rstrip(rstrip(reduce( lambda a, b: a+pformat % ((b+256)%256), [""] + bytes )),',') + pformat = "0x" + pformat + return rstrip(rstrip(reduce(lambda a, b: a + pformat % ((b + 256) % 256), [""] + bytes)), ',') -def HexListToBinString( hexlist ): - binstring="" + +def HexListToBinString(hexlist): + binstring = "" for byte in hexlist: - binstring= binstring + chr( eval( '0x%x' % byte ) ) + binstring = binstring + chr(eval('0x%x' % byte)) return binstring -def BinStringToHexList( binstring ): - hexlist=[] + +def BinStringToHexList(binstring): + hexlist = [] for byte in binstring: - hexlist= hexlist + [ ord(byte) ] + hexlist = hexlist + [ord(byte)] return hexlist -def hl2bs( hexlist ): - return HexListToBinString( hexlist ) -def bs2hl( binstring ): - return BinStringToHexList( binstring ) +def hl2bs(hexlist): + return HexListToBinString(hexlist) + + +def bs2hl(binstring): + return BinStringToHexList(binstring) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |