cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Utils.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2018-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <Python.h>
20
24#include "CepGen/Utils/String.h"
26
27namespace cepgen {
28 namespace python {
29 std::string pythonPath(const std::string& file) {
30 const auto dir = fs::path{file}.remove_filename();
31 if (!dir.empty()) {
32 CG_DEBUG("Python") << "Adding {" << dir << "} to the default search paths.";
33 utils::env::append("PYTHONPATH", dir);
34 }
35
36 const auto filename = utils::replaceAll(fs::path{file}.replace_extension("").string() /* remove the extension */,
37 {{"../", ".."}, {"/", "."}});
38 CG_DEBUG("Python") << "Python path: " << filename;
39 return filename;
40 }
41
42 std::vector<std::wstring> info() {
43 auto* py_home = Py_GetPythonHome();
44#ifdef PYTHON2
45 std::wstring path{utils::toWstring(std::string(Py_GetPath()))},
46 home{utils::toWstring(std::string(py_home ? py_home : "(not set)"))};
47#else
48 std::wstring path{Py_GetPath()}, home{py_home ? py_home : L"(not set)"};
49#endif
50 return std::vector<std::wstring>{
51 utils::toWstring("Python version: " + utils::replaceAll(std::string{Py_GetVersion()}, "\n", " ")),
52 utils::toWstring("Platform: " + std::string(Py_GetPlatform())),
53 utils::toWstring("Home directory: ") + home,
54 utils::toWstring("Parsed path: ") + path};
55 }
56 } // namespace python
57} // namespace cepgen
#define CG_DEBUG(mod)
Definition Message.h:220
std::vector< std::wstring > info()
Definition Utils.cpp:42
std::string pythonPath(const std::string &file)
Translate a filename into a python-compatible path.
Definition Utils.cpp:29
void append(const std::string &var, const std::string &value)
Add a value to an environment variable.
size_t replaceAll(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences of a text by another.
Definition String.cpp:118
std::wstring toWstring(const std::string &str)
Convert a standard characters to a wide characters string.
Definition String.cpp:171
Common namespace for this Monte Carlo generator.