def action(osmRecord): """ - remember the status value - remove the status node - ?? create an empty date element if one does not exist for this status value ?? """ if verbose: print '\n-- task 11 action ---' modified = False copyrightNotice = osmRecord.selectSingleNode( osmRecord.dom, "record/rights/copyrightNotice") if not copyrightNotice: # raise Exception, "I have to have a copyright!" rights = osmRecord.selectSingleNode(osmRecord.dom, 'record/rights') if not rights: rights = XmlUtils.addElement(osmRecord.dom, osmRecord.doc, 'rights') copyrightNotice = XmlUtils.addElement(osmRecord.dom, rights, 'copyrightNotice') copyrightNotice.setAttribute('holder', 'UCAR') copyrightNotice.setAttribute('url', termsOfUseUrl) if verbose: print copyrightNotice.toxml() XmlUtils.setText(copyrightNotice, copyrightBlurb) modified = True return modified
def finalizeXml(self): """ doctor the metadata with information contained in the folderNode """ if not self.node: return # get TN issue from title, or if not from parent's title self.tn_issue = self.getTN() or self.tn_issue if self.tn_issue: print "ADDING %s" % self.tn_issue tnElement = XmlUtils.addElement(self.dom, self.doc, "tn_isssue") XmlUtils.setText(tnElement, self.tn_issue) self.title = webcatUtils.stripIssue(self.title, self.tn_issue) self.setFieldValue("title", self.title) childrenElement = XmlUtils.addElement(self.dom, self.doc, "children") for child in self.node.children: # XmlUtils.addChild (self.dom, "child", child.title, childrenElement) md = child.getMetadata(None) id = md.getAccessionNum() print id childElement = XmlUtils.addChild(self.dom, "child", child.title, childrenElement) childElement.setAttribute("accessionNum", id) children = XmlUtils.getChildElements(self.doc) self.doc.appendChild(childrenElement)
def makeProvsOutlineOLD(self): rec = XmlRecord(xml="<body/>") body = rec.doc typeDefs = self.xsdMgr.xsd.getProvTypes() # typeDefs.sort (self.provTypeSortFn) for typeDef in typeDefs: print typeDef.countryName continue provItems = typeDef.getValues() item = provItems[ 0] # all provItems share the same country information # print "**", typeDef.__class__.__name__ countryOutline = XmlUtils.addElement(rec.dom, body, "outline") countryOutline.setAttribute("type", "group") countryOutline.setAttribute("text", item.countryName) #countryOutline.setAttribute ("vocab", item.countryCode) # don't think we want to do this for provTerm in provItems: provOutline = XmlUtils.addElement(rec.dom, countryOutline, "outline") provOutline.setAttribute("type", "vocab") provOutline.setAttribute("text", provTerm.provName) provOutline.setAttribute( "vocab", provTerm.provCode) # don't think we want to do this return rec
def setPubName (self, pubName, pubNameType=None): """ creates pubName element if one doesn't exist ASSUMES A TITLE ELEMENT EXISTS for proper insertion (inserts following title element) """ if not self.selectSingleNode (self.dom, self.xpaths['pubName']): # create pubNamem element now, but we populate later pubNameEl = XmlUtils.createElement("pubName") ## now find where to insert pubNameEl generalEl = self.selectSingleNode (self.dom, 'record/general') if not generalEl: raise Exception, "record does not contain a general element" gen_children = XmlUtils.getChildElements(generalEl) print "%d elements found" % len(gen_children) targetEl = None for child in gen_children: print ' - ', child.tagName if not child.tagName in ['recordID', 'recordDate', 'urlOfRecord']: targetEl = child print "target is %s" % child.tagName break if targetEl: # insert after targetEl generalEl.insertBefore (pubNameEl, targetEl) else: # insert at end of general element XmlUtils.addElement (self.dom, generalEl, 'pubName') self.set ('pubName', pubName) if pubNameType: self.setPubNameType (pubNameType)
def makeRecord(self): rec = XmlRecord(xml=self.record_template) XmlUtils.addChild(rec.dom, "date", asctime(localtime())) XmlUtils.addChild(rec.dom, "recordSource", self.record_source) collections = XmlUtils.addElement(rec.dom, rec.doc, "collections") for key in self.keys(): collection = XmlUtils.addElement(rec.dom, collections, "collection") collection.setAttribute("key", key) collection.setAttribute("prefix", self[key].getIdPrefix()) return rec
def write(self): rec = self.getBlankRec() for url in self.keys(): node = XmlUtils.addElement(rec.dom, rec.doc, 'entry') node.setAttribute('url', url) node.setAttribute('id', self[url]) rec.write()
def __init__(self): XmlRecord.__init__(self, xml="<positionHistory/>") self.data = JoinedData() for upid in self.data.keys(): # print upid personEl = XmlUtils.addElement(self.dom, self.doc, 'person') personEl.setAttribute('upid', str(upid)) for i, ivRec in enumerate(self.data[upid]): if i == 0: personEl.setAttribute('peid', str(ivRec.peid)) posEl = XmlUtils.addElement(self.dom, personEl, 'position') for attr in [ 'start', 'end', 'entity', 'lab', 'org', 'divProg', 'divCode' ]: posEl.setAttribute(attr, str(getattr(ivRec, attr)))
def getRootChild (self, childName): if not childName in self.root_child_order: raise Exeption, 'Unrecognized childName: "%s"' % childName child = self.selectSingleNode(self.dom, 'record/'+childName) if not child: child = XmlUtils.addElement(self.dom, self.doc, childName) XmlUtils.orderElements(self.doc, self.root_child_order) return child
def finalizeXml(self): if not self.node: return childrenElement = XmlUtils.addElement(self.dom, self.doc, "children") for child in self.node.children: XmlUtils.addChild(self.dom, "child", child.title, childrenElement) children = XmlUtils.getChildElements(self.doc) self.doc.appendChild(childrenElement)
def addDleseSupportContact(self): contacts_el = self.selectSingleNode(self.dom, 'record:collection:contacts') if not contacts_el: raise Exception, 'contacts node not found' el = XmlUtils.addElement(self.dom, contacts_el, 'contact') el.setAttribute("email", '*****@*****.**') el.setAttribute("name", 'DLESE Support') el.setAttribute("urlReport", 'false') el.setAttribute("active", 'true')
def addGeneralChild (self, childName): """ add a named child element to the /record/general node """ if not childName in self.general_child_order: raise Exeption, 'Unrecognized childName: "%s"' % childName general = self.getRootChild('general') child = XmlUtils.addElement(self.dom, general, childName) XmlUtils.orderElements(general, self.general_child_order) return child
def makeCountriesOutline(self): # keys = self.xsdMgr.countryCodes.keys() # keys.sort (self.countryTermSortFn) rec = XmlRecord(xml="<body/>") body = rec.doc countriesOutline = XmlUtils.addElement(rec.dom, body, "outline") countriesOutline.setAttribute("type", "group") countriesOutline.setAttribute("text", "Countries") countryCodes = self.xsdMgr.countryCodes.values() countryCodes.sort(self.countryTermSortFn) for countryTerm in countryCodes: countryOutline = XmlUtils.addElement(rec.dom, countriesOutline, "outline") countryOutline.setAttribute("type", "vocab") countryOutline.setAttribute("text", countryTerm.countryName) countryOutline.setAttribute( "vocab", countryTerm.countryCode) # don't think we want to do this return rec
def toxml(self): """ make an xml document containing id and dcslastTouchDate value for each record """ rec = XmlRecord(xml="<lastTouchInfo></lastTouchInfo>") rec.doc.setAttribute("collection", self.collection) for result in self: el = XmlUtils.addElement(rec.dom, rec.doc, 'rec') for key in result.keys(): el.setAttribute(key, result[key]) dest = "RAW_lastTouchData/%s.xml" % self.collection rec.write(dest) print "wrote to ", dest
def asXml(self, prefix=None): doc = XmlUtils.createDocument("record") root = doc.documentElement addChild = XmlUtils.addChild addChild(doc, "id", self.getRecordId(prefix)) addChild(doc, "title", self.title) addChild(doc, "box", self.box) addChild(doc, "folder", self.box) addChild(doc, "extent", self.extent) if self.hasDigitalObject(): digi = XmlUtils.addElement(doc, root, "digitalObject") addChild(doc, "href", self.dao.href, digi) addChild(doc, "title", self.dao.title, digi) return doc
def asXml(self): rec = XmlRecord(xml="<orgchart/>") root = rec.doc for org in self.values(): orgNode = rec.addElement(root, "node") orgNode.setAttribute("id", str(org.id)) # print "children: %s" % org.children children = XmlUtils.addElement(rec.dom, orgNode, "children") for child in org.children: # print "child: ", child XmlUtils.addChild(rec.dom, "child", str(child), children) XmlUtils.addChild(rec.dom, "parent", str(org.parent), orgNode) for attr in ['acronym', 'full_name', 'active', 'org_level_id']: val = getattr(org.orgRec, attr) XmlUtils.addChild(rec.dom, attr, str(val), orgNode) # print root.toxml() return rec
def addContact(self, dleseContributor): """ assumes there is a "contacts" node this is what a contact node looks like <contact email="*****@*****.**" name="DLESE Support" urlReport="false" active="true"/> hey ... should ALL the dlese contacts be DLESE support?? """ contacts_el = self.selectSingleNode(self.dom, 'record:collection:contacts') if not contacts_el: raise Exception, 'contacts node not found' el = XmlUtils.addElement(self.dom, contacts_el, 'contact') el.setAttribute("email", dleseContributor.getEmail()) el.setAttribute("name", dleseContributor.getFullName()) el.setAttribute("urlReport", 'false') el.setAttribute("active", 'false')
def toxml(self, path=None): """ write this map as xml to a file in the "grouping_data" directory, named after the classname """ if path is None: path = 'grouping_data/' + self.__class__.__name__ + '.xml' rec = XmlRecord(xml="<dupGroups/>") rec.doc.setAttribute('name', self.__class__.__name__) groupNum = 1 for key in self.getReportingKeys(): groupEl = XmlUtils.addElement(rec.dom, rec.doc, 'group') groupEl.setAttribute('key', key) groupEl.setAttribute('size', str(len(self[key]))) groupEl.setAttribute('groupNum', str(groupNum)) for recInfo in self[key]: groupEl.appendChild(recInfo.element.cloneNode(1)) groupNum = groupNum + 1 rec.write(path) print 'wrote to ', path
def convert(path, useTestSchema=True): rec = OsmRecord(path=path) changed = 0 if useTestSchema: rec.setSchemaLocation( 'http://test.nldr.library.ucar.edu/metadata/osm/1.1/schemas/osm.xsd', 'http://nldr.library.ucar.edu/metadata/osm') for contrib in rec.getContributorPeople() + rec.getContributorOrgs(): # print "\nBEFORE\n",contrib.element.toxml() for level in contrib.affiliationLevels: suffix = contrib.getSuffix(level) if contrib.hasAffiliationLevel(level): changed = 1 affiliation = XmlUtils.addElement(rec.dom, contrib.element, 'affiliation') for baseFieldName in contrib.affiliationFields: field = baseFieldName + suffix child2delete = contrib.element.getElementsByTagName(field) if child2delete: for e in child2delete: contrib.element.removeChild(e) value = getattr(contrib, field) # print "value: '%s' (%s)" % (value, type(value)) if value: if type(value) != type([]): value = [value] for item in value: try: XmlUtils.addChild(rec.dom, baseFieldName, item, affiliation) except TypeError: print "\nCouldn't insert %s in %s: %s" % ( item, baseFieldName, sys.exc_info()[1]) sys.exit() # print "\nAFTER\n",prettyElement(contrib.element) return rec
def setDate (self, dateStr, dateType): coverageNode = self.selectSingleNode (self.dom, 'record/coverage') if not coverageNode: #raise Exception, "no coverage node found" coverageNode = XmlUtils.addElement(self.dom, self.doc, "coverage") targetDateElement = None dateNodes = self.getDateNodes() if dateNodes: for node in dateNodes: if node.hasAttribute (dateType): targetDateElement = node if targetDateElement is None: targetDateElement = XmlUtils.createElement ("date") targetDateElement.setAttribute ("type", dateType) coverageChildren = XmlUtils.getChildElements (coverageNode) if coverageChildren: firstChild = coverageChildren[0] coverageNode.insertBefore (targetDateElement, firstChild) else: coverageNode.appendChild (targetDateElement) XmlUtils.setText (targetDateElement, dateStr) return targetDateElement
def getRightsNode(self): rightsNode = self.selectSingleNode(self.dom, 'record/rights') if not rightsNode: rightsNode = XmlUtils.addElement(self.dom, self.doc, "rights") return rightsNode
def getCoverageNode(self): coverageNode = self.selectSingleNode(self.dom, 'record/coverage') if not coverageNode: coverageNode = XmlUtils.addElement(self.dom, self.doc, "coverage") return coverageNode