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