class Site: def __init__(self, sitePath): self.sitePath = sitePath print "Creating site at path " + sitePath # TODO: make sure the config file actually exists self.config = XMLFragment(libxml2.parseFile(sitePath + "/config/config.xml")) self.db = Database(self.config) def getConfig(self): return self.config def getDatabase(self): return self.db def getComponent(self, name): if (os.path.isdir(self.sitePath + "/components/" + name)): return Component(name, self.sitePath + "/components/") return None def getConfigValue(self, xpath, default=""): results = self.config.xpathEval(xpath) if (len(results) > 0): return results[0].content else: return default
def xpathQuery(self, ctx, sysConfig, xpath): # Get the database configuration try: db = self._getDatabase(sysConfig) if (not isinstance(xpath, str)): node = libxml2.xmlNode(_obj=xpath[0]) xpath = node.content content = db.xpathQuery(None, xpath) # See if this looks like an XML result if (content.startswith("<?xml")): doc = XMLFragment(content) # Extract the expected result set now that we've converted into libxml format. result = doc.xpathEval("/results/*") self.cleanupList.append(doc) else: doc = XMLFragment(libxml2.newDoc("1.0")) result = [doc.newDocText(content)] self.cleanupList.append(doc) return result except Exception, err: db.log.exception("DbXslExtension: Error searching")
def xpathQuery(self, txn, xpath): xmlResult = [u'<?xml version="1.0"?><results>'] files = os.listdir(self.dbPath) for file in files: doc = XMLFragment(open(self.dbPath + "/" + file).read()) results = doc.xpathEval(xpath) for result in results: result.unlinkNode() result.reconciliateNs(doc.getDocument()) content = result.serialize() if (content.startswith("<")): xmlResult.append(content) else: xmlResult.append(result.content) xmlResult.append(u"</results>") #self._applyTriggers(txn, "query", XMLFragment("\n".join(xmlResult))) return u"\n".join(xmlResult)
def respondToPost(self, transaction): request = transaction.request() response = transaction.response() entryType = request.field('type', "") fields = request.fields() try: entryDocument = DocumentBuilder(self.weblog, fields) content = entryDocument.serialize() print content # Convert the rest of the form into an XML form spec. formDocument = self._buildFormDocument(fields) errorText = "" try: # If there was a problem building the initial document we just # want to display an error. errorText = entryDocument.getErrorText() if (errorText != ""): raise ProcessingError() # Otherwise we continue processing the document. else: # Add the entry document into the form specification. formDocument.getDocument().addNode("/form/content", entryDocument.getRootElement()) # Hand the form spec to the action processor for this entry # type. content = formDocument.serialize() # print content result = self.weblog.db.runTransform(content, entryType + "/action", "", "admin/action") print result actionResult = XMLFragment(result) # If there were any errors in processing we send an error # to the user. errors = actionResult.xpathEval("/results/error-text") if (len(errors) > 0): for error in errors: errorText = error.content + "<br/>" raise ProcessingError() # Otherwise we figure ou what button was clicked so that we # forward the user to the proper place. else: button = formDocument.xpathEval("/form/button/node()")[0].name #print button style = self.getStyle(request, actionResult, "/results/action/" + button) #print style self.sendResponse(response, entryDocument, style) except ProcessingError, e: # Make sure the document actually has content and then add the # error message to it. try: root = entryDocument.getRootElement() entryDocument.getDocument().addNode("/node()/error-text", errorText, 1) except: entryDocument.getDocument().addNode("/dummy/error-text", errorText, 1) print entryDocument.serialize() style = self.getStyle(request, formDocument, "/form/action/error") self.sendResponse(response, entryDocument, style) except NotFoundError, e: doc = XMLFragment("<error-text>Document not found</error-text>") self.sendResponse(response, doc, "admin/error")