4 def _bytehex2(num, ord=ord, chr=chr, ord_0=ord(
'0'), ord_a=ord(
'a')):
8 return chr(num+ord_a-10)
10 def _bytehex(num, int=int, divmod=divmod, bytehex2=_bytehex2):
11 q,r=divmod(int(num),16)
12 return bytehex2(q%16)+bytehex2(r)
14 bytehex = map(_bytehex, xrange(256))
17 def addSlashes(m,forbidden="#!|\\?\"\'\r\n",extended_forbidden=1, bytehex=bytehex):
21 quote = (extended_forbidden
and ord(m_i)>=128) \
25 if not rv
and i: rva( m[:i] )
27 rva( bytehex[ord(m_i)%256] )
30 return rv
and "".
join(rv)
or m
32 def _hexbyte2(c, ord=ord, ord_0=ord(
'0'), ord_9=ord(
'9'), ord_a=ord(
'a')):
34 if ( ord_c>=ord_0
and ord_c<=ord_9 ):
37 return 10 + ord_c - ord_a
39 def _hexbyte(s, hexbyte2=_hexbyte2):
40 return ( hexbyte2(s[0])*16+hexbyte2(s[1]) ) % 256
50 if (m[i]==
'\\')
and (i+2<l):
51 rva( chr( hexbyte(m[i+1:i+3]) ) )
59 if type(m)
is DictionaryType:
62 _addSlashes = addSlashes
63 _encodeMap = encodeMap
64 for k,v
in m.iteritems():
66 rva( _addSlashes(
str(k)) +
"#" + _encodeMap(v) )
77 ipair = ilist[0].
split(
'#')
86 _stripSlashes = stripSlashes
87 _decodeMap = decodeMap
89 ipair = ipair.split(
'#')
91 rv[_stripSlashes(ipair[0])] =
decodeMap(ipair[1])