Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNEPythonTool.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// Python tools used in netedit
19/****************************************************************************/
20
29#include <xercesc/parsers/SAXParser.hpp>
30
31#include "GNEPythonTool.h"
32
33// ============================================-===============================
34// member method definitions
35// ===========================================================================
36
37GNEPythonTool::GNEPythonTool(GNEApplicationWindow* applicationWindow, const std::string& toolPath,
38 const std::string& templateStr, FXMenuPane* menu) :
39 myApplicationWindow(applicationWindow),
40 myToolPath(toolPath),
41 myPythonToolName(FileHelpers::getFileFromPath(toolPath, true)) {
42 // build menu command
45 // parse tool options
46 if (templateStr.size() > 0) {
47 try {
49 // make a copy (needed for reset)
51 } catch (ProcessError& e) {
52 WRITE_ERROR("Error parsing template of tool: " + myPythonToolName + " (" + e.what() + ")");
53 }
54 }
55}
56
57
59
60
61const std::string&
65
66
71
72
73FXMenuCommand*
77
78
79void
81 // nothing to do here, use in children
82}
83
84
85void
87 // nothing to do here, use in children
88}
89
90
91std::string
93 // add python script
94 const char* pythonEnv = getenv("PYTHON");
95 const std::string python = (pythonEnv == nullptr) ? "python" : pythonEnv;
96 const char* sumoHomeEnv = getenv("SUMO_HOME");
97 std::string sumoHome = "";
98 if (sumoHomeEnv != nullptr && sumoHomeEnv != std::string("")) {
99 sumoHome = std::string(sumoHomeEnv);
100 // harmonise slash
101 if (sumoHome.back() == '\\') {
102 sumoHome.pop_back();
103 }
104 if (sumoHome.back() != '/') {
105 sumoHome += "/";
106 }
107 // quote string to handle spaces but prevent double quotes
108 if (sumoHome.front() != '"') {
109 sumoHome = "\"" + sumoHome;
110 }
111 if (sumoHome.back() == '"') {
112 sumoHome.pop_back();
113 }
114 }
115 return python + " " + sumoHome + myToolPath + "\"";
116}
117
118
119std::string
121 std::string arguments;
122 // add arguments
123 for (const auto& option : myPythonToolsOptions) {
124 // only add modified values
125 if (!option.second->isDefault()) {
126 // for boolean values avoid use "true"
127 if (option.second->isBool()) {
128 if (option.second->getBool()) {
129 arguments += ("--" + option.first + " ");
130 } else {
131 arguments += ("--no-" + option.first + " ");
132 }
133 } else {
134 if (!option.second->isPositional()) {
135 arguments += ("--" + option.first + " ");
136 }
137 const std::string listSeparator = option.second->getListSeparator();
138 if (listSeparator != "") {
139 StringTokenizer st(option.second->getValueString(), " ", true);
140 bool first = true;
141 for (const std::string& v : st.getVector()) {
142 if (first) {
143 first = false;
144 } else {
145 arguments += listSeparator;
146 }
147 arguments += ("\"" + v + "\"");
148 }
149 arguments += " ";
150 } else {
151 arguments += ("\"" + StringUtils::escapeShell(option.second->getValueString()) + "\" ");
152 }
153 }
154 }
155 }
156 return getCommandPath() + " " + arguments;
157}
158
159
160const std::string
161GNEPythonTool::getDefaultValue(const std::string& name) const {
162 const auto value = myPythonToolsOptionsOriginal.getValueString(name);
163 // filter "none" values
164 if (value == "none") {
165 return "";
166 } else {
167 return value;
168 }
169}
170
171
172bool
173GNEPythonTool::loadConfiguration(const std::string& file) {
174 // make all options writable
176 // build parser
177 XERCES_CPP_NAMESPACE::SAXParser parser;
178 parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Never);
179 parser.setDisableDefaultEntityResolution(true);
180 // start the parsing
182 try {
183 parser.setDocumentHandler(&handler);
184 parser.setErrorHandler(&handler);
185 parser.parse(StringUtils::transcodeToLocal(file).c_str());
186 if (handler.errorOccurred()) {
187 WRITE_ERROR(TL("Could not load tool configuration '") + file + "'.");
188 return false;
189 }
190 } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
191 WRITE_ERROR(TL("Could not load tool configuration '") + file + "':\n " + StringUtils::transcode(e.getMessage()));
192 return false;
193 }
194 // write info
195 WRITE_MESSAGE(TLF("Loaded % configuration.", myPythonToolName));
196 return true;
197}
198
199
200void
201GNEPythonTool::saveConfiguration(const std::string& file) const {
202 std::string command = getCommand() + " -C \"" + file + "\" ";
203 // start in background
204#ifndef WIN32
205 command = command + " &";
206#else
207 // see "help start" for the parameters
208 command = "start /B \"\" " + command;
209#endif
210 // write info
211 WRITE_MESSAGE(TLF("Saved % configuration.", myPythonToolName));
212 // yay! fun with dangerous commands... Never use this over the internet
214}
215
216/****************************************************************************/
@ MID_GNE_OPENPYTHONTOOLDIALOG
call tool
Definition GUIAppEnum.h:765
#define WRITE_MESSAGE(msg)
Definition MsgHandler.h:288
#define WRITE_ERROR(msg)
Definition MsgHandler.h:295
#define TL(string)
Definition MsgHandler.h:304
#define TLF(string,...)
Definition MsgHandler.h:306
Functions for an easier usage of files and paths.
Definition FileHelpers.h:38
OptionsCont myPythonToolsOptionsOriginal
original tools options
FXMenuCommand * myMenuCommand
menu command associated with this tool
virtual std::string getCommand() const
get command (python + script + arguments)
void saveConfiguration(const std::string &file) const
save configuration
bool loadConfiguration(const std::string &file)
load configuration
virtual void setCurrentValues()
set current values (used for set values like current folder and similar)
virtual ~GNEPythonTool()
destructor
const std::string getDefaultValue(const std::string &name) const
get default value of the given parameter
std::string getCommandPath() const
get command (python + script)
const std::string & getToolName() const
get tool name
FXMenuCommand * getMenuCommand() const
get menu command
const std::string myPythonToolName
tool name
virtual void postProcessing()
execute post processing
OptionsCont & getToolsOptions()
get tools options
GNEPythonTool(GNEApplicationWindow *applicationWindow, const std::string &toolPath, const std::string &templateStr, FXMenuPane *menu)
Constructor.
const std::string myToolPath
python tool path relative to SUMO_HOME
OptionsCont myPythonToolsOptions
tools options
static FXMenuCommand * buildFXMenuCommandShortcut(FXComposite *p, const std::string &text, const std::string &shortcut, const std::string &info, FXIcon *icon, FXObject *tgt, FXSelector sel)
build menu command
static FXIcon * getIcon(const GUIIcon which)
returns a icon previously defined in the enum GUIIcon
A storage for options typed value containers)
Definition OptionsCont.h:89
std::string getValueString(const std::string &name) const
Returns the string-value of the named option (all options)
void resetWritable()
Resets all options to be writeable.
A SAX-Handler for loading options.
bool errorOccurred() const
Returns the information whether an error occurred.
std::vector< std::string > getVector()
return vector of strings
static std::string escapeShell(const std::string &orig)
Escape special characters with backslash.
static std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
static std::string transcodeToLocal(const std::string &utf8String)
convert a string from UTF-8 to the local codepage
static unsigned long runHiddenCommand(const std::string &cmd)
run a shell command without popping up any windows (particuarly on win32)
Definition SysUtils.cpp:69
static void parseTemplate(OptionsCont &options, const std::string &templateString)
run parser