home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / encodings / __init__.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.3 KB  |  121 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  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.     
  62.     return '_'.join(encoding.translate(_norm_encoding_map).split())
  63.  
  64.  
  65. def search_function(encoding):
  66.     entry = _cache.get(encoding, _unknown)
  67.     if entry is not _unknown:
  68.         return entry
  69.     norm_encoding = normalize_encoding(encoding)
  70.     if not _aliases.get(norm_encoding):
  71.         pass
  72.     aliased_encoding = _aliases.get(norm_encoding.replace('.', '_'))
  73.     if aliased_encoding is not None:
  74.         modnames = [
  75.             aliased_encoding,
  76.             norm_encoding]
  77.     else:
  78.         modnames = [
  79.             norm_encoding]
  80.     for modname in modnames:
  81.         if not modname or '.' in modname:
  82.             continue
  83.         
  84.         
  85.         try:
  86.             mod = __import__('encodings.' + modname, fromlist = _import_tail, level = 0)
  87.         except ImportError:
  88.             continue
  89.  
  90.     else:
  91.         mod = None
  92.     
  93.     try:
  94.         getregentry = mod.getregentry
  95.     except AttributeError:
  96.         mod = None
  97.  
  98.     if mod is None:
  99.         _cache[encoding] = None
  100.         return None
  101.     entry = getregentry()
  102.     _cache[encoding] = entry
  103.     
  104.     try:
  105.         codecaliases = mod.getaliases()
  106.     except AttributeError:
  107.         None if not isinstance(entry, codecs.CodecInfo) else mod is None
  108.         None if not isinstance(entry, codecs.CodecInfo) else mod is None
  109.     except:
  110.         None if not isinstance(entry, codecs.CodecInfo) else mod is None
  111.  
  112.     for alias in codecaliases:
  113.         if not _aliases.has_key(alias):
  114.             _aliases[alias] = modname
  115.             continue
  116.         None if not isinstance(entry, codecs.CodecInfo) else mod is None
  117.     
  118.     return entry
  119.  
  120. codecs.register(search_function)
  121.