import lxml, string, re, sys import lxml.etree as et from dreqPy import dreq from StringIO import StringIO ifile = 'out/annotated_20150731_chk.xml' ofile = 'test.xml' ## profile.getparent().remove(profile) from https://stackoverflow.com/questions/39337042/elementtree-remove-element def lower(x): if x == None: return '' else: return x.lower() def getkey(elem): return (lower(elem.get('label')),lower(elem.get('uid'))) class dreqAppend(object): def __init__(self,ifile,dq,uidl,rmu=None,forceRm=False): """Append list of items *uidl* to XML document ifile. These items should already be in the *dq* data request object: called y requestReview script which appends items to *dq* object""" self.doc = et.parse( ifile ) self.root = self.doc.getroot() self.chapters = self.root.getchildren() self.sections = dict() self.rmu = rmu self.forceRm = forceRm self.items = dict() for c in self.chapters[1:]: for s in c.getchildren(): tn = s.tag ix = tn.find( '}' ) if ix != -1: tn = tn[ix+1:] self.sections[tn] = s if rmu != None: for i in s.getchildren(): self.items[ i.get( 'uid' ) ] = i if rmu != None and not forceRm: s = [u for u in rmu if u not in self.items] if len(s) > 0: for x in sorted(s): print 'attempt to remove non-existant items: %s' % x assert len(s) == 0 if rmu != None: self.remove() for u in uidl: if u not in dq.inx.uid: print 'ERROR.uid.00001: uid not found: %s' % u else: this = dq.inx.uid[u] sect = this._h.label docsect = self.sections[sect] d = dict() for k in this._a.keys(): if k in this.__dict__: d[k] = str(this.__dict__[k]) ## print d i = self.root.makeelement( '{urn:w3id.org:cmip6.dreq.dreq:a}item', d ) i.tail = '\n' docsect.append( i ) def remove(self): nrm = 0 for u in self.rmu: if u in self.items: this=self.items[u] this.getparent().remove(this) nrm += 1 print 'Items removed: %s' % nrm def write(self,ofile='test.xml'): for s in self.sections: try: section = self.doc.findall('.//{urn:w3id.org:cmip6.dreq.dreq:a}%s' % s) lines = section[0] lines[:] = sorted(lines, key=getkey) except: print 'Failed to find: %s' % s raise output = StringIO() self.doc.write_c14n(output) sdoc = output.getvalue() oo = open( ofile, 'w' ) oo.write( sdoc ) oo.close()