| 1 |  |  | from xml.dom.minidom import Document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from niprov.format import Format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | ## Decided not to go with prov python lib as it depends on lxml which depends on c binaries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class XmlFormat(Format): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     def __init__(self, dependencies): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |         super(XmlFormat, self).__init__(dependencies) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         self.fileExtension = 'xml' | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     def serializeSingle(self, item): | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |         return self.serializeList([item]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     def serializeList(self, itemOrList): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         prov = 'http://www.w3.org/ns/prov#' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         nfo = 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         dct = 'http://purl.org/dc/terms/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         dom = Document() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         doc = dom.createElementNS(prov, 'prov:document') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         doc.setAttribute('xmlns:prov', prov) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         doc.setAttribute('xmlns:nfo', nfo) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         doc.setAttribute('xmlns:dct', dct) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         dom.appendChild(doc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         for i, item in enumerate(itemOrList): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             entity = dom.createElementNS(prov, 'prov:entity') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             entityId = 'niprov:file'+str(i) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             entity.setAttribute('id', entityId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             fileUrl = dom.createElementNS(nfo, 'nfo:fileUrl') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             fileUrlVal = dom.createTextNode(item.location.toUrl()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             fileUrl.appendChild(fileUrlVal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             entity.appendChild(fileUrl) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             if 'size' in item.provenance: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 fileSize = dom.createElementNS(nfo, 'nfo:fileSize') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 fileSizeVal = dom.createTextNode(str(item.provenance['size'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 fileSize.appendChild(fileSizeVal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 entity.appendChild(fileSize) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             if 'created' in item.provenance: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 fileLastMod = dom.createElementNS(nfo, 'nfo:fileLastModified') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 fileLastModVal = dom.createTextNode(item.provenance['created'].isoformat()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 fileLastMod.appendChild(fileLastModVal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 entity.appendChild(fileLastMod) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             if 'hash' in item.provenance: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 fileHash = dom.createElementNS(nfo, 'nfo:FileHash') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 hashId = entityId+'.hash' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 fileHash.setAttribute('id', hashId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 hashAlgo = dom.createElementNS(nfo, 'nfo:hashAlgorithm') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 hashAlgoVal = dom.createTextNode('MD5') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 hashAlgo.appendChild(hashAlgoVal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 fileHash.appendChild(hashAlgo) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 hashValue = dom.createElementNS(nfo, 'nfo:hashValue') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 hashValueVal = dom.createTextNode(item.provenance['hash']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 hashValue.appendChild(hashValueVal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 fileHash.appendChild(hashValue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 hasHash = dom.createElementNS(nfo, 'nfo:hasHash') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 hasHashVal = dom.createTextNode(hashId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 hasHash.appendChild(hasHashVal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 entity.appendChild(hasHash) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 doc.appendChild(fileHash) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             if 'transformation' in item.provenance: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 act = dom.createElementNS(prov, 'prov:activity') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 activityId = entityId+'.xform' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 act.setAttribute('id', activityId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 actTitle = dom.createElementNS(dct, 'dct:title') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 actTitleVal = dom.createTextNode(item.provenance['transformation']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 actTitle.appendChild(actTitleVal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 act.appendChild(actTitle) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 doc.appendChild(act) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 wasGen = dom.createElementNS(prov, 'prov:wasGeneratedBy') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 entityRef = dom.createElementNS(prov, 'prov:entity') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 entityRef.setAttribute('prov:ref', entityId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 wasGen.appendChild(entityRef) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 activityRef = dom.createElementNS(prov, 'prov:activity') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 activityRef.setAttribute('prov:ref', activityId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 wasGen.appendChild(activityRef) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 doc.appendChild(wasGen) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 if 'created' in item.provenance: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     wasGenTime = dom.createElementNS(prov, 'prov:time') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     wasGenTimeVal = dom.createTextNode(item.provenance['created'].isoformat()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     wasGenTime.appendChild(wasGenTimeVal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     wasGen.appendChild(wasGenTime) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             doc.appendChild(entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return dom.toprettyxml(encoding="UTF-8") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  |