[Announce] autoreload.py

Thomas Heller thomas.heller at ion-tof.com
Tue Apr 18 13:37:19 EDT 2000


> My newsreader seriously munged this.  Can you repost the code, not as
> an attachment?
Sure. Probably my fault...

-------- cut here --------
#
# autoreload.py - automatically reload changed source
# code into a running program
#
# You may want to place the following two lines
# into your sitecustomize.py:
#
# import autoreload
# autoreload.run()
#
# Thomas Heller, 2000-04-17
#
# python-style license
#

import sys, imp, __builtin__, string
import time, os
import threading
import types


#####################################################################
#
# Most of this file is copied (and hacked) from standard module knee
#
# The purpose is the replace the __import__ function with
# a new one that keeps track of imported modules.
#
#####################################################################

# Replacement for __import__()
def import_hook(name, globals=None, locals=None, fromlist=None):
    parent = determine_parent(globals)
    q, tail = find_head_package(parent, name)
    m = load_tail(q, tail)
    if not fromlist:
        return q
    if hasattr(m, "__path__"):
        ensure_fromlist(m, fromlist)
    return m

def determine_parent(globals):
    if not globals or  not globals.has_key("__name__"):
        return None
    pname = globals['__name__']
    if globals.has_key("__path__"):
        parent = sys.modules[pname]
        assert globals is parent.__dict__
        return parent
    if '.' in pname:
        i = string.rfind(pname, '.')
        pname = pname[:i]
        parent = sys.modules[pname]
        assert parent.__name__ == pname
        return parent
    return None

def find_head_package(parent, name):
    if '.' in name:
        i = string.find(name, '.')
        head = name[:i]
        tail = name[i+1:]
    else:
        head = name
        tail = ""
    if parent:
        qname = "%s.%s" % (parent.__name__, head)
    else:
        qname = head
    q = import_module(head, qname, parent)
    if q: return q, tail
    if parent:
        qname = head
        parent = None
        q = import_module(head, qname, parent)
        if q: return q, tail
    raise ImportError, "No module named " + qname

def load_tail(q, tail):
    m = q
    while tail:
        i = string.find(tail, '.')
        if i < 0: i = len(tail)
        head, tail = tail[:i], tail[i+1:]
        mname = "%s.%s" % (m.__name__, head)
        m = import_module(head, mname, m)
        if not m:
            raise ImportError, "No module named " + mname
    return m

def ensure_fromlist(m, fromlist, recursive=0):
    for sub in fromlist:
        if sub == "*":
            if not recursive:
                try:
                    all = m.__all__
                except AttributeError:
                    pass
                else:
                    ensure_fromlist(m, all, 1)
            continue
        if sub != "*" and not hasattr(m, sub):
            subname = "%s.%s" % (m.__name__, sub)
            submod = import_module(sub, subname, m)
            if not submod:
                raise ImportError, "No module named " + subname

_watched_modules = {}

def import_module(partname, fqname, parent, force_reload=0):
    if not force_reload:
        try:
            return sys.modules[fqname]
        except KeyError:
            pass
    try:
        fp, pathname, stuff = imp.find_module(partname,
                                              parent and parent.__path__)
    except ImportError:
        return None
    try:
        m = imp.load_module(fqname, fp, pathname, stuff)
        if stuff[2] == imp.PY_SOURCE:
            pathname = os.path.abspath (pathname)
            _watched_modules[pathname] = (os.stat(pathname), m)
    finally:
        if fp: fp.close()
    if parent:
        setattr(parent, partname, m)
    return m

# Replacement for reload()
def reload_hook(module):
    name = module.__name__
    if '.' not in name:
        return import_module(name, name, None, force_reload=1)
    i = string.rfind(name, '.')
    pname = name[:i]
    parent = sys.modules[pname]
    return import_module(name[i+1:], name, parent, force_reload=1)

# Save the original hooks
original_import = __builtin__.__import__
original_reload = __builtin__.reload

# Now install our hooks
__builtin__.__import__ = import_hook
__builtin__.reload = reload_hook

#####################################################################
#
# Now the real new stuff
#
#####################################################################


def _monitor ():
    while 1:
        time.sleep (1)
        start = time.time()
        for path, (status, module) in _watched_modules.items():
            if os.stat(path) != status:
                superreload (module)

_old_classes = {}

def superreload(module, reload=reload):
    """superreload (module) -> module

    Enhanced version of the builtin reload function.
    superreload replaces the class dictionary of every top-level
    class in the module with the new one automatically.
    """
    for name, oldclass in module.__dict__.items():
        if type(oldclass) == types.ClassType:
     _old_classes[name] = _old_classes.get (name, []) + [oldclass]
            
    result = reload(module)
            
    for name, oldclasses in _old_classes.items():
 newclass = getattr(module, name)
 for oldclass in oldclasses:
     oldclass.__dict__ = newclass.__dict__.copy()
    return result

_worker = None

def run():
    global _worker
    _worker = threading.Thread (target=_monitor)
    _worker.setDaemon(1)
    _worker.start()
--------cut here--------





More information about the Python-list mailing list