This repository was archived by the owner on Apr 28, 2025. It is now read-only.
forked from jparise/python-reloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreloader.py
More file actions
154 lines (124 loc) · 5.19 KB
/
reloader.py
File metadata and controls
154 lines (124 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Python Module Reloader
#
# Copyright (c) 2009, 2010, 2011 Jon Parise <jon@indelible.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Python Module Reloader"""
try:
import builtins
except ImportError:
#python 2.x
import __builtin__ as builtins
import imp
import sys
__author__ = 'Jon Parise <jon@indelible.org>'
__version__ = '0.3'
__all__ = ('enable', 'disable', 'get_dependencies', 'reload')
_baseimport = builtins.__import__
_parents = dict()
_parent = None
def enable():
"""Enable global module dependency tracking."""
builtins.__import__ = _import
def disable():
"""Disable global module dependency tracking."""
builtins.__import__ = _baseimport
_parents.clear()
_parent = None
def get_parents(m):
"""Get the dependency list for the given imported module."""
return _parents.get(m.__name__, None)
def _deepcopy_module_dict(m):
"""Make a deep copy of a module's dictionary."""
import copy
# We can't deepcopy() everything in the module's dictionary because some
# items, such as '__builtins__', aren't deepcopy()-able. To work around
# that, we start by making a shallow copy of the dictionary, giving us a
# way to remove keys before performing the deep copy.
d = vars(m).copy()
del d['__builtins__']
return copy.deepcopy(d)
def _reload(m, visited):
"""Internal module reloading routine."""
name = getattr(m, '__name__', None)
# Start by adding this module to our set of visited modules. We use this
# set to avoid running into infinite recursion while walking the module
# dependency graph.
visited.add(m)
if name is None:
return
# Because we're triggering a reload and not an import, the module itself
# won't run through our _import hook below. In order for this module's
# dependencies (which will pass through the _import hook) to be associated
# with this module, we need to set our parent pointer beforehand.
global _parent
_parent = name
# If the module has a __reload__(d) function, we'll call it with a copy of
# the original module's dictionary after it's been reloaded.
callback = getattr(m, '__reload__', None)
if callback is not None:
d = _deepcopy_module_dict(m)
imp.reload(m)
callback(d)
else:
imp.reload(m)
# Reset our parent pointer now that the reloading operation is complete.
_parent = None
# follow our parents so they can grab the changes we made to ourself
parents = _parents.get(name)
if parents is not None:
for parent in parents:
if parent not in visited:
_reload(parent, visited)
def reload(m):
"""Reload an existing module.
Any known dependencies of the module will also be reloaded.
If a module has a __reload__(d) function, it will be called with a copy of
the original module's dictionary after the module is reloaded."""
_reload(m, set())
def _import(name, globals=None, locals=None, fromlist=None, level=-1):
"""__import__() replacement function that tracks module dependencies."""
# Track our current parent module. This is used to find our current place
# in the dependency graph.
global _parent
parent = _parent
_parent = name
if globals is None:
globals = sys._getframe(1).f_globals
if locals is None:
locals = {}
if fromlist is None:
fromlist = []
# Perform the actual import using the base import function. We get the
# module directly from sys.modules because the import function only
# returns the top-level module reference for a nested import statement
# (e.g. `import package.module`).
m = _baseimport(name, globals, locals, fromlist, level)
sysentry = sys.modules.get(name, None)
# If we have a parent (i.e. this is a nested import) and this is a
# reloadable (source-based) module, we append ourself to our parent's
# dependency list.
if parent is not None and hasattr(sysentry, '__file__'):
parent_entry = sys.modules.get(parent)
if parent_entry:
l = _parents.setdefault(name,set())
l.add(parent_entry)
# Lastly, we always restore our global _parent pointer.
_parent = parent
return m