summaryrefslogtreecommitdiff
path: root/python/gras
diff options
context:
space:
mode:
Diffstat (limited to 'python/gras')
-rw-r--r--python/gras/CMakeLists.txt10
-rw-r--r--python/gras/GRAS_Factory.i16
-rw-r--r--python/gras/GRAS_Loader.py48
-rw-r--r--python/gras/__init__.py1
4 files changed, 73 insertions, 2 deletions
diff --git a/python/gras/CMakeLists.txt b/python/gras/CMakeLists.txt
index de6175e..dda4934 100644
--- a/python/gras/CMakeLists.txt
+++ b/python/gras/CMakeLists.txt
@@ -79,8 +79,16 @@ endforeach(gras_swig_module)
########################################################################
# install other python files
########################################################################
+configure_file(
+ ${CMAKE_CURRENT_SOURCE_DIR}/GRAS_Loader.py
+ ${CMAKE_CURRENT_BINARY_DIR}/GRAS_Loader.py
+@ONLY)
+
GR_PYTHON_INSTALL(
- FILES __init__.py GRAS_Utils.py
+ FILES
+ __init__.py
+ GRAS_Utils.py
+ ${CMAKE_CURRENT_BINARY_DIR}/GRAS_Loader.py
DESTINATION ${GR_PYTHON_DIR}/gras
COMPONENT ${GRAS_COMP_PYTHON}
)
diff --git a/python/gras/GRAS_Factory.i b/python/gras/GRAS_Factory.i
index 01b51b9..1124963 100644
--- a/python/gras/GRAS_Factory.i
+++ b/python/gras/GRAS_Factory.i
@@ -27,12 +27,26 @@ namespace gras
////////////////////////////////////////////////////////////////////////
// Create python make method for the factory
////////////////////////////////////////////////////////////////////////
+%pythoncode %{
+#TODO we need to register this into the real factory
+_py_factory = dict()
+%}
+
%extend gras::Factory
{
%insert("python")
%{
@staticmethod
- def make(name, *args):
+ def register_make(name, fcn):
+ #TODO we need to register this into the real factory
+ _py_factory[name] = fcn
+
+ @staticmethod
+ def make(name, *args, **kwargs):
+
+ #first try the local to python py factory #TODO real factory
+ if name in _py_factory: return _py_factory[name](*args, **kwargs)
+
from PMC import PMC_M
pmcargs = PMC_M(list(args))
return Factory._handle_make(name, pmcargs)
diff --git a/python/gras/GRAS_Loader.py b/python/gras/GRAS_Loader.py
new file mode 100644
index 0000000..b70bc60
--- /dev/null
+++ b/python/gras/GRAS_Loader.py
@@ -0,0 +1,48 @@
+# Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.
+
+import os
+import sys
+
+#try to import module
+#http://effbot.org/zone/import-string.htm
+def __try_module_import(filename):
+ directory, module_name = os.path.split(filename)
+ module_name = os.path.splitext(module_name)[0]
+
+ path = list(sys.path)
+ sys.path.insert(0, directory)
+ try:
+ module = __import__(module_name)
+ except Exception as ex:
+ print 'Could not import', filename, ex
+ finally:
+ sys.path[:] = path # restore
+
+#recursive search for modules in path
+def __module_import(p):
+ if not os.path.exists(p): return
+ if os.path.isfile(p):
+ return __try_module_import(p)
+ if not os.path.isdir(p): return
+ if os.path.exists(os.path.join(p, '__init__.py')):
+ return __try_module_import(p)
+ for sub in os.listdir(p):
+ name, ext = os.path.splitext(sub)
+ #prefer .pyo over .pyc, prefer .pyc over .py
+ has_pyc = os.path.exists(os.path.join(p, name+'.pyc'))
+ has_pyo = os.path.exists(os.path.join(p, name+'.pyo'))
+ if ext == ".py" and (has_pyc or has_pyo): continue
+ if ext == ".pyc" and has_pyo: continue
+ __module_import(os.path.join(p, sub))
+
+#separate the paths and load each one
+def __load_modules_from_paths(paths, suffix):
+ if not paths: return
+ for path in paths.split(os.pathsep):
+ if not path: continue
+ if suffix: path = os.path.join(path, suffix)
+ __module_import(path)
+
+__load_modules_from_paths(os.getenv("GRAS_ROOT", "@GRAS_ROOT@"), os.path.join("lib@LIB_SUFFIX@", "gras", "python"))
+__load_modules_from_paths(os.getenv("GRAS_PATH", ""), os.path.join("lib@LIB_SUFFIX@", "gras", "python"))
+__load_modules_from_paths(os.getenv("GRAS_PYTHON_PATH", ""), "")
diff --git a/python/gras/__init__.py b/python/gras/__init__.py
index 3c16388..6dcd4cd 100644
--- a/python/gras/__init__.py
+++ b/python/gras/__init__.py
@@ -18,3 +18,4 @@ from GRAS_PyBlock import PyBlock as Block
from GRAS_PyHierBlocks import PyHierBlock as HierBlock
from GRAS_PyHierBlocks import PyTopBlock as TopBlock
from GRAS_ThreadPool import ThreadPoolConfig, ThreadPool
+import GRAS_Loader