# Utilities for combining multiple module source files # into one. from xml.dom import minidom from swordutils.xml import utils class LazyNodes(object): # Pulling all the documents in at once uses up too much memory. # This class is responsible for acting as a replacement # 'childNodes' which loads documents one at a time, # does corrections on them and spews out the body nodes def __init__(self, maindoc, files, alterationfunc, nodepath): self.maindoc = maindoc # Don't actually need this self.files = files self.iterated_count = 0 self.nodepath = nodepath self.alterationfunc = alterationfunc def __iter__(self): self.iterated_count += 1 if self.iterated_count == 2: # We've got a big performance bug if this happens. raise Exception('Performance bug') for f in self.files: doc = minidom.parse(f) self.alterationfunc(doc) body = utils.getNodesFromXPath(doc, self.nodepath)[0] for n in body.childNodes: yield n