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 rospy
7 from julius_ros.transport import SocketTransport
8 from Queue import Queue
9 import lxml.etree
10 import traceback
11 from xml.sax.saxutils import escape
12 
13 
15  def __init__(self, host, port, max_retry, encoding="utf-8"):
16  super(ModuleClient, self).__init__(host, port, max_retry)
17  self.encoding = encoding
18 
19  def send_command(self, cmds):
20  data = os.linesep.join(cmds) + os.linesep
21  self.send(data)
22  rospy.sleep(0.05)
23 
24  def parse(self, data):
25  parsed = data.split("." + os.linesep)
26  if len(parsed) < 2:
27  raise ValueError("Received data too short")
28  parsed_data = [self.parse_xml(d) for d in parsed[:-1]]
29  parsed_length = len(data) - len(parsed[-1])
30  return parsed_data, parsed_length
31 
32  def parse_xml(self, data):
33  try:
34  data = data.decode(self.encoding)
35  data = self.validate_xml(data)
36  xml = lxml.etree.fromstring(data)
37  return xml.tag, xml
38  except Exception as e:
39  raise RuntimeError(e)
40 
41  def validate_xml(self, data):
42  parsed = data.split('"')
43  for i in range(len(parsed))[1::2]:
44  parsed[i] = escape(parsed[i])
45  return '"'.join(parsed)
46 
47 if __name__ == '__main__':
48  pass
def __init__(self, host, port, max_retry, encoding="utf-8")


julius_ros
Author(s): Yuki Furuta
autogenerated on Wed Jul 10 2019 03:47:05