module_client.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright: Yuki Furuta <furushchev@jsk.imi.i.u-tokyo.ac.jp>
4 
5 import os
6 import sys
7 import rospy
8 from julius_ros.transport import SocketTransport
9 try:
10  from Queue import Queue ## for Python2
11 except ImportError:
12  from queue import Queue ## for Python3
13 import lxml.etree
14 import traceback
15 from xml.sax.saxutils import escape
16 
17 
19  def __init__(self, host, port, max_retry, encoding="utf-8"):
20  super(ModuleClient, self).__init__(host, port, max_retry)
21  self.encoding = encoding
22 
23  def send_command(self, cmds):
24  data = os.linesep.join(cmds) + os.linesep
25  self.send(data)
26  rospy.sleep(0.05)
27 
28  def parse(self, data):
29  parsed = data.split("." + os.linesep)
30  if len(parsed) < 2:
31  raise ValueError("Received data too short")
32  parsed_data = [self.parse_xml(d) for d in parsed[:-1]]
33  parsed_length = len(data) - len(parsed[-1])
34  return parsed_data, parsed_length
35 
36  def parse_xml(self, data):
37  try:
38  if sys.version_info.major < 3:
39  data = data.decode(self.encoding) ## Python2 needs to convert to utf-8
40  data = self.validate_xml(data)
41  xml = lxml.etree.fromstring(data)
42  return xml.tag, xml
43  except Exception as e:
44  raise RuntimeError(e)
45 
46  def validate_xml(self, data):
47  parsed = data.split('"')
48  for i in range(len(parsed))[1::2]:
49  parsed[i] = escape(parsed[i])
50  return '"'.join(parsed)
51 
52 if __name__ == '__main__':
53  pass
def __init__(self, host, port, max_retry, encoding="utf-8")


julius_ros
Author(s): Yuki Furuta
autogenerated on Tue May 11 2021 02:55:36