Skip to content

Commit

Permalink
Refs #4333. Tidy up singleton access from Python.
Browse files Browse the repository at this point in the history
Add a static instance method on to the Python class so that it
looks the same as in C++
  • Loading branch information
martyngigg committed Dec 14, 2011
1 parent e2adad2 commit bc5fb03
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 71 deletions.
8 changes: 4 additions & 4 deletions Code/Mantid/Framework/PythonInterface/mantid/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
###############################################################################
# Make the singleton objects available as named variables
###############################################################################
framework_mgr = get_framework_mgr() # This starts the framework
algorithm_mgr = get_algorithm_mgr()
algorithm_factory = get_algorithm_factory()
analysis_data_svc = get_analysis_data_service()
framework_mgr = FrameworkManager.Instance() # This starts the framework
algorithm_mgr = AlgorithmManager.Instance()
algorithm_factory = AlgorithmFactory.Instance()
analysis_data_svc = AnalysisDataService.Instance()

###############################################################################
# Starting the FrameworkManager loads the C++ plugin libraries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ using namespace boost::python;
namespace
{
///@cond
//------------------------------------------------------------------------------------------------------
// A factory function returning a reference to the AlgorithmFactory instance so that Python can use it
AlgorithmFactoryImpl & getAlgorithmFactory()
{
return AlgorithmFactory::Instance();
}

//------------------------------------------------------------------------------------------------------
/**
Expand Down Expand Up @@ -65,11 +59,11 @@ namespace

void export_AlgorithmFactory()
{
// Create a factory function to return this in Python
def("get_algorithm_factory", &getAlgorithmFactory, return_value_policy<reference_existing_object>(), //This policy is really only safe for singletons
"Returns a reference to the AlgorithmFactory singleton");

class_<AlgorithmFactoryImpl,boost::noncopyable>("AlgorithmFactory", no_init)
.def("Instance", &AlgorithmFactory::Instance, return_value_policy<reference_existing_object>(), //This policy is really only safe for singletons
"Returns a reference to the AlgorithmFactory singleton")
.staticmethod("Instance")
.def("get_registered_algorithms", &getRegisteredAlgorithms, "Returns a Python dictionary of ")
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ using namespace boost::python;
namespace
{
///@cond
//------------------------------------------------------------------------------------------------------
// A factory function returning a reference to the AlgorithmManager instance so that Python can use it
AlgorithmManagerImpl & getAlgorithmManager()
{
return AlgorithmManager::Instance();
}

//------------------------------------------------------------------------------------------------------
/// Define overload generators
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(create_overloads,AlgorithmManagerImpl::create, 1,2);
Expand All @@ -31,13 +24,11 @@ namespace
void export_AlgorithmManager()
{
class_<AlgorithmManagerImpl,boost::noncopyable>("AlgorithmManager", no_init)
.def("Instance", &AlgorithmManager::Instance, return_value_policy<reference_existing_object>(), //This policy is really only safe for singletons
"Returns a reference to the AlgorithmManager singleton")
.staticmethod("Instance")
.def("create", &AlgorithmManagerImpl::create, create_overloads(args("name", "version"), "Creates a managed algorithm."))
.def("create_unmanaged", &AlgorithmManagerImpl::createUnmanaged,
createUnmanaged_overloads(args("name", "version"), "Creates an unmanaged algorithm."))
;

// Create a factory function to return this in Python
def("get_algorithm_mgr", &getAlgorithmManager, return_value_policy<reference_existing_object>(), //This policy is really only safe for singletons
"Returns a reference to the AlgorithmManager singleton");

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ namespace boost
namespace
{
///@cond
//----------------------------------------------------------------------------
// Factory function to return the singleton reference
AnalysisDataServiceImpl & getAnalysisDataService()
{
return AnalysisDataService::Instance();
}


//----------------------------------------------------------------------------
//
Expand All @@ -75,13 +68,12 @@ namespace
* @return A weak pointer to the named object. If the name does not exist it
* sets a KeyError error indicator.
*/
DataItem_wptr retrieveAsDataItem(object self, const std::string & name)
DataItem_wptr retrieveAsDataItem(AnalysisDataServiceImpl& self, const std::string & name)
{
UNUSED_ARG(self);
DataItem_wptr item;
try
{
item = getAnalysisDataService().retrieve(name);
item = self.retrieve(name);
}
catch(Mantid::Kernel::Exception::NotFoundError&)
{
Expand Down Expand Up @@ -115,6 +107,9 @@ void export_AnalysisDataService()
register_ptr_to_python<DataItem_wptr>();

class_<AnalysisDataServiceImpl,boost::noncopyable>("AnalysisDataService", no_init)
.def("Instance", &AnalysisDataService::Instance, return_value_policy<reference_existing_object>(),
"Return a reference to the ADS singleton")
.staticmethod("Instance")
.def("retrieve_as_data_item", &retrieveAsDataItem, "Retrieve the named object as data item. Raises an exception if the name does not exist")
.def("retrieve", &retrieveUpcastedPtr, "Retrieve the named object. Raises an exception if the name does not exist")
.def("remove", &AnalysisDataServiceImpl::remove, "Remove a named object")
Expand All @@ -128,7 +123,7 @@ void export_AnalysisDataService()
;

// Factory function
def("get_analysis_data_service", &getAnalysisDataService, return_value_policy<reference_existing_object>(),
def("get_analysis_data_service", &AnalysisDataService::Instance, return_value_policy<reference_existing_object>(),
"Return a reference to the ADS singleton");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ using namespace boost::python;
namespace
{
///@cond

// A factory function returning a reference to the AlgorithmManager instance so that
// Python can use it
FrameworkManagerImpl & getFrameworkManager()
{
return FrameworkManager::Instance();
}

/**
* Creates an initialised algorithm.
* If this called from within a Python, i.e if PyExec is in the call stack,
Expand All @@ -36,7 +28,7 @@ namespace
* @param name :: The name of the algorithm to create
* @param version :: The version of the algorithm to create (default = -1 = highest version)
*/
PyObject * createAlgorithm(boost::python::object self, const std::string & name, const int version = -1)
PyObject * createAlgorithm(FrameworkManagerImpl & self, const std::string & name, const int version = -1)
{
UNUSED_ARG(self);
IAlgorithm_sptr alg;
Expand All @@ -55,7 +47,7 @@ namespace
alg->setRethrows(true);

PyObject * wrapped = converter::shared_ptr_to_python(alg);
// Add an atribute to indicate asynchronous execution
// Add an attribute to indicate asynchronous execution
PyObject_SetAttrString(wrapped, "__async__", PyBool_FromLong(async));
return wrapped;
}
Expand All @@ -70,6 +62,9 @@ namespace
void export_FrameworkManager()
{
class_<FrameworkManagerImpl,boost::noncopyable>("FrameworkManager", no_init)
.def("Instance", &FrameworkManager::Instance, return_value_policy<reference_existing_object>(),
"Returns a reference to the FrameworkManager singleton")
.staticmethod("Instance")
.def("clear", &FrameworkManagerImpl::clear, "Clear all memory held by Mantid")
.def("clear_algorithms", &FrameworkManagerImpl::clearAlgorithms, "Clear memory held by algorithms (does not include workspaces)")
.def("clear_data", &FrameworkManagerImpl::clearData, "Clear memory held by the data service (essentially all workspaces, including hidden)")
Expand All @@ -80,11 +75,5 @@ void export_FrameworkManager()
"be a managed algorithm"))
;

// Create a factory function to return this in Python
def("get_framework_mgr", &getFrameworkManager, return_value_policy<reference_existing_object>(),
"Returns a reference to the FrameworkManager singleton")
;


}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
###############################################################################
# Make the singleton objects available as named variables
###############################################################################
config = get_config_service()
config = ConfigService.Instance()

###############################################################################
# Set up a general Python logger. Others can be created as they are required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,18 @@ using Mantid::Kernel::ConfigService;
using Mantid::Kernel::ConfigServiceImpl;
using namespace boost::python;

namespace
{
/**
* Getter to return the singleton reference
* @returns A reference to the ConfigServic singleton
*/
ConfigServiceImpl & getConfigService()
{
return ConfigService::Instance();
}

}

void export_ConfigService()
{
class_<ConfigServiceImpl, boost::noncopyable>("ConfigService", no_init)
.def("Instance", &ConfigService::Instance, return_value_policy<reference_existing_object>(),
"Returns a reference to the ConfigService")
.staticmethod("Instance")
.def("get_data_dirs",&ConfigServiceImpl::getDataSearchDirs, return_value_policy<copy_const_reference>(),
"Return the current list of data search paths")
// Treat this as a dictionary
.def("__getitem__", (std::string (ConfigServiceImpl::*)(const std::string &))&ConfigServiceImpl::getString)
.def("__setitem__", &ConfigServiceImpl::setString)
.def("__contains__", &ConfigServiceImpl::hasProperty)
;

def("get_config_service", &getConfigService, return_value_policy<reference_existing_object>(),
"Returns a reference to the ConfigService");

;
}

0 comments on commit bc5fb03

Please sign in to comment.