home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from peak.util.imports import lazyModule
- sys = lazyModule('sys')
- warnings = lazyModule('warnings')
- locale = lazyModule('locale')
- collections = lazyModule('collections')
- urllib = lazyModule('urllib')
- urllib2 = lazyModule('urllib2')
- codecs = lazyModule('codecs')
- StringIO = lazyModule('StringIO')
- zipfile = lazyModule('zipfile')
- gzip = lazyModule('gzip')
- htmlentitydefs = lazyModule('htmlentitydefs')
- base64 = lazyModule('base64')
- HAVE_LZMA = False
- ENCODE_LZMA = False
- __simplechars_enc = {
- ord('<'): 'lt',
- ord('>'): 'gt',
- ord('"'): 'quot',
- ord('&'): 'amp' }
- __simplechars_dec = dict((lambda .0: for k, v in .0:
- (v, unichr(k)))(__simplechars_enc.items()))
- __simplechars_dec['apos'] = unichr(ord("'"))
- _encodings = [
- (lambda : locale.getpreferredencoding()),
- (lambda : sys.getfilesystemencoding()),
- (lambda : sys.getdefaultencoding())]
- _to_register = []
-
- def register_codec(name, encode, decode):
-
- def _search(n):
- if n == name:
- return codecs.CodecInfo(name = name, encode = encode, decode = decode)
-
- _to_register.append(_search)
-
-
- def install():
- to_register = _to_register[:]
- _to_register[:] = []
- for codec in to_register:
- codecs.register(codec)
-
-
-
- def fuzzydecode(s, encoding = None, errors = 'strict'):
- if isinstance(s, unicode):
- import warnings
- warnings.warn('decoding unicode is not supported!')
- return s
- encodings = [ enc() for enc in _encodings ]
- if isinstance(encoding, basestring):
- encodings.insert(0, encoding)
- elif encoding is None:
- pass
- else:
- encodings = list(encoding) + encodings
- for e in encodings:
-
- try:
- res = s.decode(e, errors)
- except (UnicodeDecodeError, LookupError):
- isinstance(s, unicode)
- _ex = isinstance(s, unicode)
- import warnings
- warnings.warn('Exception when fuzzydecoding %r: %r' % (s, _ex))
- continue
-
- return res
-
- return s.decode(encoding, 'replace')
-
-
- def fuzzyencode(s, errors = 'strict'):
- raise NotImplementedError
-
-
- def _xml_encode(input, errors = 'simple'):
- simple = 'simple' in errors
- origtype = type(input)
- if simple:
- chars = __simplechars_enc
- else:
- chars = htmlentitydefs.codepoint2name
- res = []
- append = res.append
- for ch in input:
- och = ord(ch)
- if och in chars:
- append('&%s;' % chars[och])
- continue
- append(ch)
-
- return (origtype(''.join(res)), len(input))
-
-
- def _xml_decode(input, errors = 'strict'):
- data = collections.deque(input)
- res = []
- append = res.append
- popleft = data.popleft
- extendleft = data.extendleft
- name2codepoint = htmlentitydefs.name2codepoint
- while data:
- ch = popleft()
- if ch == '&':
- curtoken = ''
- is_ref = False
- is_num = False
- is_hex = False
- while len(curtoken) < 10 and data:
- nch = popleft()
- if nch == '#':
- is_num = True
-
- if is_num and len(curtoken) == 1 and nch == 'x':
- is_hex = True
-
- if nch == ';':
- is_ref = True
- break
-
- curtoken += nch
- if nch == '&':
- break
- continue
- if not is_ref:
- extendleft(reversed(curtoken))
- append('&')
- continue
- elif is_num:
-
- try:
- curtoken = curtoken[1:]
- if is_hex:
- curtoken = curtoken[1:]
- och = int(curtoken, 16)
- else:
- och = int(curtoken, 10)
- append(unichr(och))
- continue
- except (UnicodeError, ValueError, TypeError):
- pass
- except:
- None<EXCEPTION MATCH>(UnicodeError, ValueError, TypeError)
-
-
-
- if curtoken in name2codepoint:
- append(unichr(name2codepoint[curtoken]))
- elif curtoken in __simplechars_dec:
- append(__simplechars_dec[curtoken])
- else:
- append('&%s;' % curtoken)
- curtoken in name2codepoint
- append(ch)
- return (u''.join(res), len(input))
-
- register_codec('xml', _xml_encode, _xml_decode)
-
- def _pk_decode(input, errors = 'strict'):
- li = len(input)
- input = StringIO.StringIO(input)
- z = zipfile.ZipFile(input, mode = 'r')
- zi = z.filelist[0]
- return (z.read(zi.filename), li)
-
-
- def _pk_encode(input, errors = 'strict'):
- li = len(input)
- s = StringIO.StringIO()
- z = zipfile.ZipFile(s, mode = 'wb', compression = zipfile.ZIP_DEFLATED)
- z.writestr('file', input)
- z.close()
- return (s.getvalue(), li)
-
-
- def _gzip_decode(input, errors = 'strict'):
- li = len(input)
- input = StringIO.StringIO(input)
- g = gzip.GzipFile(mode = 'rb', fileobj = input)
- return (g.read(), li)
-
-
- def _gzip_encode(input, errors = 'strict'):
- li = len(input)
- s = StringIO.StringIO()
- g = gzip.GzipFile(mode = 'wb', fileobj = s)
- g.write(input)
- g.close()
- return (s.getvalue(), li)
-
-
- def search(name):
- if name == 'gzip':
- name = 'gzip'
- return codecs.CodecInfo(name = name, encode = _gzip_encode, decode = _gzip_decode)
-
- _to_register.append(search)
- del search
-
- def _fuzzyzip_decode(input, errors = 'strict'):
- global HAVE_LZMA
- magic_num = input[:2]
- if magic_num == 'PK':
- return _pk_decode(input, errors = errors)
- if magic_num == 'BZ':
- return (input.decode('bz2'), len(input))
- if magic_num == '\x1f\x8b':
- return _gzip_decode(input, errors = errors)
- if HAVE_LZMA:
-
- try:
- return (pylzma.decompress(input), len(input))
- except ImportError:
- magic_num == '\x1f\x8b'
- magic_num == '\x1f\x8b'
- magic_num == 'BZ'
- HAVE_LZMA = False
- except Exception:
- magic_num == '\x1f\x8b'
- magic_num == '\x1f\x8b'
- magic_num == '\x1f\x8b'
- except:
- magic_num == '\x1f\x8b'<EXCEPTION MATCH>ImportError
-
-
- magic_num == '\x1f\x8b'
- return (input.decode('zip'), len(input))
-
-
- def _fuzzyzip_encode(input, errors = 'strict'):
- li = len(input)
- funcs = [
- ((lambda : input.encode('bz2')),),
- (lambda : input.encode('zlib'))]
- if HAVE_LZMA and ENCODE_LZMA:
- (funcs.append,)((lambda : pylzma.compress(input)))
-
- shortest_val = None
- shortest_len = -1
- for func in funcs:
- newval = func()
- newlen = len(newval)
- if shortest_len < 0 or newlen < shortest_len:
- shortest_len = newlen
- shortest_val = newval
- continue
-
- return (shortest_val, li)
-
-
- def search(name):
- if name == 'z' or name == 'fuzzyzip':
- name = 'fuzzyzip'
- return codecs.CodecInfo(name = 'fuzzyzip', encode = _fuzzyzip_encode, decode = _fuzzyzip_decode)
-
- _to_register.append(search)
- del search
-
- def search(name):
- if name.startswith('fuzzy'):
- if name == 'fuzzyzip':
- return None
- if not name[len('fuzzy'):]:
- pass
- encoding = None
- elif name.endswith('?'):
- if not name[:-1]:
- pass
- encoding = None
- else:
- return None
- name = name == 'fuzzyzip'
- encode = fuzzyencode
-
- def decode(s, errors = ('strict',)):
- if encoding:
- encs = filter(bool, encoding.split())
- else:
- encs = None
- return (fuzzydecode(s, encs, errors), len(s))
-
- return codecs.CodecInfo(name = name, encode = encode, decode = decode)
-
- _to_register.append(search)
- del search
-
- __locale_encoding = lambda : locale.getpreferredencoding()
- register_codec('locale', (lambda s, errors = 'strict': (s.encode(__locale_encoding()), len(s))), (lambda s, errors = 'strict': (s.decode(__locale_encoding()), len(s))))
-
- __filesysencoding = lambda : sys.getfilesystemencoding()
-
- def _filesys_encode(s, errors = 'strict'):
- if isinstance(s, str):
- return (s, len(s))
- return (s.encode(__filesysencoding()), len(s))
-
-
- def _filesys_decode(s, errors = 'strict'):
- if isinstance(s, unicode):
- return (s, len(s))
- return (s.decode(__filesysencoding()), len(s))
-
- register_codec('filesys', _filesys_encode, _filesys_decode)
- del _filesys_encode
- del _filesys_decode
-
- def _url_encode(input, errors = 'strict'):
- return (urllib2.quote(input), len(input))
-
-
- def _url_decode(input, errors = 'strict'):
- return (urllib.unquote_plus(input), len(input))
-
- register_codec('url', _url_encode, _url_decode)
-
- def _utf8url_encode(input, errors = 'strict'):
- output = input.encode('utf-8', errors)
- output = urllib2.quote(output)
- return (output, len(input))
-
-
- def _utf8url_decode(input, errors = 'strict'):
- output = input.encode('ascii', errors)
- output = urllib.unquote_plus(output)
- output = output.decode('utf-8', errors)
- return (output, len(input))
-
- register_codec('utf8url', _utf8url_encode, _utf8url_decode)
- b64_codecs = { }
- b64_names = frozenset(('b64', 'b32', 'b16'))
-
- def make_funcs(encode, decode):
-
- def _encode(input, errors = ('strict',)):
- return (encode(input), len(input))
-
-
- def _decode(input, errors = ('strict',)):
- return (decode(input), len(input))
-
- return dict(encode = _encode, decode = _decode)
-
-
- def search_base64(name):
- if name not in b64_names:
- return None
-
- try:
- return b64_codecs[name]
- except KeyError:
- name not in b64_names
- name not in b64_names
- if name == 'b64':
- codec = b64_codecs[name] = codecs.CodecInfo(name = name, **make_funcs(base64.b64encode, base64.b64decode))
-
- if name == 'b32':
- codec = b64_codecs[name] = codecs.CodecInfo(name = name, **make_funcs(base64.b32encode, base64.b32decode))
-
- if name == 'b16':
- codec = b64_codecs[name] = codecs.CodecInfo(name = name, **make_funcs(base64.b16encode, base64.b16decode))
-
- return codec
-
-
- _to_register.append(search_base64)
- del search_base64
- __all__ = []
- if __name__ == '__main__':
- install()
-
- def gen_rand_str(length = 5000):
- randint = randint
- randchoice = choice
- import random
- ascii_letters = ascii_letters
- import string
- ents = list(__simplechars_dec)
- data = []
- append = data.append
- for x in xrange(length):
- r = randint(0, 10)
- if r == 0:
- append('&%s;' % randchoice(ents))
- elif r == 1:
- append('&%d;' % randint(0, 65535))
- elif r == 2:
- append('&%x;' % randint(0, 65535))
-
- if r > 3:
- append(randchoice(ascii_letters))
- continue
-
- return ''.join(data)
-
- strings = [ gen_rand_str() for x in xrange(100) ]
- results1 = []
- results2 = []
- from time import clock
-
- def timeit(func):
- before = clock()
- func()
- return clock() - before
-
-
- def foo(encoding, res):
- for s in strings:
- res.append(s.decode(encoding))
-
-
- print 'xml', timeit((lambda : foo('xml', results1)))
- print 'xml2', timeit((lambda : foo('xml2', results2)))
-
-