def doFormulas(self, parent): """ formulas are expressed as attribute values. e.g., <TxtHeight F="Height*0.861111"> variables used are 'Width' and 'Height', so these must be available to eval all elements having formulaty ("F") attrs are assigned a text value that is the result of evaluating the formula """ Width = getattr(self, 'Width') Height = self.Height # print 'Width is a %s' % type(Width) for child in XmlUtils.getChildElements(parent): f = child.getAttribute("F") if f: # print '- %s - "%s"' % (child.tagName, f) if f.startswith('NURBS'): val = f else: val = eval(f) # print " -> ", val XmlUtils.setText(child, str(val)) if XmlUtils.getChildElements(child): self.doFormulas(child)
def __init__(self, element): """ element is a row element containing an unknown number of cells. the last cell is where the data is, the others are "indents", which determin this node's "level" """ cells = XmlUtils.getChildElements(element, "TD") self.level = len(cells) - 1 dataCell = cells[-1] components = XmlUtils.getChildElements(dataCell, "A") icon = components[0] img = XmlUtils.getChild("IMG", icon) filename = os.path.split(img.getAttribute("SRC"))[1] self.type = filename.split(".")[0] self.metadatapath = webcatUtils.webcatDomain + icon.getAttribute( "HREF") linkElement = components[1] url = linkElement.getAttribute("HREF") label = XmlUtils.getText(linkElement) self.link = webcatUtils.WebCatLink((url, label)) self.title = self.link.label self.parent = None self.children = None
def __init__(self, element): self.element = element self.attrs = [] for child in XmlUtils.getChildElements(element): attr = child.tagName self.attrs.append(attr) setattr(self, attr, XmlUtils.getText(child))
def processRecord(self, rec): """ tally the fields. first make a map of occurrances for the record, then merge record map into global map """ recordData = {} for element in XmlUtils.getChildElements(rec.doc): tag = element.tagName text = XmlUtils.getText(element).strip() if not recordData.has_key(tag): recordData[tag] = 0 recordData[tag] = recordData[tag] + 1 ## now enter data into global tally for tag in recordData.keys(): if not self.has_key(tag): self[tag] = Entry(tag) entry = self[tag] entry.count = entry.count + recordData[tag] entry.max = max(entry.max, recordData[tag]) if entry.max == recordData[tag]: entry.maxRec = os.path.split(rec.path)[1] entry.min = min(entry.min, recordData[tag]) for entry in self.values(): if entry.tag in recordData.keys(): continue entry.min = 0 self.recordCount = self.recordCount + 1
def segmentToHtml(self, element, domParent): """ recursively convert segment to html and attaching to DomParent """ text = element.getAttribute("text") children = XmlUtils.getChildElements(element) id = element.getAttribute('id') node = DIV(klass="node") if id: node_label = SPAN("%s (%s)" % (text, id), klass="term label", id=id) else: node_label = SPAN(text, klass="label") if children: node_widget = IMG(src="btnExpandClsd.gif", klass="widget") else: node_widget = IMG(src="clear.gif", klass="no-widget") node_header = DIV(node_widget, node_label) node.append(node_header) if children: node_body = DIV(klass="node_body", style="display:none") node.append(node_body) for child in children: self.segmentToHtml(child, node_body) domParent.append(node) return node
def contributorFactory(element): type = XmlUtils.getChildElements(element)[0].tagName # print 'contributor type: ', type if type == 'person': return ContributorPerson(element) elif type == 'organization': return ContributorOrganization(element)
def insertFiscalYear(self, fiscalYear): """ insert proviced fiscalYear in the dom if setFiscalYear fails the first call, the instance record does not have the necessary elements, which are created """ try: self.setFiscalYear(fiscalYear) return except: pass fyEl = XmlUtils.createElement('fiscalYear') coverageNode = self.getCoverageNode() # coverageNode = self.selectSingleNode (self.dom, 'record/coverage') if not coverageNode: raise Exception, "not not found at 'record/coverage' for %s" % self.getId( ) children = XmlUtils.getChildElements(coverageNode) if children: coverageNode.insertBefore(fyEl, children[0]) else: coverageNode.appendChild(fyEl) self.setFiscalYear(fiscalYear)
def __init__(self, element): self.element = element self.id = element.getAttribute('id') for child in XmlUtils.getChildElements(element): attr = child.tagName val = XmlUtils.getText(child) setattr(self, attr, val)
def finalizeXml (self): self.doc.setAttribute ("xmlns:"+self.schema_instance_namespace, \ self.SCHEMA_INSTANCE_URI) self.setNoNamespaceSchemaLocation ( \ "http://www.dls.ucar.edu/people/ostwald/Metadata/webcat/webcat-record.xsd") accessionNum = self.getAccessionNum () url = "http://www.library.ucar.edu/uhtbin/hyperion-image/" + accessionNum urlElement = self.dom.createElement ("url") XmlUtils.setText(urlElement, url) try: id = makeId (accessionNum, self.prefix) except: id = "ERROR" msg = "Error processing " + self.url print msg print sys.exc_info()[0], sys.exc_info()[1] idElement = self.dom.createElement ("recordID") XmlUtils.setText(idElement, id) children = XmlUtils.getChildElements (self.doc) self.doc.insertBefore (urlElement, children[0]) self.doc.insertBefore (idElement, urlElement)
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 getElementHtml(self, element, level): klass = 'level-%d' % level tagName = element.tagName text = XmlUtils.getText(element) children = XmlUtils.getChildElements(element) attributes = element.attributes if not (text or attributes or children): return "" html = DIV(klass="element") if text: html.append( DIV(SPAN(tagName + ': ', klass='el-name'), SPAN(text, klass="el-text"), klass=klass)) if attributes: html.append(self.getAttributesHtml(attributes, level)) else: html.append(DIV(tagName, klass=klass)) if attributes: html.append(self.getAttributesHtml(attributes, level)) if children: for child in children: html.append(self.getElementHtml(child, level + 1)) return html
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 processRecord(self, rec): """ add namespace info add RecordID, Url elements """ rec.doc.setAttribute ("xmlns:"+rec.schema_instance_namespace, \ rec.SCHEMA_INSTANCE_URI) rec.setNoNamespaceSchemaLocation ( \ "http://www.dls.ucar.edu/people/ostwald/Metadata/webcat/webcat-record.xsd") accessionNum = self.getAccessionNum(rec) # print "%d (%s)" % (idNum, type(idNum)) # print accessionNum, id url = "http://www.library.ucar.edu/uhtbin/hyperion-image/" + accessionNum urlElement = rec.dom.createElement("Url") XmlUtils.setText(urlElement, url) id = makeId(accessionNum) idElement = rec.dom.createElement("RecordID") XmlUtils.setText(idElement, id) children = XmlUtils.getChildElements(rec.doc) rec.doc.insertBefore(urlElement, children[0]) rec.doc.insertBefore(idElement, urlElement) # print rec rec.write() print accessionNum
def get_payload(self): metadata = self.selectSingleNode(self.dom, "record:metadata") children = XmlUtils.getChildElements(metadata) if not children: raise Exception, "Could not find payload" if len(children) != 1: raise Exception, "Found multiple payload elements" return self.payload_constructor(xml=children[0].toxml())
def clearValues (self): """ remove all vocab terms """ for child in XmlUtils.getChildElements(self.restriction): # self.clearElement (child) self.restriction.removeChild (child) child.unlink()
def clearValues(self): """ remove vocabs from this enumeration """ for child in XmlUtils.getChildElements(self.restriction): # self.clearElement (child) self.restriction.removeChild(child) child.unlink()
def orderElements(self): """ enforce schema-ordering of child elements """ elements = XmlUtils.getChildElements(self.element) keyFn = lambda x:self.element_order.index(x.tagName) elements.sort (key=keyFn) for el in elements: self.element.appendChild(el)
def getTagNames(self): # print self tags = [] children = XmlUtils.getChildElements(self.doc) for child in children: # print "*", child.tagName if not child.tagName in tags: tags.append(child.tagName) return tags
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 populateXml (self, xmlData): dataRec = XmlRecord (xml=xmlData) dataElements = dataRec.getElements (dataRec.doc) for dataElement in dataElements: cells = XmlUtils.getChildElements (dataElement, "TD") name = XmlUtils.getText (cells[0]).strip() if name[-1] == ":": name = name[:-1] value = XmlUtils.getText (XmlUtils.getChild ("B", cells[1])).strip() XmlUtils.addChild (self.dom, self.normalizeTagName(name), value)
def addViewContext(self, vc): vcParent = self.selectSingleNode(self.dom, 'record:collection:viewContexts') vcNodes = XmlUtils.getChildElements(vcParent) print '%d vc nodes found' % len(vcNodes) vcValues = map(lambda x: XmlUtils.getText(x), vcNodes) for val in vcValues: print '-', val if not vc in vcValues: XmlUtils.addChild(self.dom, 'viewContext', vc, vcParent)
def getTopLevelElement(self, element, level): children = XmlUtils.getChildElements(element) if not children: return None html = DIV(klass="element") for child in children: html.append(self.getElementHtml(child, level + 1)) return html
def __init__(self, element): self.lastName = XmlUtils.getChildText(element, "lastName") self.firstName = XmlUtils.getChildText(element, "firstName") try: self.order = int(element.getAttribute("order")) except: self.order = 1000 self.role = element.getAttribute("role") self.upid = element.getAttribute("UCARid") self.affiliations = map( Affiliation, XmlUtils.getChildElements(element, 'affiliation'))
def injectComponents(self): """ inject connections after super class does its thing. connections come before TextBlock """ Shape.injectComponents(self) # connections are inserted one at a time as children of shape connectorsNode = self.components['Connections'] if not connectorsNode: raise DomException, "Connections not found in components" textBlockNodeNode = self.selectSingleNode(self.dom, 'Shape/TextBlock') if not textBlockNodeNode: raise DomException("TextXForm not found in Shape") connectors = XmlUtils.getChildElements(connectorsNode) while (connectors): connector = connectorsNode.removeChild(connectors[0]) self.doc.insertBefore(connector, textBlockNodeNode) connectors = XmlUtils.getChildElements(connectorsNode)
def makeOPML (self): for category in self.lexicon_tree.categories.values(): # print category catOutline = self.addElement (self.body, 'outline') catOutline.setAttribute ("type", "group") catOutline.setAttribute ("collapsible", "true") catOutline.setAttribute ("text", category.getAttribute("text")) for segment in XmlUtils.getChildElements(category): self.segmentToOpml (segment, catOutline) break
def processRecord(self, rec): """ tally the fields. first make a map of occurrances for the record, then merge record map into global map """ elementOrder = [] for element in XmlUtils.getChildElements(rec.doc): tag = element.tagName if not elementOrder or (tag != elementOrder[-1]): elementOrder.append(tag) self.recordCount = self.recordCount + 1 return elementOrder
def get_payload(self): """ we can't select mods (because of namespaces?) so we first grab the metadata node and build payload from first child (the mods element) """ metadata = self.selectSingleNode(self.dom, "result:metadata") if not metadata: raise Exception, "Could not find metadata" children = XmlUtils.getChildElements(metadata) if not children: raise Exception, "Could not find payload" if len(children) != 1: raise Exception, "Found multiple payload elements" return self.payload_constructor(xml=children[0].toxml())
def getTopLevelElement(self, element, level): """ represents the contents (children) of toplevel elements (but not TAG of the element), which is rendered in side-by-side display """ children = XmlUtils.getChildElements(element) if not children: return None klass = "top-level-element" html = DIV(klass=klass) for child in children: html.append(self.getElementHtml(child, level + 1)) return html
def repair (self): todelete = [] entryEls = XmlUtils.getChildElements(self.entriesElement) for entryEl in entryEls: entry = StatusEntry(entryEl.toxml()) if entry.timeStamp == 0.0: todelete.append(entryEl) if len(todelete) > 0: for el in todelete: self.entriesElement.removeChild(el) if self.dowrites: self.write() print 'repaired', self.path else: print 'WOULD HAVE repaired', self.path
def __init__(self, element): UserDict.__init__(self) if element is None: return for node in XmlUtils.getChildElements(element, "content"): field = node.getAttribute("fieldName") value = XmlUtils.getText(node) if self.has_key(field): curVal = self[field] if type(curVal) != type([]): curVal = [curVal, value] else: curVal.append(value) self[field] = curValue else: self[field] = value