Skip to content

Commit

Permalink
Introduce new directory structure for python plugins.
Browse files Browse the repository at this point in the history
There is now a single directory called plugins that contains sub-
directories for algorithms & functions. These get installed into the
package under ROOT/plugins/python. This requires new keys to find the
directories and deprecates pythonalgorithms.directories key. Refs #970
  • Loading branch information
martyngigg committed Apr 17, 2013
1 parent 3361fdd commit 383df8d
Show file tree
Hide file tree
Showing 38 changed files with 44 additions and 18 deletions.
6 changes: 4 additions & 2 deletions Code/Mantid/Framework/Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ set ( PLUGINS "." )
set ( PV_PLUGINS "./pvplugins" )
set ( IGNORE_PARAVIEW "0" )
set ( QTPLUGINS "." )
set ( PYTHONALG_DIRS "${MANTID_ROOT}/Framework/PythonAPI/PythonAlgorithms;${MANTID_ROOT}/Framework/PythonInterface/PythonAlgorithms" )
set ( PYTHONALG_DIRS "${MANTID_ROOT}/Framework/PythonAPI/PythonAlgorithms;${MANTID_ROOT}/Framework/PythonInterface/plugins/algorithms" ) # deprecated
set ( PYTHONPLUGIN_DIRS "${MANTID_ROOT}/Framework/PythonAPI/PythonAlgorithms;${MANTID_ROOT}/Framework/PythonInterface/plugins" )
set ( DATADIRS ${MANTID_ROOT}/../../Test/AutoTestData;${MANTID_ROOT}/instrument )
set ( COLORMAPS_FOLDER ${MANTID_ROOT}/Installers/colormaps/ )

Expand Down Expand Up @@ -454,7 +455,8 @@ else ()
endif ()

set ( PLUGINS ${MANTID_ROOT}/plugins )
set ( PYTHONALG_DIRS ${PLUGINS}/PythonAlgs )
set ( PYTHONALG_DIRS ${PLUGINS}/PythonAlgs ) # deprecated
set ( PYTHONPLUGIN_DIRS ${PLUGINS}/python )
set ( DATADIRS "" )

# Construct script paths by replacing the old MANTID_ROOT with the new one.
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Properties/Mantid.properties.template
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pvplugins.directory = @PV_PLUGINS@
# Where to find Mantid Qt plugin libraries
mantidqt.plugins.directory = @QTPLUGINS@

# Where to find python plugins
python.plugins.directories = @PYTHONPLUGIN_DIRS@

# Where to load instrument definition files from
instrumentDefinition.directory = @MANTID_ROOT@/instrument

Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/PythonAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,6 @@ endif ()
# Pure Python files
install ( FILES ${PY_FILES} DESTINATION ${BIN_DIR} )
# Algorithms
install ( DIRECTORY PythonAlgorithms/ DESTINATION ${PLUGINS_DIR}/PythonAlgs
install ( DIRECTORY plugins/ DESTINATION ${PLUGINS_DIR}/python
PATTERN "*.pyc" EXCLUDE
PATTERN ".svn" EXCLUDE )
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ add_subdirectory( test )
###########################################################################

# Python algorithms
install ( DIRECTORY PythonAlgorithms/ DESTINATION ${PLUGINS_DIR}/PythonAlgs
install ( DIRECTORY plugins/ DESTINATION ${PLUGINS_DIR}/python
PATTERN "*.pyc" EXCLUDE
PATTERN ".svn" EXCLUDE )
23 changes: 18 additions & 5 deletions Code/Mantid/Framework/PythonInterface/mantid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,35 @@ def apiVersion():
import simpleapi as _simpleapi
from kernel import plugins as _plugins

plugin_dirs = kernel.config['pythonalgorithms.directories'].split(";")
plugin_dirs = kernel.config['python.plugins.directories'].split(";")
_user_key='user.python.plugins.directories'
plugin_dirs += kernel.config[_user_key].split(";")

# Check deprecated path
_deprecated_key='pythonalgorithms.directories'
deprecated_dirs = kernel.config[_deprecated_key].split(";")
if len(deprecated_dirs) > 0:
plugin_dirs += deprecated_dirs

plugin_files = []
alg_files = []
for directory in plugin_dirs:
try:
if directory != '':
plugin_files += _plugins.find_plugins(directory)
all_plugins, algs = _plugins.find_plugins(directory)
plugin_files += all_plugins
alg_files += algs
except ValueError, exc:
logger.warning(str(exc))
continue

# Mockup the full API first so that any Python algorithm module has something to import
_simpleapi._mockup(plugin_files)
# Now actually load the Python plugins
_simpleapi._mockup(alg_files)
# Load the plugins
plugin_modules = _plugins.load(plugin_files)
# Create the proper definitions in the module
# Create the proper algorithm definitions in the module
new_attrs = _simpleapi._translate()
# Finally, overwrite the mocked function definitions in the loaded modules with the real ones
_plugins.sync_attrs(_simpleapi, new_attrs, plugin_modules)

################################################################################
24 changes: 15 additions & 9 deletions Code/Mantid/Framework/PythonInterface/mantid/kernel/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ def find_plugins(top_dir):
"""
if not _os.path.isdir(top_dir):
raise ValueError("Cannot search given path for plugins, path is not a directory: '%s' " % str(top_dir))
plugins = []
all_plugins = []
algs = []
for root, dirs, files in _os.walk(top_dir):
for f in files:
if f.endswith(PluginLoader.extension):
plugins.append(_os.path.join(root, f))

return plugins
filename = _os.path.join(root, f)
all_plugins.append(filename)
if contains_newapi_algorithm(filename)):
algs.append(filename)

return all_plugins, algs

#======================================================================================================================

Expand Down Expand Up @@ -120,15 +124,14 @@ def load_from_dir(directory):

def load_from_file(filepath):
"""
If the algorithm is a new API algorithm then load it
Loads the plugin file. Any code present at the top-level will
be executed on loading
@param filepath :: A path that must point to a file
"""
loaded = []
try:
if contains_newapi_algorithm(filepath):
name, module = load_plugin(filepath)
loaded.append(module)
name, module = load_plugin(filepath)
loaded.append(module)
except Exception, exc:
logger.warning("Failed to load plugin %s. Error: %s" % (filepath, str(exc)))

Expand Down Expand Up @@ -189,5 +192,8 @@ def contains_newapi_algorithm(filename):
if 'registerPyAlgorithm' in line:
alg_found = False
break
if 'registerAlgorithm' in line:
alg_found = True
break
file.close()
return alg_found
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory contains example algorithms that can be plugged into the Mantid.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory contains example functions that can be plugged into the Mantid optimisation framework.

0 comments on commit 383df8d

Please sign in to comment.