"Fossies" - the Fresh Open Source Software Archive

Member "NZMATH-1.2.0/nzmath/all.py" (19 Nov 2012, 2346 Bytes) of package /linux/misc/old/NZMATH-1.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "all.py" see the Fossies "Dox" file reference documentation.

    1 from os import listdir
    2 from os.path import split, isdir, join, isabs
    3 from imp import get_suffixes
    4 
    5 import nzmath
    6 _NZMATH_root = split(nzmath.__path__[0])[0]
    7 del nzmath
    8 
    9 def import_all(directory):
   10     """
   11     Execute 'from (directory) import (all files except "all.py")'.
   12     """
   13     try:
   14         exec('import ' + directory)
   15 
   16         import_files = eval(directory + ".__all__")
   17         for import_file in import_files:
   18             if import_file != "all":
   19                 exec('import ' + directory + '.' + import_file)
   20     except AttributeError: # __all__ does not exist
   21         pass
   22     except ImportError: 
   23         # 1. __init__.py does not exist
   24         # 2. import_file is not file name
   25         pass
   26 
   27 def search_subdir(directory):
   28     """
   29     Return all (1-level) subdirectory-names in directory.
   30     directory should be a path string.
   31     """
   32     return [subdir for subdir in listdir(directory) 
   33             if isdir(join(directory, subdir))]
   34 
   35 def path_to_import_str(path, import_root_path='/'):
   36     """
   37     Return '.' separated form (for Python import statements)
   38     from an (absolute) path.
   39     """
   40     for (suffix,mode,imp_type) in get_suffixes():
   41         dot_idx = path.find(suffix)
   42         if path.find(suffix) >= 0:
   43             path = path[:len(suffix)]
   44 
   45     if path.find(import_root_path) >= 0:
   46         path = path[len(import_root_path):]
   47     if isabs(path):
   48         path = path[1:]
   49 
   50     head, tail = split(path)
   51     if not(head):
   52         return tail
   53     head = path_to_import_str(head, import_root_path)
   54     return head + '.' + tail
   55 
   56 def recursive_import_all(rootdir, import_root_path):
   57     """
   58     Execute 'from (dir) import *' at all directories in rootdir recursively.
   59     All directories are searched by using Breadth-First Search 
   60     from rootdir (rootdir is included in). 
   61 
   62     rootdir should be an absolute path under import_root_path. 
   63     import_root_path should be a directory 
   64     whose Python can be searchable on importing process.
   65     """
   66     queue = [rootdir]
   67     while queue:
   68         target_dir = queue.pop(0)
   69         import_all(path_to_import_str(target_dir, import_root_path))
   70         queue.extend([join(target_dir, subdir) 
   71             for subdir in search_subdir(target_dir)])
   72 
   73 script_dir = split(__file__)[0]
   74 recursive_import_all(script_dir, _NZMATH_root)
   75 
   76 del listdir, split, isdir, join, isabs, get_suffixes