diff --git a/include/dynamic_graph_bridge/ros_interpreter.hh b/include/dynamic_graph_bridge/ros_interpreter.hh index b2877eeb1f188f0c0cf9d4512ff58a36e631ca2c..df1047a7694f6d3e1caf0645114989f604180d43 100644 --- a/include/dynamic_graph_bridge/ros_interpreter.hh +++ b/include/dynamic_graph_bridge/ros_interpreter.hh @@ -2,6 +2,7 @@ # define DYNAMIC_GRAPH_BRIDGE_INTERPRETER_HH # include <ros/ros.h> # include <dynamic_graph_bridge_msgs/RunCommand.h> +# include <dynamic_graph_bridge_msgs/RunPythonFile.h> # include <dynamic-graph/python/interpreter.hh> namespace dynamicgraph @@ -20,6 +21,11 @@ namespace dynamicgraph dynamic_graph_bridge_msgs::RunCommand::Response&)> runCommandCallback_t; + typedef boost::function< + bool (dynamic_graph_bridge_msgs::RunPythonFile::Request&, + dynamic_graph_bridge_msgs::RunPythonFile::Response&)> + runPythonFileCallback_t; + explicit Interpreter (ros::NodeHandle& nodeHandle); /// \brief Run a command and return result. @@ -29,6 +35,10 @@ namespace dynamicgraph void runCommand(const std::string & command, std::string &result, std::string &out, std::string &err); + /// \brief Method to parse python scripts. + /// \param Input file name to parse. + void runPythonFile(std::string ifilename); + /// Initialize service run_command void startRosService (); @@ -37,10 +47,15 @@ namespace dynamicgraph bool runCommandCallback (dynamic_graph_bridge_msgs::RunCommand::Request& req, dynamic_graph_bridge_msgs::RunCommand::Response& res); + /// \brief Run a Python file. + bool runPythonFileCallback (dynamic_graph_bridge_msgs::RunPythonFile::Request& req, + dynamic_graph_bridge_msgs::RunPythonFile::Response& res); + private: python::Interpreter interpreter_; ros::NodeHandle& nodeHandle_; ros::ServiceServer runCommandSrv_; + ros::ServiceServer runPythonFileSrv_; }; } // end of namespace dynamicgraph. diff --git a/scripts/run_command b/scripts/run_command index f06cf1cf261f0b951a612011897b56e3201055dd..9615adc93f8c91874f3ce274d5830dde06f3fb81 100755 --- a/scripts/run_command +++ b/scripts/run_command @@ -19,7 +19,9 @@ class RosShell(InteractiveConsole): rospy.wait_for_service('run_command') self.client = rospy.ServiceProxy( 'run_command', dynamic_graph_bridge_msgs.srv.RunCommand, True) - + rospy.wait_for_service('run_script') + self.scriptClient = rospy.ServiceProxy( + 'run_script', dynamic_graph_bridge_msgs.srv.RunPythonFile, True) def runcode(self, code, retry = True): source = self.cache[:-1] @@ -69,6 +71,8 @@ class RosShell(InteractiveConsole): if __name__ == '__main__': import optparse manifest = roslib.manifest.load_manifest('dynamic_graph_bridge') + rospy.init_node('run_command', argv=sys.argv) + sys.argv = rospy.myargv(argv=None) parser = optparse.OptionParser( usage='\n\t%prog [options]', version='%%prog %s' % manifest.version) @@ -82,12 +86,8 @@ if __name__ == '__main__': if not sh.client: print("Connection to remote server has been lost.") sys.exit(1) - response = sh.client(str(source)) - if response.stdout != "": - print response.stdout[:-1] - if response.stderr != "": - print response.stderr[:-1] - elif response.result != "None": - print response.result + response = sh.scriptClient(infile) + if not response: + print("Error while file parsing ") else: sh.interact("Interacting with remote server.") diff --git a/src/ros_interpreter.cpp b/src/ros_interpreter.cpp index 4b5c528386c3dbdb5e07166b11bb693b6a7c923a..edd13b52cee619e724ea37e6677bd8e47f1bcb08 100644 --- a/src/ros_interpreter.cpp +++ b/src/ros_interpreter.cpp @@ -7,7 +7,8 @@ namespace dynamicgraph Interpreter::Interpreter (ros::NodeHandle& nodeHandle) : interpreter_ (), nodeHandle_ (nodeHandle), - runCommandSrv_ () + runCommandSrv_ (), + runPythonFileSrv_ () { } @@ -17,6 +18,11 @@ namespace dynamicgraph boost::bind (&Interpreter::runCommandCallback, this, _1, _2); runCommandSrv_ = nodeHandle_.advertiseService ("run_command", runCommandCb); + + runPythonFileCallback_t runPythonFileCb = + boost::bind (&Interpreter::runPythonFileCallback, this, _1, _2); + runPythonFileSrv_ = + nodeHandle_.advertiseService ("run_script", runPythonFileCb); } bool @@ -28,6 +34,15 @@ namespace dynamicgraph return true; } + bool + Interpreter::runPythonFileCallback (dynamic_graph_bridge_msgs::RunPythonFile::Request& req, + dynamic_graph_bridge_msgs::RunPythonFile::Response& res) + { + interpreter_.runPythonFile(req.input); + res.result = "File parsed"; // FIX: It is just an echo, is there a way to have a feedback? + return true; + } + std::string Interpreter::runCommand (const std::string& command) @@ -44,5 +59,9 @@ namespace dynamicgraph interpreter_.python(command, result, out, err); } + void Interpreter::runPythonFile( std::string ifilename ){ + interpreter_.runPythonFile(ifilename); + } + } // end of namespace dynamicgraph. diff --git a/srv/RunPythonFile.srv b/srv/RunPythonFile.srv new file mode 100644 index 0000000000000000000000000000000000000000..fa49f81a577721897f91ba9d72158fd4e430b592 --- /dev/null +++ b/srv/RunPythonFile.srv @@ -0,0 +1,3 @@ +string input +--- +string result