def read(source, chatty=True): """ Convert a NRML file into a validated LiteralNode object. Keeps the entire tree in memory. :param source: a file name or file object open for reading """ nrml = xmlparse(source).getroot() assert striptag(nrml.tag) == 'nrml', nrml.tag # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5') xmlns = nrml.tag.split('}')[0][1:] if xmlns != NRML05 and chatty: # for the moment NRML04 is still supported, so we hide the warning logging.debug('%s is at an outdated version: %s', source, xmlns) subnodes = [] for elem in nrml: nodecls = nodefactory[striptag(elem.tag)] try: subnodes.append(node_from_elem(elem, nodecls)) except ValueError as exc: raise ValueError('%s of %s' % (exc, source)) return LiteralNode( 'nrml', {'xmlns': xmlns, 'xmlns:gml': GML_NAMESPACE}, nodes=subnodes)
def read_lazy(source, lazytags): """ Convert a NRML file into a validated LiteralNode object. The tree is lazy, i.e. you access nodes by iterating on them. :param source: a file name or file object open for reading :param lazytags: the name of nodes which subnodes must be read lazily :returns: a list of nodes; some of them will contain lazy subnodes """ nodes = [] try: for _, el in iterparse(source, remove_comments=True): tag = striptag(el.tag) if tag in nodefactory: # NRML tag nodes.append(node_from_elem(el, nodefactory[tag], lazy=lazytags)) el.clear() # save memory except: etype, exc, tb = sys.exc_info() msg = str(exc) if str(source) not in msg: msg = "%s in %s" % (msg, source) raise_(etype, msg, tb) return nodes
def read_lazy(source, lazytags): """ Convert a NRML file into a validated LiteralNode object. The tree is lazy, i.e. you access nodes by iterating on them. :param source: a file name or file object open for reading :param lazytags: the name of nodes which subnodes must be read lazily :returns: a list of nodes; some of them will contain lazy subnodes """ nodes = [] try: for _, el in iterparse(source, remove_comments=True): tag = striptag(el.tag) if tag in nodefactory: # NRML tag nodes.append( node_from_elem(el, nodefactory[tag], lazy=lazytags)) el.clear() # save memory except: etype, exc, tb = sys.exc_info() msg = str(exc) if str(source) not in msg: msg = '%s in %s' % (msg, source) raise_(etype, msg, tb) return nodes
def read(source, chatty=True): """ Convert a NRML file into a validated LiteralNode object. Keeps the entire tree in memory. :param source: a file name or file object open for reading """ nrml = parse(source).getroot() assert striptag(nrml.tag) == "nrml", nrml.tag # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5') xmlns = nrml.tag.split("}")[0][1:] if xmlns != NRML05 and chatty: logging.warn("%s is at an outdated version: %s", source, xmlns) subnodes = [] for elem in nrml: nodecls = nodefactory[striptag(elem.tag)] subnodes.append(node_from_elem(elem, nodecls)) return LiteralNode("nrml", {"xmlns": xmlns, "xmlns:gml": GML_NAMESPACE}, nodes=subnodes)
def read(source): """ Convert a NRML file into a validated LiteralNode object. Keeps the entire tree in memory. :param source: a file name or file object open for reading """ nrml = parse(source).getroot() assert striptag(nrml.tag) == 'nrml', nrml.tag # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5') xmlns = nrml.tag.split('}')[0][1:] subnodes = [] for elem in nrml: nodecls = nodefactory[striptag(elem.tag)] subnodes.append(node_from_elem(elem, nodecls)) return LiteralNode( 'nrml', {'xmlns': xmlns, 'xmlns:gml': GML_NAMESPACE}, nodes=subnodes)
def read(source, chatty=True): """ Convert a NRML file into a validated LiteralNode object. Keeps the entire tree in memory. :param source: a file name or file object open for reading """ nrml = parse(source).getroot() assert striptag(nrml.tag) == 'nrml', nrml.tag # extract the XML namespace URL ('http://openquake.org/xmlns/nrml/0.5') xmlns = nrml.tag.split('}')[0][1:] if xmlns != NRML05 and chatty: logging.warn('%s is at an outdated version: %s', source, xmlns) subnodes = [] for elem in nrml: nodecls = nodefactory[striptag(elem.tag)] subnodes.append(node_from_elem(elem, nodecls)) return LiteralNode('nrml', { 'xmlns': xmlns, 'xmlns:gml': GML_NAMESPACE }, nodes=subnodes)