def mergeRa(self, other): ''' Input: Two RaFile objects Output: A merged RaFile Common stanzas and key-val pairs are collapsed into one with identical values being preserved, differences are marked with a >>> and <<< ''' mergedKeys = ucscUtils.mergeList(list(self), list(other)) selfKeys = set(self) otherKeys = set(other) newCommon = RaFile() p = re.compile('^\s*#') p2 = re.compile('^\s*$') for i in mergedKeys: if p.match(i) or p2.match(i): newCommon.append(i) continue if i not in selfKeys: newCommon[i] = other[i] continue if i not in otherKeys: newCommon[i] = self[i] continue if i in otherKeys and i in selfKeys: newStanza = RaStanza() selfStanzaKeys = set(self[i].iterkeys()) otherStanzaKeys = set(other[i].iterkeys()) stanzaKeys = ucscUtils.mergeList(list(self[i]), list(other[i])) for j in stanzaKeys: if p.match(j): newStanza.append(j) continue if j not in selfStanzaKeys: newStanza[j] = other[i][j] continue if j not in otherStanzaKeys: newStanza[j] = self[i][j] continue if j in selfStanzaKeys and j in otherStanzaKeys: if self[i][j] == other[i][j]: newStanza[j] = self[i][j] else: in_j = '>>>>>%s' % j out_j = '<<<<<%s' % j newStanza[out_j] = self[i][j] newStanza[in_j] = other[i][j] newCommon[i] = newStanza return newCommon
def updateDiffFilter(self, term, other): ''' Replicates updateMetadata. Input: Term Other raFile Output: Merged RaFile Stanzas found in 'self' and 'other' that have the 'Term' in 'other' are overwritten (or inserted if not found) into 'self'. Final merged dictionary is returned. ''' ret = self common = set(self.iterkeys()) & set(other.iterkeys()) for stanza in common: if term not in self[stanza] and term not in other[stanza]: continue if term in self[stanza] and term not in other[stanza]: del ret[stanza][term] continue if term in other[stanza]: #Remake stanza to keep order of terms tempStanza = RaStanza() tempStanza._name = stanza selfKeys = list(self[stanza].iterkeys()) otherKeys = list(other[stanza].iterkeys()) newOther = list() #filter out keys in other that aren't in self, or the term we're interested in for i in otherKeys: if not i in selfKeys and i != term: continue else: newOther.append(i) #merge self keylist and filtered other list masterList = ucscUtils.mergeList(newOther, selfKeys) for i in masterList: if i == term: tempStanza[i] = other[stanza][i] else: tempStanza[i] = self[stanza][i] ret[stanza] = tempStanza return ret