home *** CD-ROM | disk | FTP | other *** search
/ Freelog 116 / FreelogNo116-JuilletSeptembre2013.iso / Bureautique / gImageReader / gimagereader_0.9-1_win32.exe / bin / encodings / __init__.pyc (.txt) next >
Python Compiled Bytecode  |  2011-03-24  |  4KB  |  130 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. ''' Standard "encodings" Package
  5.  
  6.     Standard Python encoding modules are stored in this package
  7.     directory.
  8.  
  9.     Codec modules must have names corresponding to normalized encoding
  10.     names as defined in the normalize_encoding() function below, e.g.
  11.     \'utf-8\' must be implemented by the module \'utf_8.py\'.
  12.  
  13.     Each codec module must export the following interface:
  14.  
  15.     * getregentry() -> codecs.CodecInfo object
  16.     The getregentry() API must a CodecInfo object with encoder, decoder,
  17.     incrementalencoder, incrementaldecoder, streamwriter and streamreader
  18.     atttributes which adhere to the Python Codec Interface Standard.
  19.  
  20.     In addition, a module may optionally also define the following
  21.     APIs which are then used by the package\'s codec search function:
  22.  
  23.     * getaliases() -> sequence of encoding name strings to use as aliases
  24.  
  25.     Alias names returned by getaliases() must be normalized encoding
  26.     names as defined by normalize_encoding().
  27.  
  28. Written by Marc-Andre Lemburg (mal@lemburg.com).
  29.  
  30. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  31.  
  32. '''
  33. import codecs
  34. from encodings import aliases
  35. import __builtin__
  36. _cache = { }
  37. _unknown = '--unknown--'
  38. _import_tail = [
  39.     '*']
  40. _norm_encoding_map = '                                              . 0123456789       ABCDEFGHIJKLMNOPQRSTUVWXYZ      abcdefghijklmnopqrstuvwxyz                                                                                                                                     '
  41. _aliases = aliases.aliases
  42.  
  43. class CodecRegistryError(LookupError, SystemError):
  44.     pass
  45.  
  46.  
  47. def normalize_encoding(encoding):
  48.     """ Normalize an encoding name.
  49.  
  50.         Normalization works as follows: all non-alphanumeric
  51.         characters except the dot used for Python package names are
  52.         collapsed and replaced with a single underscore, e.g. '  -;#'
  53.         becomes '_'. Leading and trailing underscores are removed.
  54.  
  55.         Note that encoding names should be ASCII only; if they do use
  56.         non-ASCII characters, these must be Latin-1 compatible.
  57.  
  58.     """
  59.     if hasattr(__builtin__, 'unicode') and isinstance(encoding, unicode):
  60.         encoding = encoding.encode('latin-1')
  61.     return '_'.join(encoding.translate(_norm_encoding_map).split())
  62.  
  63.  
  64. def search_function(encoding):
  65.     entry = _cache.get(encoding, _unknown)
  66.     if entry is not _unknown:
  67.         return entry
  68.     norm_encoding = None(encoding)
  69.     if not _aliases.get(norm_encoding):
  70.         pass
  71.     aliased_encoding = _aliases.get(norm_encoding.replace('.', '_'))
  72.     if aliased_encoding is not None:
  73.         modnames = [
  74.             aliased_encoding,
  75.             norm_encoding]
  76.     else:
  77.         modnames = [
  78.             norm_encoding]
  79.     for modname in modnames:
  80.         if not modname or '.' in modname:
  81.             continue
  82.         
  83.         try:
  84.             mod = __import__('encodings.' + modname, fromlist = _import_tail, level = 0)
  85.         except ImportError:
  86.             continue
  87.  
  88.         break
  89.     else:
  90.         mod = None
  91.     
  92.     try:
  93.         getregentry = mod.getregentry
  94.     except AttributeError:
  95.         mod = None
  96.  
  97.     if mod is None:
  98.         _cache[encoding] = None
  99.         return None
  100.     entry = None()
  101.     if not isinstance(entry, codecs.CodecInfo):
  102.         if len(entry) <= len(entry):
  103.             pass
  104.         elif not len(entry) <= 7:
  105.             raise CodecRegistryError, 'module "%s" (%s) failed to register' % (mod.__name__, mod.__file__)
  106.         if not not hasattr(entry[0], '__call__') and not hasattr(entry[1], '__call__'):
  107.             if not entry[2] is not None or not hasattr(entry[2], '__call__'):
  108.                 if not entry[3] is not None or not hasattr(entry[3], '__call__'):
  109.                     if (len(entry) > 4 and entry[4] is not None or not hasattr(entry[4], '__call__') or len(entry) > 5) and entry[5] is not None and not hasattr(entry[5], '__call__'):
  110.                         raise CodecRegistryError, 'incompatible codecs in module "%s" (%s)' % (mod.__name__, mod.__file__)
  111.                     if len(entry) < 7 or entry[6] is None:
  112.                         entry += (None,) * (6 - len(entry)) + (mod.__name__.split('.', 1)[1],)
  113.                     entry = codecs.CodecInfo(*entry)
  114.                 _cache[encoding] = entry
  115.                 
  116.                 try:
  117.                     codecaliases = mod.getaliases()
  118.                 except AttributeError:
  119.                     len(entry) <= 7
  120.                     len(entry) <= 7
  121.                     4
  122.  
  123.                 for alias in codecaliases:
  124.                     if alias not in _aliases:
  125.                         _aliases[alias] = modname
  126.                         continue
  127.                 return entry
  128.  
  129. codecs.register(search_function)
  130.