Source code for tic.loader

import os.path

import fnmatch
import logging
import os
from tic.utils.importlib import import_module

__all__ = ['load_components', 'locate']

[docs]def locate(pattern, root=None): '''Locate all files matching supplied filename pattern in and below supplied root directory.''' if root: walker_path = os.path.abspath(root) else: walker_path = root_path() for path, dirs, files in walk(walker_path, followlinks=True): for filename in fnmatch.filter(files, pattern): yield os.path.join(path, filename)
def root_path(): mod_path = __name__.replace(".", "/") file_path, ext = __file__.rsplit(".", 1) if file_path.startswith("./"): #fallback to abspath file_path = "%s/" % os.path.abspath(os.curdir) return file_path.replace(mod_path, '') def get_relative_path(full_path): return full_path.replace(root_path(), '') def _get_module_name(path): """takes an absolute path of a module and returns the fully qualified name of the module """ relative_path = path.replace(root_path(), '/') # remove __init__.py .. (invalid module name) # relative_path = relative_path.replace("__init__.py", '') return relative_path[1:-3].replace("/", ".") def load_py_files(): """Loader that look for Python source files in the plugins directories, which simply get imported, thereby registering them with the component manager if they define any components. """ def _load_py_files(env, search_path, auto_enable=None): import sys for path in search_path: sys.path.append(os.path.join(path, "lib")) plugin_files = locate("*.py", path) for plugin_file in plugin_files: p = "%s%s" % (os.path.join(path, "lib"), os.sep) if plugin_file.startswith(p): continue try: plugin_name = os.path.basename(plugin_file[:-3]) module_name = _get_module_name(plugin_file) if '.__init__' in module_name: module_name = module_name.replace('.__init__', '') import_module(module_name) _enable_plugin(env, plugin_name) except NotImplementedError, e: #print "Cant Implement This" pass return _load_py_files def get_plugins_dir(env): """Return the path to the `plugins` directory of the environment.""" plugins_dir = os.path.realpath(".") path = root_path() return os.path.normcase(path) def _enable_plugin(env, module): """Enable the given plugin module if it wasn't disabled explicitly.""" if env.is_component_enabled(module) is None: env.enable_component(module)
[docs]def load_components(env, extra_path=None, loaders=(load_py_files(),)): """Load all plugin components found on the given search path.""" plugins_dir = get_plugins_dir(env) search_path = [plugins_dir] if extra_path: search_path += list(extra_path) for loadfunc in loaders: loadfunc(env, search_path, auto_enable=plugins_dir)
def walk(top, topdown=True, onerror=None, followlinks=False): """Directory tree generator. STOLEN FROM PYTHON 2.6 .. "followlinks" feature is only available in 2.6 For each directory in the directory tree rooted at top (including top itself, but excluding '.' and '..'), yields a 3-tuple dirpath, dirnames, filenames dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists are just names, with no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name). If optional arg 'topdown' is true or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top down). If topdown is false, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom up). When topdown is true, the caller can modify the dirnames list in-place (e.g., via del or slice assignment), and walk will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, or to impose a specific order of visiting. Modifying dirnames when topdown is false is ineffective, since the directories in dirnames have already been generated by the time dirnames itself is generated. By default errors from the os.listdir() call are ignored. If optional arg 'onerror' is specified, it should be a function; it will be called with one argument, an os.error instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object. By default, os.walk does not follow symbolic links to subdirectories on systems that support them. In order to get this functionality, set the optional argument 'followlinks' to true. Caution: if you pass a relative pathname for top, don't change the current working directory between resumptions of walk. walk never changes the current directory, and assumes that the client doesn't either. Example: import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print root, "consumes", print sum([getsize(join(root, name)) for name in files]), print "bytes in", len(files), "non-directory files" if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories """ from os.path import join, isdir, islink # We may not have read permission for top, in which case we can't # get a list of the files the directory contains. os.path.walk # always suppressed the exception then, rather than blow up for a # minor reason when (say) a thousand readable directories are still # left to visit. That logic is copied here. try: # Note that listdir and error are globals in this module due # to earlier import-*. names = os.listdir(top) except os.error, err: if onerror is not None: onerror(err) return dirs, nondirs = [], [] for name in names: if isdir(join(top, name)): dirs.append(name) else: nondirs.append(name) if topdown: yield top, dirs, nondirs for name in dirs: path = join(top, name) if followlinks or not islink(path): for x in walk(path, topdown, onerror, followlinks): yield x if not topdown: yield top, dirs, nondirs