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-2023 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* GNEApp, const std::string& toolPath,
38 const std::string& templateStr, FXMenuPane* menu) :
39 myGNEApp(GNEApp),
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
63 return myGNEApp;
64}
65
66
67const std::string&
71
72
77
78
79FXMenuCommand*
83
84
85void
87 // nothing to do here, use in children
88}
89
90
91void
93 // nothing to do here, use in children
94}
95
96
97std::string
99 // add python script
100 const char* pythonEnv = getenv("PYTHON");
101 const std::string python = (pythonEnv == nullptr) ? "python" : pythonEnv;
102 const char* sumoHomeEnv = getenv("SUMO_HOME");
103 std::string sumoHome = "";
104 if (sumoHomeEnv != nullptr && sumoHomeEnv != std::string("")) {
105 sumoHome = std::string(sumoHomeEnv);
106 // quote string to handle spaces but prevent double quotes
107 if (sumoHome.front() != '"') {
108 sumoHome = "\"" + sumoHome;
109 }
110 if (sumoHome.back() != '"') {
111 sumoHome += "\"";
112 }
113 sumoHome += "/";
114 }
115 // get command
116 std::string command = python + " " + sumoHome + myToolPath;
117 // declare arguments
118 std::string arguments;
119 // add arguments
120 for (const auto& option : myPythonToolsOptions) {
121 // only add modified values
122 if (!option.second->isDefault()) {
123 // for boolean values avoid use "true"
124 if (option.second->isBool()) {
125 arguments += ("--" + option.first + " ");
126 } else {
127 if (!option.second->isPositional()) {
128 arguments += ("--" + option.first + " ");
129 }
130 const std::string listSeparator = option.second->getListSeparator();
131 if (listSeparator != "") {
132 StringTokenizer st(option.second->getValueString(), " ", true);
133 bool first = true;
134 for (const std::string& v : st.getVector()) {
135 if (first) {
136 first = false;
137 } else {
138 arguments += listSeparator;
139 }
140 arguments += ("\"" + v + "\"");
141 }
142 arguments += " ";
143 } else {
144 arguments += ("\"" + option.second->getValueString() + "\" ");
145 }
146 }
147 }
148 }
149 return command + " " + arguments;
150}
151
152
153const std::string
154GNEPythonTool::getDefaultValue(const std::string& name) const {
155 const auto value = myPythonToolsOptionsOriginal.getValueString(name);
156 // filter "none" values
157 if (value == "none") {
158 return "";
159 } else {
160 return value;
161 }
162}
163
164
165bool
166GNEPythonTool::loadConfiguration(const std::string& file) {
167 // make all options writables
169 // build parser
170 XERCES_CPP_NAMESPACE::SAXParser parser;
171 parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Never);
172 parser.setDisableDefaultEntityResolution(true);
173 // start the parsing
175 try {
176 parser.setDocumentHandler(&handler);
177 parser.setErrorHandler(&handler);
178 parser.parse(StringUtils::transcodeToLocal(file).c_str());
179 if (handler.errorOccurred()) {
180 WRITE_ERROR(TL("Could not load tool configuration '") + file + "'.");
181 return false;
182 }
183 } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
184 WRITE_ERROR(TL("Could not load tool configuration '") + file + "':\n " + StringUtils::transcode(e.getMessage()));
185 return false;
186 }
187 // write info
188 WRITE_MESSAGE(TLF("Loaded % configuration.", myPythonToolName));
189 return true;
190}
191
192
193void
194GNEPythonTool::saveConfiguration(const std::string& file) const {
195 // add python script
196 const char* pythonEnv = getenv("PYTHON");
197 const std::string python = (pythonEnv == nullptr) ? "python" : pythonEnv;
198 const char* sumoHomeEnv = getenv("SUMO_HOME");
199 const std::string sumoHome = (sumoHomeEnv == nullptr) ? "" : sumoHomeEnv + std::string("/");
200 // get command
201 std::string command = python + " " + sumoHome + myToolPath + " -C " + file + " ";
202 // add arguments
203 for (const auto& option : myPythonToolsOptions) {
204 // only write modified values
205 if (!option.second->isDefault()) {
206 command += ("--" + option.first + " \"" + option.second->getValueString() + "\" ");
207 }
208 }
209 // start in background
210#ifndef WIN32
211 command = command + " &";
212#else
213 // see "help start" for the parameters
214 command = "start /B \"\" " + command;
215#endif
216 // write info
217 WRITE_MESSAGE(TLF("Saved % configuration.", myPythonToolName));
218 // yay! fun with dangerous commands... Never use this over the internet
220}
221
222/****************************************************************************/
@ MID_GNE_OPENPYTHONTOOLDIALOG
call tool
Definition GUIAppEnum.h:735
#define WRITE_MESSAGE(msg)
Definition MsgHandler.h:272
#define WRITE_ERROR(msg)
Definition MsgHandler.h:279
#define TL(string)
Definition MsgHandler.h:287
#define TLF(string,...)
Definition MsgHandler.h:288
Functions for an easier usage of files and paths.
Definition FileHelpers.h:38
The main window of Netedit.
GNEPythonTool(GNEApplicationWindow *GNEApp, const std::string &toolPath, const std::string &templateStr, FXMenuPane *menu)
Constructor.
OptionsCont myPythonToolsOptionsOriginal
original tools options
FXMenuCommand * myMenuCommand
menu command associated with this tool
virtual std::string getCommand() const
get command (python + script + arguments)
GNEApplicationWindow * getGNEApp() const
get to GNEApplicationWindow
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
GNEApplicationWindow * myGNEApp
pointer to GNEApplicationWindow
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
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 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:68
static void parseTemplate(OptionsCont &options, const std::string &templateString)
run parser