class Parser: def __init__(self, data=None): self.parser = ParserCreate() self.parser.Parse("<d>", 0) # dummy document element self.parser.StartElementHandler = self._start_element self.parser.EndElementHandler = self._end_element self.parser.CharacterDataHandler = self._character_data self.fragment = Fragment() self._stack = [] if data: self.parser.Parse(data, 0) def parse(self, data): self.parser.Parse(data, 0) def _start_element(self, name, attrs): self._stack.append(self.fragment) self.fragment = Element(name, attrs) def _end_element(self, name): element = self.fragment self.fragment = self._stack.pop() self.fragment.append(element) def _character_data(self, data): self.fragment.append(data)
def work(self): data, addr = self._sock.recvfrom(255) node, endpoint, profile, cluster = addr[:4] cur_time = time() is_data = endpoint == ENDPOINT and profile == PROFILE and cluster == IS_CLUSTER node_name = self._resolver.to_name_non_blocking(node, is_data) data_name = "t" data_text = data data_attrs = dict(time=fmt_time(cur_time)) other_addr = "" if endpoint == ENDPOINT and profile == PROFILE and cluster == TTY_CLUSTER: updateDataLine(node_name, data, cur_time) else: data_name = "h" data_text = str2hex(data) data_attrs["endpoint"] = hh(endpoint, 2) data_attrs["profile"] = hh(profile, 4) data_attrs["cluster"] = hh(cluster, 2) other_addr = " (" + data_attrs["endpoint"] + "," + data_attrs["profile"] + "," + data_attrs["cluster"] + ")" self.append(node, Element(data_name, data_attrs, data_text)) print "xgw: Recv ", node_name, other_addr, ": ", repr(data_text) if is_data and not is_addr(node_name): device_type, product_type = self._resolver.to_product_type_non_blocking(node) items = {} if product_type == XBeeSensorLTHAdapter: items = xbeelth.sample(data) print "xgw: Recv ", node_name, " IS sample from ", GetXBeeProductName(product_type), " = ", repr(items) updateDataItems(node_name, items, cur_time)
def __init__(self, data=None): self.parser = ParserCreate() self.parser.Parse("<d>", 0) # dummy document element self.parser.StartElementHandler = self._start_element self.parser.EndElementHandler = self._end_element self.parser.CharacterDataHandler = self._character_data self.fragment = Fragment() self._stack = [] if data: self.parser.Parse(data, 0)
try: if len(cmd) > 0: result = m(self._controller, str(Generator(cmd)), **cmd_attrs) else: result = m(self._controller, **cmd_attrs) except Exception, e: error = str(e) else: error = "Method is not found" # prepare attrs for result/error result_attrs = dict(cmd.attrs) result_attrs["for"] = cmd.name # return result/error if error: return Element("error", result_attrs, error) else: return Element("result", result_attrs, result) def callback(self, commands): print "xgw: Dispatching commands" result = ["\n"] for cmd in Parser(commands).fragment: if isinstance(cmd, Element): print "xgw: Invoking ", cmd cmd_res = self._invoke(cmd) print "xgw: Outcome ", cmd_res.name # print error or result result.append(cmd_res) result.append("\n") return str(Generator(result))
def _start_element(self, name, attrs): self._stack.append(self.fragment) self.fragment = Element(name, attrs)
def element(self): return Element("m", dict(time=fmt_time(self.time), node=self.node), [self.message])
def element(self): return Element("d", dict(tag=self.tag, time=fmt_time(self.time), node=self.node), [self.value])
"""Tests for XML modules""" from xml_generator import Generator from xml_element import Element from xml_parser import Parser print Generator([Element("test", dict(a='1', b='2'), [12345])]) assert "<test a='1' b='2'>12345</test>" == str( Generator([Element("test", dict(a='1', b='2'), [12345])])) print Parser( "<send dest='FIO1'>!RR\\r\\n</send><recv src='FIO1' wait='1'/>").fragment assert "<send dest='FIO1'>!RR\\r\\n</send><recv src='FIO1' wait='1'/>" == str( Parser("<send dest='FIO1'>!RR\\r\\n</send><recv src='FIO1' wait='1'/>"). fragment) print Parser("<send dest='FIO1'>!RR\\r\\n</send> <recv src='FIO1' wait='1'/>" ).fragment for a in Parser( "<send dest='FIO1'>!RR\\r\\n</send> <recv src='FIO1' wait='1'/>" ).fragment: print a.__class__, len(a), str(a)