def parse_response(xml_string): if xml_string is None: raise EInvalidValue('must be xml string') ps = dom.parseString(xml_string) de = ps.documentElement elements = de.getElementsByTagName('PointData') #this produces a list of DOM elements points = {} for point in elements: #make a map of point vs their attributes and values point_map = {} for n in point.childNodes: value = point.getElementsByTagName(n.tagName)[0].firstChild.nodeValue point_map[str(n.tagName)] = str(value) # @fixme convert unicode to str #if debug: print str(n.tagName), str(value) points[point_map['Name']] = point_map return points
def parse_response(xml_string): if xml_string is None: raise EInvalidValue('must be xml string') ps = dom.parseString(xml_string) de = ps.documentElement elements = de.getElementsByTagName( 'PointData') #this produces a list of DOM elements points = {} for point in elements: #make a map of point vs their attributes and values point_map = {} for n in point.childNodes: value = point.getElementsByTagName( n.tagName)[0].firstChild.nodeValue point_map[str(n.tagName)] = str( value) # @fixme convert unicode to str #if debug: print str(n.tagName), str(value) points[point_map['Name']] = point_map return points
def parse(m, string, filename='string'): """ Parse manifest.xml string contents @param string: manifest.xml contents @type string: str @param m: field to populate @type m: L{_Manifest} @return: return m, populated with parsed fields @rtype: L{_Manifest} """ try: d = dom.parseString(string) except Exception as e: raise ManifestException("invalid XML: %s"%e) p = get_nodes_by_name(d, m._type) if len(p) != 1: raise ManifestException("manifest must have a single '%s' element"%m._type) p = p[0] m.description = check('description')(p, filename) m.brief = '' try: tag = get_nodes_by_name(p, 'description')[0] m.brief = tag.getAttribute('brief') or '' except: # means that 'description' tag is missing pass #TODO: figure out how to multiplex if m._type == 'package': m.depends = check_depends('depend')(p, filename) elif m._type == 'stack': m.depends = check_stack_depends('depend')(p, filename) elif m._type == 'app': # not implemented yet pass m.rosdeps = check('rosdep')(p, filename) m.platforms = check('platform')(p, filename) m.exports = check('export')(p, filename) m.versioncontrol = check('versioncontrol')(p,filename) m.license = check('license')(p, filename) m.license_url = '' try: tag = get_nodes_by_name(p, 'license')[0] m.license_url = tag.getAttribute('url') or '' except: pass #manifest is missing required 'license' tag m.status='unreviewed' try: tag = get_nodes_by_name(p, 'review')[0] m.status=tag.getAttribute('status') or '' except: pass #manifest is missing optional 'review status' tag m.notes='' try: tag = get_nodes_by_name(p, 'review')[0] m.notes=tag.getAttribute('notes') or '' except: pass #manifest is missing optional 'review notes' tag m.author = check('author')(p, filename) m.url = check('url')(p, filename) m.version = check('version')(p, filename) m.logo = check('logo')(p, filename) # do some validation on what we just parsed if m._type == 'stack': if m.exports: raise ManifestException("stack manifests are not allowed to have exports") if m.rosdeps: raise ManifestException("stack manifests are not allowed to have rosdeps") # store unrecognized tags m.unknown_tags = [e for e in p.childNodes if e.nodeType == e.ELEMENT_NODE and e.tagName not in VALID] return m
def parse_manifest(manifest_name, string, filename='string'): """ Parse manifest string contents. :param manifest_name: ``MANIFEST_FILE`` or ``STACK_FILE``, ``str`` :param string: manifest.xml contents, ``str`` :param filename: full file path for debugging, ``str`` :returns: return parsed :class:`Manifest` """ if manifest_name == MANIFEST_FILE: type_ = 'package' elif manifest_name == STACK_FILE: type_ = 'stack' try: d = dom.parseString(string) except Exception as e: raise InvalidManifest("[%s] invalid XML: %s"%(filename, e)) m = Manifest(type_, filename) p = _get_nodes_by_name(d, type_) if len(p) != 1: raise InvalidManifest("manifest [%s] must have a single '%s' element"%(filename, type_)) p = p[0] m.description = _check('description')(p, filename) m.brief = '' try: tag = _get_nodes_by_name(p, 'description')[0] m.brief = tag.getAttribute('brief') or '' except: # means that 'description' tag is missing pass m.depends = _check_depends(type_, p, filename) m.rosdeps = _check_rosdeps(p, filename) m.platforms = _check_platform(p, filename) m.exports = _check_exports(p, filename) m.license = _check('license')(p, filename) m.license_url = '' try: tag = _get_nodes_by_name(p, 'license')[0] m.license_url = tag.getAttribute('url') or '' except: pass #manifest is missing required 'license' tag m.status='unreviewed' try: tag = _get_nodes_by_name(p, 'review')[0] m.status = tag.getAttribute('status') or '' except: pass #manifest is missing optional 'review status' tag m.notes = '' try: tag = _get_nodes_by_name(p, 'review')[0] m.notes = tag.getAttribute('notes') or '' except: pass #manifest is missing optional 'review notes' tag m.author = _check('author')(p, filename) m.url = _check('url')(p, filename) m.version = _check('version')(p, filename) # do some validation on what we just parsed if type_ == 'stack': if m.exports: raise InvalidManifest("stack manifests are not allowed to have exports") if m.rosdeps: raise InvalidManifest("stack manifests are not allowed to have rosdeps") # store unrecognized tags m.unknown_tags = [e for e in p.childNodes if e.nodeType == e.ELEMENT_NODE and e.tagName not in VALID] return m