Package pysys :: Package utils :: Module loader
[hide private]
[frames] | no frames]

Source Code for Module pysys.utils.loader

 1  #!/usr/bin/env python 
 2  # PySys System Test Framework, Copyright (C) 2006-2013  M.B.Grieve 
 3   
 4  # This library is free software; you can redistribute it and/or 
 5  # modify it under the terms of the GNU Lesser General Public 
 6  # License as published by the Free Software Foundation; either 
 7  # version 2.1 of the License, or (at your option) any later version. 
 8   
 9  # This library is distributed in the hope that it will be useful, 
10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12  # Lesser General Public License for more details. 
13   
14  # You should have received a copy of the GNU Lesser General Public 
15  # License along with this library; if not, write to the Free Software 
16  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
17   
18  # Contact: moraygrieve@users.sourceforge.net 
19   
20  import sys, imp, threading 
21   
22 -def import_module(name, path, reload=False):
23 """Import a named module, searching within a list of paths. 24 25 Supports loading of hierarchical modules from a list of paths, returning a reference to the loaded module. 26 Reloading of the module can be requested should the module already exist in sys.modules. Note that in a 27 module named X.Y.Z, should reload be set to True only the tail module, X.Y.Z, will be reloaded on import; 28 the intervening modules, X and X.Y, will not be reloaded. 29 30 @param name: The module name 31 @param path: A list of paths to search for the module 32 @param reload: A boolean indicating if the module should be reloaded if already in sys.modules 33 34 """ 35 elements = name.split(".") 36 module = __import_module(elements[0], elements[0], None, path, reload and elements[0] == name) 37 if not module: raise ImportError, "No module named " + name 38 39 if len(elements) > 1: 40 for element in elements[1:]: 41 fqname = "%s.%s" % (module.__name__, element) 42 module = __import_module(fqname, element, module, module and module.__path__, reload and fqname == name) 43 if not module: raise ImportError, "No module named " + fqname 44 return module
45 46
47 -def __import_module(fqname, qname, parent, path, reload):
48 """Method to load a module. 49 50 @param fqname: The fully qualified name of the module 51 @param qname: The qualified name relative to the parent module (if one exists) 52 @param parent: A reference to the loaded parent module 53 @param path: The path used to search for the module 54 @param reload: Boolean indicating if the module should be reloaded if already in sys.modules 55 56 """ 57 if not reload: 58 try: 59 return sys.modules[fqname] 60 except KeyError: 61 pass 62 try: 63 file, pathname, description = imp.find_module(qname, path) 64 except ImportError: 65 return None 66 try: 67 module = imp.load_module(fqname, file, pathname, description) 68 finally: 69 if file: file.close() 70 if parent: setattr(parent, qname, module) 71 return module
72