SourceXtractorPlusPlus  0.19
SourceXtractor++, the next generation SExtractor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PythonConfig.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2019-2022 Université de Genève, LMU Munich - Faculty of Physics, IAP-CNRS/Sorbonne Université
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 /*
19  * @file PythonConfig.cpp
20  * @author Nikolaos Apostolakos <nikoapos@gmail.com>
21  */
22 
23 #include <boost/filesystem.hpp>
25 
26 using namespace Euclid::Configuration;
27 namespace po = boost::program_options;
28 namespace fs = boost::filesystem;
29 
30 namespace {
31 
32 const std::string PYTHON_CONFIG_FILE { "python-config-file" };
33 const std::string PYTHON_ARGV { "python-arg" };
34 
35 }
36 
37 namespace SourceXtractor {
38 
39 PythonConfig::PythonConfig(long manager_id) : Configuration(manager_id) {
41 }
42 
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  }}};
50 }
51 
52 
53 void 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";
58  }
59 }
60 
61 void 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()) {
66  if (args.find(PYTHON_ARGV) != args.end()) {
67  argv = args.find(PYTHON_ARGV)->second.as<std::vector<std::string>>();
68  }
69  singleton.runFile(filename, argv);
70  }
71  singleton.setupContext();
72 }
73 
76 }
77 
78 } // end of namespace SourceXtractor
PythonInterpreter & getInterpreter() const
STL class.
STL class.
void preInitialize(const UserValues &args) override
string filename
Definition: conf.py:65
void initialize(const UserValues &args) override
std::map< std::string, Configuration::OptionDescriptionList > getProgramOptions() override
static PythonInterpreter & getSingleton()