def parsexml(self, xmlstring, modules, source=None): """Parses the docstrings out of the specified xml file. :arg source: the path to the file from which the XML string was extracted. """ result = {} from fortpy.utility import XML_fromstring xmlroot = XML_fromstring(xmlstring, source) if xmlroot.tag == "fortpy" and "mode" in xmlroot.attrib and \ xmlroot.attrib["mode"] == "docstring": #We fill the dictionary with decorates names as keys and lists #of the xml docstring elements as values. for child in xmlroot: xmltags = [] if child.tag == "decorates" and "name" in child.attrib: decorates = child.attrib["name"] xmltags.extend(list(child)) elif "decorates" in child.attrib: decorates = child.attrib["decorates"] xmltags.append(child) if decorates in result: result[decorates].extend(xmltags) else: result[decorates] = xmltags #Loop through all the docstrings we found and team them up with #their respective module members. self._xml_update_modules(result, modules)
def parsexml(self, xmlstring, modules, source=None): """Parses the docstrings out of the specified xml file. :arg source: the path to the file from which the XML string was extracted. """ result = {} from fortpy.utility import XML_fromstring xmlroot = XML_fromstring(xmlstring, source) if xmlroot.tag == "fortpy" and "mode" in xmlroot.attrib and \ xmlroot.attrib["mode"] == "docstring": #First, cycle through the kids to find the <global> tag (if any #exist). It's children will apply to any of the other tags we find #and we will have to update their attributes accordingly. xmlglobals = {} for child in xmlroot.iterfind("globals"): _update_globals(list(child), xmlglobals) _set_global_defaults(xmlglobals) #We fill the dictionary with decorates names as keys and lists #of the xml docstring elements as values. for child in xmlroot: if child.tag == "globals": continue xmltags = [] if child.tag == "decorates" and "name" in child.attrib: decorates = child.attrib["name"] xmltags.extend(list(child)) elif "decorates" in child.attrib: decorates = child.attrib["decorates"] xmltags.append(child) for xtag in xmltags: _update_from_globals(xtag, xmlglobals, child) if decorates in result: result[decorates].extend(xmltags) else: result[decorates] = xmltags #Loop through all the docstrings we found and team them up with #their respective module members. self._xml_update_modules(result, modules)
def _get_fortpy(self, line1, source): """Extracts the fortpy tag from the first line of the file. :arg source: the path to the file from which the line was extracted. """ if "#" in line1: from fortpy.utility import XML_fromstring try: lxml = XML_fromstring(line1.split("#")[1], source) if lxml.tag == "fortpy": return lxml else: return None except ET.ParseError as err: msg.warn(err.msg, 2) msg.warn("no version information found in the file. Assuming version 1.", 2) return None else: msg.warn("no version information found in the file. Assuming version 1.", 2) return None
def _xml_load(self): """Loads the XML file and splits the template entries based on version numbers.""" with open(self.filepath) as f: lines = f.read() from fortpy.utility import XML_fromstring root = XML_fromstring(lines, self.filepath) #The first element must be a fortpy with attribute template #otherwise give a message about it being an invalid template file if "mode" in root.attrib and root.attrib["mode"] == "template": #See if we have multiple versions to work with or if it is a #straight laced template versions = xml_get_versions(root) #Create a contents object for each version specified in the file. for v in versions: if v not in self.contents: self.contents[v] = TemplateContents() self._xml_load_versions(root) else: msg.warn("The XML template {} is not".format(self.filepath) + " a valid fortpy template file.")