home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / __init__.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  2.8 KB  |  87 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  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 standard lower-case
  10.     encoding names with hyphens mapped to underscores, e.g. \'utf-8\' is
  11.     implemented by the module \'utf_8.py\'.
  12.  
  13.     Each codec module must export the following interface:
  14.  
  15.     * getregentry() -> (encoder, decoder, stream_reader, stream_writer)
  16.     The getregentry() API must return callable objects which adhere to
  17.     the Python Codec Interface Standard.
  18.  
  19.     In addition, a module may optionally also define the following
  20.     APIs which are then used by the package\'s codec search function:
  21.  
  22.     * getaliases() -> sequence of encoding name strings to use as aliases
  23.  
  24.     Alias names returned by getaliases() must be standard encoding
  25.     names as defined above (lower-case, hyphens converted to
  26.     underscores).
  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. import aliases
  35. import exceptions
  36. _cache = { }
  37. _unknown = '--unknown--'
  38.  
  39. class CodecRegistryError(exceptions.LookupError, exceptions.SystemError):
  40.     pass
  41.  
  42.  
  43. def search_function(encoding):
  44.     entry = _cache.get(encoding, _unknown)
  45.     if entry is not _unknown:
  46.         return entry
  47.     
  48.     modname = encoding.replace('-', '_')
  49.     modname = aliases.aliases.get(modname, modname)
  50.     
  51.     try:
  52.         mod = __import__(modname, globals(), locals(), '*')
  53.     except ImportError:
  54.         why = None
  55.         _cache[encoding] = None
  56.         return None
  57.  
  58.     
  59.     try:
  60.         entry = tuple(mod.getregentry())
  61.     except AttributeError:
  62.         entry = ()
  63.  
  64.     if len(entry) != 4:
  65.         raise CodecRegistryError, 'module "%s" (%s) failed to register' % (mod.__name__, mod.__file__)
  66.     
  67.     for obj in entry:
  68.         if not callable(obj):
  69.             raise CodecRegistryError, 'incompatible codecs in module "%s" (%s)' % (mod.__name__, mod.__file__)
  70.         
  71.     
  72.     _cache[encoding] = entry
  73.     
  74.     try:
  75.         codecaliases = mod.getaliases()
  76.     except AttributeError:
  77.         pass
  78.  
  79.     for alias in codecaliases:
  80.         if not aliases.aliases.has_key(alias):
  81.             aliases.aliases[alias] = modname
  82.         
  83.     
  84.     return entry
  85.  
  86. codecs.register(search_function)
  87.