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
22#include <utils/xml/XMLSubSys.h>
30#include <xercesc/parsers/SAXParser.hpp>
31
32#include "GNEPythonTool.h"
33
34// ============================================-===============================
35// member method definitions
36// ===========================================================================
37
38GNEPythonTool::GNEPythonTool(GNEApplicationWindow* applicationWindow, const std::string& toolPath,
39 const std::string& templateStr, FXMenuPane* menu) :
40 myApplicationWindow(applicationWindow),
41 myToolPath(toolPath),
42 myPythonToolName(FileHelpers::getFileFromPath(toolPath, true)) {
43 // build menu command
46 // parse tool options
47 if (templateStr.size() > 0) {
48 try {
50 // make a copy (needed for reset)
52 } catch (ProcessError& e) {
53 WRITE_ERROR("Error parsing template of tool: " + myPythonToolName + " (" + e.what() + ")");
54 }
55 }
56}
57
58
60
61
62const std::string&
66
67
72
73
74FXMenuCommand*
78
79
80void
82 // nothing to do here, use in children
83}
84
85
86void
88 // nothing to do here, use in children
89}
90
91
92std::string
94 // add python script
95 const char* pythonEnv = getenv("PYTHON");
96 const std::string python = (pythonEnv == nullptr) ? "python" : pythonEnv;
97 const char* sumoHomeEnv = getenv("SUMO_HOME");
98 std::string sumoHome = "";
99 if (sumoHomeEnv != nullptr && sumoHomeEnv != std::string("")) {
100 sumoHome = std::string(sumoHomeEnv);
101 // harmonise slash
102 if (sumoHome.back() == '\\') {
103 sumoHome.pop_back();
104 }
105 if (sumoHome.back() != '/') {
106 sumoHome += "/";
107 }
108 // quote string to handle spaces but prevent double quotes
109 if (sumoHome.front() != '"') {
110 sumoHome = "\"" + sumoHome;
111 }
112 if (sumoHome.back() == '"') {
113 sumoHome.pop_back();
114 }
115 }
116 return python + " " + sumoHome + myToolPath + "\"";
117}
118
119
120std::string
122 std::string arguments;
123 // add arguments
124 for (const auto& option : myPythonToolsOptions) {
125 // only add modified values
126 if (!option.second->isDefault()) {
127 // for boolean values avoid use "true"
128 if (option.second->isBool()) {
129 if (option.second->getBool()) {
130 arguments += ("--" + option.first + " ");
131 } else {
132 arguments += ("--no-" + option.first + " ");
133 }
134 } else {
135 if (!option.second->isPositional()) {
136 arguments += ("--" + option.first + " ");
137 }
138 const std::string listSeparator = option.second->getListSeparator();
139 if (listSeparator != "") {
140 StringTokenizer st(option.second->getValueString(), " ", true);
141 bool first = true;
142 for (const std::string& v : st.getVector()) {
143 if (first) {
144 first = false;
145 } else {
146 arguments += listSeparator;
147 }
148 arguments += ("\"" + v + "\"");
149 }
150 arguments += " ";
151 } else {
152 arguments += ("\"" + StringUtils::escapeShell(option.second->getValueString()) + "\" ");
153 }
154 }
155 }
156 }
157 return getCommandPath() + " " + arguments;
158}
159
160
161const std::string
162GNEPythonTool::getDefaultValue(const std::string& name) const {
163 const auto value = myPythonToolsOptionsOriginal.getValueString(name);
164 // filter "none" values
165 if (value == "none") {
166 return "";
167 } else {
168 return value;
169 }
170}
171
172
173bool
174GNEPythonTool::loadConfiguration(const std::string& file) {
175 // make all options writable
177 // build parser
178 XERCES_CPP_NAMESPACE::SAXParser parser;
179 parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Never);
180 parser.setDisableDefaultEntityResolution(true);
181 // start the parsing
183 try {
184 parser.setDocumentHandler(&handler);
185 parser.setErrorHandler(&handler);
186 parser.parse(XMLSubSys::transcodeToLocal(file).c_str());
187 if (handler.errorOccurred()) {
188 WRITE_ERROR(TL("Could not load tool configuration '") + file + "'.");
189 return false;
190 }
191 } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
192 WRITE_ERROR(TL("Could not load tool configuration '") + file + "':\n " + XMLSubSys::transcode(e.getMessage()));
193 return false;
194 }
195 // write info
196 WRITE_MESSAGE(TLF("Loaded % configuration.", myPythonToolName));
197 return true;
198}
199
200
201void
202GNEPythonTool::saveConfiguration(const std::string& file) const {
203 std::string command = getCommand() + " -C \"" + file + "\" ";
204 // start in background
205#ifndef WIN32
206 command = command + " &";
207#else
208 // see "help start" for the parameters
209 command = "start /B \"\" " + command;
210#endif
211 // write info
212 WRITE_MESSAGE(TLF("Saved % configuration.", myPythonToolName));
213 // yay! fun with dangerous commands... Never use this over the internet
215}
216
217/****************************************************************************/
@ 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:39
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 unsigned long runHiddenCommand(const std::string &cmd)
run a shell command without popping up any windows (particuarly on win32)
Definition SysUtils.cpp:70
static void parseTemplate(OptionsCont &options, const std::string &templateString)
run parser
static std::string transcode(const XMLCh *const data, int length=-1)
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