1515 * along with this library; if not, write to the Free Software Foundation, Inc.,
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1717 */
18- /*
18+ /*
1919 * @file PythonConfig.cpp
2020 * @author Nikolaos Apostolakos <nikoapos@gmail.com>
2121 */
2222
23- #include < boost/filesystem.hpp>
2423#include < SEImplementation/Configuration/PythonConfig.h>
24+ #include < boost/filesystem.hpp>
2525
2626using namespace Euclid ::Configuration;
2727namespace po = boost::program_options;
2828namespace fs = boost::filesystem;
2929
3030namespace {
3131
32- const std::string PYTHON_CONFIG_FILE { " python-config-file" };
33- const std::string PYTHON_ARGV { " python-arg" };
32+ const std::string PYTHON_CONFIG_FILE{" python-config-file" };
33+ const std::string PYTHON_ARGV{" python-arg" };
34+ const std::string PYTHON_CONFIG_OBJ{" python-config-object" };
3435
35- }
36+ } // namespace
3637
3738namespace SourceXtractor {
3839
@@ -41,38 +42,43 @@ PythonConfig::PythonConfig(long manager_id) : Configuration(manager_id) {
4142}
4243
4344std::map<std::string, Configuration::OptionDescriptionList> PythonConfig::getProgramOptions () {
44- return {{" Measurement config" , {
45- {PYTHON_CONFIG_FILE.c_str (), po::value<std::string>()->default_value ({}, " " ),
46- " Measurements python configuration file" },
47- {PYTHON_ARGV.c_str (), po::value<std::vector<std::string>>()->multitoken (),
48- " Parameters to pass to Python via sys.argv" }
49- }}};
45+ return {{" Measurement config" ,
46+ {{PYTHON_CONFIG_FILE.c_str (), po::value<std::string>()->default_value ({}, " " ),
47+ " Measurements python configuration file" },
48+ {PYTHON_ARGV.c_str (), po::value<std::vector<std::string>>()->multitoken (),
49+ " Parameters to pass to Python via sys.argv" }}}};
5050}
5151
52-
5352void PythonConfig::preInitialize (const UserValues& args) {
54- auto filename = args.find (PYTHON_CONFIG_FILE)->second .as <std::string>();
55- if (!filename.empty () && !fs::exists (filename)) {
56- throw Elements::Exception () << " Python configuration file " << filename
57- << " does not exist" ;
53+ auto filename = args.find (PYTHON_CONFIG_FILE)->second .as <std::string>();
54+ auto py_obj_iter = args.find (PYTHON_CONFIG_OBJ);
55+
56+ if (py_obj_iter != args.end ()) {
57+ m_measurement_config = py_obj_iter->second .as <boost::python::object>();
58+ } else if (!filename.empty () && !fs::exists (filename)) {
59+ throw Elements::Exception () << " Python configuration file " << filename << " does not exist" ;
5860 }
5961}
6062
6163void PythonConfig::initialize (const UserValues& args) {
62- auto &singleton = PythonInterpreter::getSingleton ();
63- auto filename = args.find (PYTHON_CONFIG_FILE)->second .as <std::string>();
64- if (!filename.empty ()) {
65- std::vector<std::string> argv;
66- if (args.find (PYTHON_ARGV) != args.end ()) {
67- argv = args.find (PYTHON_ARGV)->second .as <std::vector<std::string>>();
64+ auto & singleton = PythonInterpreter::getSingleton ();
65+ if (m_measurement_config) {
66+ singleton.setupContext (m_measurement_config);
67+ } else {
68+ auto filename = args.find (PYTHON_CONFIG_FILE)->second .as <std::string>();
69+ if (!filename.empty ()) {
70+ std::vector<std::string> argv;
71+ if (args.find (PYTHON_ARGV) != args.end ()) {
72+ argv = args.find (PYTHON_ARGV)->second .as <std::vector<std::string>>();
73+ }
74+ singleton.runFile (filename, argv);
6875 }
69- singleton.runFile (filename, argv);
7076 }
71- singleton.setupContext ();
77+ singleton.setupContext (m_measurement_config );
7278}
7379
7480PythonInterpreter& PythonConfig::getInterpreter () const {
7581 return PythonInterpreter::getSingleton ();
7682}
7783
78- } // end of namespace SourceXtractor
84+ } // end of namespace SourceXtractor
0 commit comments