ImportSpy.py :  » Web-Frameworks » Webware » Webware-1.0.2 » WebKit » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » WebKit » ImportSpy.py
"""ImportSpy

Keeps track of modules not imported directly by Webware for Python.

This module helps save the filepath of every module which is imported.
This is used by the `AutoReloadingAppServer` (see doc strings for more
information) to restart the server if any source files change.

Other than keeping track of the filepaths, the behaviour of this module
loader is identical to Python's default behaviour.

If the system supports FAM (file alteration monitor) and python-fam is
installed, then the need for reloading can be monitored very effectively
with the use of ImportSpy. Otherwise, ImportSpy will not have much benefit.

Note that ImportSpy is based on the new import hooks of Python 2.3 described in
PEP 302, falling back to the old ihooks module if new hooks are not available.
In some cases this may become problematic, when other templating systems are
used with Webware which are also using ihook support to load their templates,
or if they are using zipimports. Therefore, it is possible to suppress the use
of ImportSpy by setting `UseImportSpy` in AppServer.config to False.

"""


try: # if possible, use new (PEP 302) import hooks
  from sys import path_hooks,path_importer_cache
except ImportError:
  path_hooks = None


if path_hooks is not None:

  from os.path import isdir


  class ImportSpy(object):
    """New style import tracker."""

    _imp = None

    def __init__(self, path=None):
      """Create importer."""
      assert self._imp
      if path and isdir(path):
        self.path = path
      else:
        raise ImportError

    def find_module(self, fullname):
      """Replaces imp.find_module."""
      try:
        self.file, self.filename, self.info = self._imp.find_module(
          fullname.split('.')[-1], [self.path])
      except ImportError:
        pass
      else:
        return self

    def load_module(self, fullname):
      """Replaces imp.load_module."""
      mod = self._imp.load_module(fullname, self.file, self.filename, self.info)
      if mod:
        mod.__loader__ = self
      return mod

  def activate(impManager):
    """Activate ImportSpy."""
    assert not ImportSpy._imp
    ImportSpy._imp = impManager
    path_hooks.append(ImportSpy)
    path_importer_cache.clear()
    impManager.recordModules()
    return 'new import hooks'


else: # Python < 2.3, fall back to using the old ihooks module

  import ihooks


  class ImportSpy(ihooks.ModuleLoader):
    """Old style import tracker."""

    _imp = None

    def __init__(self):
      """Create import hook."""
      assert self._imp
      ihooks.ModuleLoader.__init__(self)
      self._lock = self._imp._lock
      imp = ihooks.ModuleImporter(loader=self)
      ihooks.install(imp)
      self._imp.recordModules()

    def load_module(self, name, stuff):
      """Replaces imp.load_module."""
      file, filename, info = stuff
      try:
        try:
          self._lock.acquire()
          mod = ihooks.ModuleLoader.load_module(self, name, stuff)
        finally:
          self._lock.release()
        self._imp.recordModule(mod)
      except:
        self._imp.recordFile(filename)
        raise
      return mod

  def activate(impManager):
    """Activate ImportSpy."""
    assert not ImportSpy._imp
    ImportSpy._imp = impManager
    ImportSpy()
    return 'ihooks'
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.