reload_importer.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import __builtin__
34 from importlib import reload
35 import os
36 import sys
37 
38 
40  """
41  Overrides the builtin import and automatically reloads all modules.
42 
43  Modules are imported from one of the reload paths after calling enable.
44  """
45 
46  def __init__(self):
47  self._excluded_modules = sys.modules.keys()
48  self._reload_paths = None
49  self._import_stack = []
50  self._reloaded_modules = set()
51  self._import = __builtin__.__import__
52 
53  def enable(self):
54  __builtin__.__import__ = self._reimport
55 
56  def disable(self):
57  __builtin__.__import__ = self._import
58 
59  def add_reload_path(self, path):
60  if self._reload_paths is None:
61  self._reload_paths = ()
62  self._reload_paths += (os.path.abspath(path),)
63 
64  def _reload(self, module):
65  if module.__name__ not in self._import_stack and module.__name__ in sys.modules:
66  if not self._import_stack:
67  self._reloaded_modules.clear()
68  self._import_stack.append(module.__name__)
69  # force reload
70  if module.__name__ not in self._reloaded_modules:
71  reload(module)
72  self._reloaded_modules.add(module.__name__)
73  self._import_stack.pop()
74 
75  def _reimport(self, name, globals_=None, locals_=None, fromlist=None, level=-1):
76  module = self._import(
77  name, globals_, locals_, fromlist if not None else [], level if not None else -1)
78 
79  if module.__name__ not in self._excluded_modules:
80  if self._reload_paths is None or \
81  (hasattr(module, '__file__') and
82  len([p for p in self._reload_paths if module.__file__.startswith(p)]) > 0):
83  self._reload(module)
84 
85  return module
qt_gui.reload_importer.ReloadImporter._reload_paths
_reload_paths
Definition: reload_importer.py:48
qt_gui.reload_importer.ReloadImporter._import
_import
Definition: reload_importer.py:51
qt_gui.reload_importer.ReloadImporter._reimport
def _reimport(self, name, globals_=None, locals_=None, fromlist=None, level=-1)
Definition: reload_importer.py:75
qt_gui.reload_importer.ReloadImporter._import_stack
_import_stack
Definition: reload_importer.py:49
qt_gui.reload_importer.ReloadImporter.add_reload_path
def add_reload_path(self, path)
Definition: reload_importer.py:59
qt_gui.reload_importer.ReloadImporter._excluded_modules
_excluded_modules
Definition: reload_importer.py:47
qt_gui.reload_importer.ReloadImporter
Definition: reload_importer.py:39
qt_gui.reload_importer.ReloadImporter._reload
def _reload(self, module)
Definition: reload_importer.py:64
qt_gui.reload_importer.ReloadImporter.disable
def disable(self)
Definition: reload_importer.py:56
qt_gui.reload_importer.ReloadImporter._reloaded_modules
_reloaded_modules
Definition: reload_importer.py:50
qt_gui.reload_importer.ReloadImporter.enable
def enable(self)
Definition: reload_importer.py:53
qt_gui.reload_importer.ReloadImporter.__init__
def __init__(self)
Definition: reload_importer.py:46


qt_gui
Author(s): Dirk Thomas
autogenerated on Sat Jun 25 2022 02:15:05