home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / IHOOKS.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-05  |  25.8 KB  |  634 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Import hook support.
  5.  
  6. Consistent use of this module will make it possible to change the
  7. different mechanisms involved in loading modules independently.
  8.  
  9. While the built-in module imp exports interfaces to the built-in
  10. module searching and loading algorithm, and it is possible to replace
  11. the built-in function __import__ in order to change the semantics of
  12. the import statement, until now it has been difficult to combine the
  13. effect of different __import__ hacks, like loading modules from URLs
  14. by rimport.py, or restricted execution by rexec.py.
  15.  
  16. This module defines three new concepts:
  17.  
  18. 1) A "file system hooks" class provides an interface to a filesystem.
  19.  
  20. One hooks class is defined (Hooks), which uses the interface provided
  21. by standard modules os and os.path.  It should be used as the base
  22. class for other hooks classes.
  23.  
  24. 2) A "module loader" class provides an interface to to search for a
  25. module in a search path and to load it.  It defines a method which
  26. searches for a module in a single directory; by overriding this method
  27. one can redefine the details of the search.  If the directory is None,
  28. built-in and frozen modules are searched instead.
  29.  
  30. Two module loader class are defined, both implementing the search
  31. strategy used by the built-in __import__ function: ModuleLoader uses
  32. the imp module\'s find_module interface, while HookableModuleLoader
  33. uses a file system hooks class to interact with the file system.  Both
  34. use the imp module\'s load_* interfaces to actually load the module.
  35.  
  36. 3) A "module importer" class provides an interface to import a
  37. module, as well as interfaces to reload and unload a module.  It also
  38. provides interfaces to install and uninstall itself instead of the
  39. default __import__ and reload (and unload) functions.
  40.  
  41. One module importer class is defined (ModuleImporter), which uses a
  42. module loader instance passed in (by default HookableModuleLoader is
  43. instantiated).
  44.  
  45. The classes defined here should be used as base classes for extended
  46. functionality along those lines.
  47.  
  48. If a module mporter class supports dotted names, its import_module()
  49. must return a different value depending on whether it is called on
  50. behalf of a "from ... import ..." statement or not.  (This is caused
  51. by the way the __import__ hook is used by the Python interpreter.)  It
  52. would also do wise to install a different version of reload().
  53.  
  54. '''
  55. import __builtin__
  56. import imp
  57. import os
  58. import sys
  59. import string
  60. VERBOSE = 0
  61. PY_COMPILED
  62. PKG_DIRECTORY
  63. BUILTIN_MODULE = C_BUILTIN
  64. FROZEN_MODULE = PY_FROZEN
  65.  
  66. class _Verbose:
  67.     
  68.     def __init__(self, verbose = VERBOSE):
  69.         self.verbose = verbose
  70.  
  71.     
  72.     def get_verbose(self):
  73.         return self.verbose
  74.  
  75.     
  76.     def set_verbose(self, verbose):
  77.         self.verbose = verbose
  78.  
  79.     
  80.     def note(self, *args):
  81.         if self.verbose:
  82.             apply(self.message, args)
  83.         
  84.  
  85.     
  86.     def message(self, format, *args):
  87.         if args:
  88.             print format % args
  89.         else:
  90.             print format
  91.  
  92.  
  93.  
  94. class BasicModuleLoader(_Verbose):
  95.     """Basic module loader.
  96.  
  97.     This provides the same functionality as built-in import.  It
  98.     doesn't deal with checking sys.modules -- all it provides is
  99.     find_module() and a load_module(), as well as find_module_in_dir()
  100.     which searches just one directory, and can be overridden by a
  101.     derived class to change the module search algorithm when the basic
  102.     dependency on sys.path is unchanged.
  103.  
  104.     The interface is a little more convenient than imp's:
  105.     find_module(name, [path]) returns None or 'stuff', and
  106.     load_module(name, stuff) loads the module.
  107.  
  108.     """
  109.     
  110.     def find_module(self, name, path = None):
  111.         if path is None:
  112.             path = [
  113.                 None] + self.default_path()
  114.         
  115.         for dir in path:
  116.             stuff = self.find_module_in_dir(name, dir)
  117.         
  118.         return None
  119.  
  120.     
  121.     def default_path(self):
  122.         return sys.path
  123.  
  124.     
  125.     def find_module_in_dir(self, name, dir):
  126.         if dir is None:
  127.             return self.find_builtin_module(name)
  128.         else:
  129.             
  130.             try:
  131.                 return imp.find_module(name, [
  132.                     dir])
  133.             except ImportError:
  134.                 return None
  135.  
  136.  
  137.     
  138.     def find_builtin_module(self, name):
  139.         if imp.is_builtin(name):
  140.             return (None, '', ('', '', BUILTIN_MODULE))
  141.         
  142.         if imp.is_frozen(name):
  143.             return (None, '', ('', '', FROZEN_MODULE))
  144.         
  145.         return None
  146.  
  147.     
  148.     def load_module(self, name, stuff):
  149.         (file, filename, info) = stuff
  150.         
  151.         try:
  152.             return imp.load_module(name, file, filename, info)
  153.         finally:
  154.             if file:
  155.                 file.close()
  156.             
  157.  
  158.  
  159.  
  160.  
  161. class Hooks(_Verbose):
  162.     '''Hooks into the filesystem and interpreter.
  163.  
  164.     By deriving a subclass you can redefine your filesystem interface,
  165.     e.g. to merge it with the URL space.
  166.  
  167.     This base class behaves just like the native filesystem.
  168.  
  169.     '''
  170.     
  171.     def get_suffixes(self):
  172.         return imp.get_suffixes()
  173.  
  174.     
  175.     def new_module(self, name):
  176.         return imp.new_module(name)
  177.  
  178.     
  179.     def is_builtin(self, name):
  180.         return imp.is_builtin(name)
  181.  
  182.     
  183.     def init_builtin(self, name):
  184.         return imp.init_builtin(name)
  185.  
  186.     
  187.     def is_frozen(self, name):
  188.         return imp.is_frozen(name)
  189.  
  190.     
  191.     def init_frozen(self, name):
  192.         return imp.init_frozen(name)
  193.  
  194.     
  195.     def get_frozen_object(self, name):
  196.         return imp.get_frozen_object(name)
  197.  
  198.     
  199.     def load_source(self, name, filename, file = None):
  200.         return imp.load_source(name, filename, file)
  201.  
  202.     
  203.     def load_compiled(self, name, filename, file = None):
  204.         return imp.load_compiled(name, filename, file)
  205.  
  206.     
  207.     def load_dynamic(self, name, filename, file = None):
  208.         return imp.load_dynamic(name, filename, file)
  209.  
  210.     
  211.     def load_package(self, name, filename, file = None):
  212.         return imp.load_module(name, file, filename, ('', '', PKG_DIRECTORY))
  213.  
  214.     
  215.     def add_module(self, name):
  216.         d = self.modules_dict()
  217.         if d.has_key(name):
  218.             return d[name]
  219.         
  220.         d[name] = m = self.new_module(name)
  221.         return m
  222.  
  223.     
  224.     def modules_dict(self):
  225.         return sys.modules
  226.  
  227.     
  228.     def default_path(self):
  229.         return sys.path
  230.  
  231.     
  232.     def path_split(self, x):
  233.         return os.path.split(x)
  234.  
  235.     
  236.     def path_join(self, x, y):
  237.         return os.path.join(x, y)
  238.  
  239.     
  240.     def path_isabs(self, x):
  241.         return os.path.isabs(x)
  242.  
  243.     
  244.     def path_exists(self, x):
  245.         return os.path.exists(x)
  246.  
  247.     
  248.     def path_isdir(self, x):
  249.         return os.path.isdir(x)
  250.  
  251.     
  252.     def path_isfile(self, x):
  253.         return os.path.isfile(x)
  254.  
  255.     
  256.     def path_islink(self, x):
  257.         return os.path.islink(x)
  258.  
  259.     
  260.     def openfile(self, *x):
  261.         return apply(open, x)
  262.  
  263.     openfile_error = IOError
  264.     
  265.     def listdir(self, x):
  266.         return os.listdir(x)
  267.  
  268.     listdir_error = os.error
  269.  
  270.  
  271. class ModuleLoader(BasicModuleLoader):
  272.     """Default module loader; uses file system hooks.
  273.  
  274.     By defining suitable hooks, you might be able to load modules from
  275.     other sources than the file system, e.g. from compressed or
  276.     encrypted files, tar files or (if you're brave!) URLs.
  277.  
  278.     """
  279.     
  280.     def __init__(self, hooks = None, verbose = VERBOSE):
  281.         BasicModuleLoader.__init__(self, verbose)
  282.         if not hooks:
  283.             pass
  284.         self.hooks = Hooks(verbose)
  285.  
  286.     
  287.     def default_path(self):
  288.         return self.hooks.default_path()
  289.  
  290.     
  291.     def modules_dict(self):
  292.         return self.hooks.modules_dict()
  293.  
  294.     
  295.     def get_hooks(self):
  296.         return self.hooks
  297.  
  298.     
  299.     def set_hooks(self, hooks):
  300.         self.hooks = hooks
  301.  
  302.     
  303.     def find_builtin_module(self, name):
  304.         if self.hooks.is_builtin(name):
  305.             return (None, '', ('', '', BUILTIN_MODULE))
  306.         
  307.         if self.hooks.is_frozen(name):
  308.             return (None, '', ('', '', FROZEN_MODULE))
  309.         
  310.         return None
  311.  
  312.     
  313.     def find_module_in_dir(self, name, dir, allow_packages = 1):
  314.         if dir is None:
  315.             return self.find_builtin_module(name)
  316.         
  317.         if allow_packages:
  318.             fullname = self.hooks.path_join(dir, name)
  319.             if self.hooks.path_isdir(fullname):
  320.                 stuff = self.find_module_in_dir('__init__', fullname, 0)
  321.                 if stuff:
  322.                     file = stuff[0]
  323.                     if file:
  324.                         file.close()
  325.                     
  326.                     return (None, fullname, ('', '', PKG_DIRECTORY))
  327.                 
  328.             
  329.         
  330.         for info in self.hooks.get_suffixes():
  331.             (suff, mode, type) = info
  332.             fullname = self.hooks.path_join(dir, name + suff)
  333.             
  334.             try:
  335.                 fp = self.hooks.openfile(fullname, mode)
  336.                 return (fp, fullname, info)
  337.             except self.hooks.openfile_error:
  338.                 0
  339.                 0
  340.                 self.hooks.get_suffixes()
  341.             except:
  342.                 0
  343.  
  344.         
  345.         return None
  346.  
  347.     
  348.     def load_module(self, name, stuff):
  349.         (file, filename, info) = stuff
  350.         (suff, mode, type) = info
  351.         
  352.         try:
  353.             if type == BUILTIN_MODULE:
  354.                 return self.hooks.init_builtin(name)
  355.             
  356.             if type == FROZEN_MODULE:
  357.                 return self.hooks.init_frozen(name)
  358.             
  359.             if type == C_EXTENSION:
  360.                 m = self.hooks.load_dynamic(name, filename, file)
  361.             elif type == PY_SOURCE:
  362.                 m = self.hooks.load_source(name, filename, file)
  363.             elif type == PY_COMPILED:
  364.                 m = self.hooks.load_compiled(name, filename, file)
  365.             elif type == PKG_DIRECTORY:
  366.                 m = self.hooks.load_package(name, filename, file)
  367.             else:
  368.                 raise ImportError, 'Unrecognized module type (%s) for %s' % (`type`, name)
  369.         finally:
  370.             if file:
  371.                 file.close()
  372.             
  373.  
  374.         m.__file__ = filename
  375.         return m
  376.  
  377.  
  378.  
  379. class FancyModuleLoader(ModuleLoader):
  380.     '''Fancy module loader -- parses and execs the code itself.'''
  381.     
  382.     def load_module(self, name, stuff):
  383.         (suff, mode, type) = (file, filename)
  384.         realfilename = filename
  385.         path = None
  386.         if type == FROZEN_MODULE:
  387.             code = self.hooks.get_frozen_object(name)
  388.         elif type == PY_COMPILED:
  389.             import marshal
  390.             file.seek(8)
  391.             code = marshal.load(file)
  392.         elif type == PY_SOURCE:
  393.             data = file.read()
  394.             code = compile(data, realfilename, 'exec')
  395.         else:
  396.             return ModuleLoader.load_module(self, name, stuff)
  397.         m = self.hooks.add_module(name)
  398.         if path:
  399.             m.__path__ = path
  400.         
  401.         m.__file__ = filename
  402.         exec code in m.__dict__
  403.         return m
  404.  
  405.  
  406.  
  407. class BasicModuleImporter(_Verbose):
  408.     '''Basic module importer; uses module loader.
  409.  
  410.     This provides basic import facilities but no package imports.
  411.  
  412.     '''
  413.     
  414.     def __init__(self, loader = None, verbose = VERBOSE):
  415.         _Verbose.__init__(self, verbose)
  416.         if not loader:
  417.             pass
  418.         self.loader = ModuleLoader(None, verbose)
  419.         self.modules = self.loader.modules_dict()
  420.  
  421.     
  422.     def get_loader(self):
  423.         return self.loader
  424.  
  425.     
  426.     def set_loader(self, loader):
  427.         self.loader = loader
  428.  
  429.     
  430.     def get_hooks(self):
  431.         return self.loader.get_hooks()
  432.  
  433.     
  434.     def set_hooks(self, hooks):
  435.         return self.loader.set_hooks(hooks)
  436.  
  437.     
  438.     def import_module(self, name, globals = { }, locals = { }, fromlist = []):
  439.         if self.modules.has_key(name):
  440.             return self.modules[name]
  441.         
  442.         stuff = self.loader.find_module(name)
  443.         if not stuff:
  444.             raise ImportError, 'No module named %s' % name
  445.         
  446.         return self.loader.load_module(name, stuff)
  447.  
  448.     
  449.     def reload(self, module, path = None):
  450.         name = module.__name__
  451.         stuff = self.loader.find_module(name, path)
  452.         if not stuff:
  453.             raise ImportError, 'Module %s not found for reload' % name
  454.         
  455.         return self.loader.load_module(name, stuff)
  456.  
  457.     
  458.     def unload(self, module):
  459.         del self.modules[module.__name__]
  460.  
  461.     
  462.     def install(self):
  463.         self.save_import_module = __builtin__.__import__
  464.         self.save_reload = __builtin__.reload
  465.         if not hasattr(__builtin__, 'unload'):
  466.             __builtin__.unload = None
  467.         
  468.         self.save_unload = __builtin__.unload
  469.         __builtin__.__import__ = self.import_module
  470.         __builtin__.reload = self.reload
  471.         __builtin__.unload = self.unload
  472.  
  473.     
  474.     def uninstall(self):
  475.         __builtin__.__import__ = self.save_import_module
  476.         __builtin__.reload = self.save_reload
  477.         __builtin__.unload = self.save_unload
  478.         if not (__builtin__.unload):
  479.             del __builtin__.unload
  480.         
  481.  
  482.  
  483.  
  484. class ModuleImporter(BasicModuleImporter):
  485.     '''A module importer that supports packages.'''
  486.     
  487.     def import_module(self, name, globals = None, locals = None, fromlist = None):
  488.         parent = self.determine_parent(globals)
  489.         (q, tail) = self.find_head_package(parent, name)
  490.         m = self.load_tail(q, tail)
  491.         if not fromlist:
  492.             return q
  493.         
  494.         if hasattr(m, '__path__'):
  495.             self.ensure_fromlist(m, fromlist)
  496.         
  497.         return m
  498.  
  499.     
  500.     def determine_parent(self, globals):
  501.         if not globals or not globals.has_key('__name__'):
  502.             return None
  503.         
  504.         pname = globals['__name__']
  505.         if globals.has_key('__path__'):
  506.             parent = self.modules[pname]
  507.             if not __debug__ and globals is parent.__dict__:
  508.                 raise AssertionError
  509.             return parent
  510.         
  511.         if '.' in pname:
  512.             i = string.rfind(pname, '.')
  513.             pname = pname[:i]
  514.             parent = self.modules[pname]
  515.             if not __debug__ and parent.__name__ == pname:
  516.                 raise AssertionError
  517.             return parent
  518.         
  519.         return None
  520.  
  521.     
  522.     def find_head_package(self, parent, name):
  523.         if '.' in name:
  524.             i = string.find(name, '.')
  525.             head = name[:i]
  526.             tail = name[i + 1:]
  527.         else:
  528.             head = name
  529.             tail = ''
  530.         if parent:
  531.             qname = '%s.%s' % (parent.__name__, head)
  532.         else:
  533.             qname = head
  534.         q = self.import_it(head, qname, parent)
  535.         if q:
  536.             return (q, tail)
  537.         
  538.         if parent:
  539.             qname = head
  540.             parent = None
  541.             q = self.import_it(head, qname, parent)
  542.             if q:
  543.                 return (q, tail)
  544.             
  545.         
  546.         raise ImportError, 'No module named ' + qname
  547.  
  548.     
  549.     def load_tail(self, q, tail):
  550.         m = q
  551.         while tail:
  552.             i = string.find(tail, '.')
  553.             if i < 0:
  554.                 i = len(tail)
  555.             
  556.             (head, tail) = (tail[:i], tail[i + 1:])
  557.             mname = '%s.%s' % (m.__name__, head)
  558.             m = self.import_it(head, mname, m)
  559.             if not m:
  560.                 raise ImportError, 'No module named ' + mname
  561.             
  562.         return m
  563.  
  564.     
  565.     def ensure_fromlist(self, m, fromlist, recursive = 0):
  566.         for sub in fromlist:
  567.             if sub == '*':
  568.                 continue
  569.             
  570.             if sub != '*' and not hasattr(m, sub):
  571.                 subname = '%s.%s' % (m.__name__, sub)
  572.                 submod = self.import_it(sub, subname, m)
  573.                 if not submod:
  574.                     raise ImportError, 'No module named ' + subname
  575.                 
  576.             
  577.         
  578.  
  579.     
  580.     def import_it(self, partname, fqname, parent):
  581.         if not partname:
  582.             raise ValueError, 'Empty module name'
  583.         
  584.         
  585.         try:
  586.             return self.modules[fqname]
  587.         except KeyError:
  588.             pass
  589.  
  590.         
  591.         try:
  592.             if parent:
  593.                 pass
  594.             path = parent.__path__
  595.         except AttributeError:
  596.             return None
  597.  
  598.         stuff = self.loader.find_module(partname, path)
  599.         if not stuff:
  600.             return None
  601.         
  602.         m = self.loader.load_module(fqname, stuff)
  603.         if parent:
  604.             setattr(parent, partname, m)
  605.         
  606.         return m
  607.  
  608.     
  609.     def reload(self, module):
  610.         name = module.__name__
  611.         if '.' not in name:
  612.             return self.import_it(name, name, None)
  613.         
  614.         i = string.rfind(name, '.')
  615.         pname = name[:i]
  616.         parent = self.modules[pname]
  617.         return self.import_it(name[i + 1:], name, parent)
  618.  
  619.  
  620. default_importer = None
  621. current_importer = None
  622.  
  623. def install(importer = None):
  624.     global current_importer
  625.     if not importer and default_importer:
  626.         pass
  627.     current_importer = ModuleImporter()
  628.     current_importer.install()
  629.  
  630.  
  631. def uninstall():
  632.     current_importer.uninstall()
  633.  
  634.