def from_xml(self, content, forbid_dtd=True, forbid_entities=True): """ Given some XML data, returns a Python dictionary of the decoded data. By default XML entity declarations and DTDs will raise a BadRequest exception content but subclasses may choose to override this if necessary. """ if lxml is None: raise ImproperlyConfigured( "Usage of the XML aspects requires lxml and defusedxml.") try: # Stripping the encoding declaration. Because lxml. # See http://lxml.de/parsing.html, "Python unicode strings". content = XML_ENCODING.sub('', content) parsed = parse_xml( six.StringIO(content), forbid_dtd=forbid_dtd, forbid_entities=forbid_entities ) except (LxmlError, DefusedXmlException): raise BadRequest() return self.from_etree(parsed.getroot())
def get_data_types(plat): try: data_types = {} # save the current path base_path = os.path.abspath(os.getcwd()) # parse the mapping file data_tree = parse_xml((os.path.join(os.getcwd(), "models", "dataMapping.xml"))) data_root = data_tree.getroot() # return to the previous path os.chdir(base_path) # map the data types if any(platform.attrib.get("name") == plat for platform in data_root): for platform in data_root.iter("platform"): if platform.attrib.get("name") == plat: for dataType in platform.iter("type"): data_types[dataType.attrib.get("name")] = \ dataType.attrib.get("value") else: print("Chosen platform doesn't exist") sys.exit(1) except Exception as ex: print("Error parsing dataType mapping file") raise ex return data_types
def parse_base(self, path): generic_topics = [] try: gen_tree = parse_xml(path) gen_tree.xinclude() gen_root = gen_tree.getroot() for topic in gen_root: generic_topics.append(self.process_topic(topic, True)) except Exception as ex: print("Problem parsing generic topics") raise ex return generic_topics
def from_xml(self, content, forbid_dtd=True, forbid_entities=True): """ Given some XML data, returns a Python dictionary of the decoded data. By default XML entity declarations and DTDs will raise a BadRequest exception content but subclasses may choose to override this if necessary. """ if lxml is None: raise ImproperlyConfigured("Usage of the XML aspects requires lxml and defusedxml.") try: parsed = parse_xml(StringIO(content), forbid_dtd=forbid_dtd, forbid_entities=forbid_entities) except (LxmlError, DefusedXmlException): raise BadRequest return self.from_etree(parsed.getroot())
def from_xml(self, content, tag=None, forbid_dtd=True, forbid_entities=True): """ Given some XML data, returns a Python dictionary of the decoded data. """ if lxml is None: raise ImproperlyConfigured("Usage of the XML aspects requires lxml and defusedxml.") try: # Stripping the encoding declaration. Because lxml. # See http://lxml.de/parsing.html, "Python unicode strings". content = XML_ENCODING.sub('', content) parsed = parse_xml( six.StringIO(content), forbid_dtd=forbid_dtd, forbid_entities=forbid_entities ) except (LxmlError, DefusedXmlException): raise BadRequest() return self.from_etree(parsed.getroot(), tagName=tag)
def parse_composition(self, path): comp_tree = parse_xml(path) comp_root = comp_tree.getroot() composition = Composition(comp_root.attrib.get("name")) for model in comp_root: topic_list = [] for topic in model.findall("topic"): topic_list.append(topic.attrib.get("name")) param_list = [] for param in model.findall("param"): param_list.append(param.attrib.get("name")) model_path = os.path.join(os.getcwd(), model.attrib.get("path")) module = self.parse_file(model_path) for topic in list(module.topics): if not topic.mandatory and topic.name not in topic_list: module.topics.remove(topic) for param in list(module.params): if not param.mandatory and param.name not in param_list: module.params.remove(param) composition.modules.append(module) return composition