def _fetchTemplate(self, url, title, bodyStr): templateService = getApplicationModel().getService( IZBlogAppServiceIDs.TEMPLATE_SERVICE_ID) template = templateService.createTemplate() templateDir = template.getTemplateDirectory() try: webPageGetter = ZHttpWebpageGetter(url, templateDir, self) webPageGetter.setBasePathToken( IZTemplateConstants.TEMPLATE_BASE_TOKEN) rootFilePath = webPageGetter.saveAsWebpage() if self.cancelled: raise ZTemplateFetchCancelledException() rootXHtmlDoc = loadXhtmlDocumentFromFile(rootFilePath) rootXHtmlDom = rootXHtmlDoc.getDom() titleElems = self._findTitleElems(rootXHtmlDom, title) if not titleElems: return None contentElems = self._findContentElems(rootXHtmlDom, bodyStr) if not contentElems: return None indexAllUTFileName = os.path.join( templateDir, u"index-all-untrimmed.html") #$NON-NLS-1$ rootXHtmlDom.save(indexAllUTFileName) if self.cancelled: raise ZTemplateFetchCancelledException() for titleElem in titleElems: titleElem.removeAllChildren() titleMarkerElem = rootXHtmlDom.createElement( u"ravenTitle", titleElem.getNamespaceUri()) #$NON-NLS-1$ titleElem.appendChild(titleMarkerElem) for contentElem in contentElems: contentElem.removeAllChildren() contentMarkerElem = rootXHtmlDom.createElement( u"ravenBody", contentElem.getNamespaceUri()) #$NON-NLS-1$ contentElem.appendChild(contentMarkerElem) rootFile = makeRelativePath(templateDir, rootFilePath) indexAllFileName = u"index-all.html" #$NON-NLS-1$ indexBodyOnlyFileName = u"index-body.html" #$NON-NLS-1$ indexTitleAndBodyFileName = u"index-titleBody.html" #$NON-NLS-1$ template.setRootFileName(rootFile) template.setAllFileName(indexAllFileName) template.setBodyOnlyFileName(indexBodyOnlyFileName) template.setTitleAndBodyFileName(indexTitleAndBodyFileName) indexAll = os.path.join(templateDir, indexAllFileName) indexTitleAndBody = os.path.join(templateDir, indexTitleAndBodyFileName) indexBodyOnly = os.path.join(templateDir, indexBodyOnlyFileName) self.listener.grabFeedback( _extstr(u"templategrabber.TemplateDownloaded_Trimming") ) #$NON-NLS-1$ if self.cancelled: raise ZTemplateFetchCancelledException() # Save the 'all' variant rootXHtmlDom.save(indexAll) if self.cancelled: raise ZTemplateFetchCancelledException() # Trim the dom - remove everything except the title and body self._trimStageOne(titleElems, contentElems) rootXHtmlDom.save(indexTitleAndBody) if self.cancelled: raise ZTemplateFetchCancelledException() # Trim the dom again - remove everything except the body self._trimStageTwo(contentElems) rootXHtmlDom.save(indexBodyOnly) if self.cancelled: raise ZTemplateFetchCancelledException() self.listener.grabFeedback( _extstr( u"templategrabber.TemplateTrimmedAndSaved")) #$NON-NLS-1$ return template except Exception, e: getLoggerService().exception(e) deleteDirectory(templateDir, True) raise e
def _fetchTemplate(self, url, title, bodyStr): templateService = getApplicationModel().getService(IZBlogAppServiceIDs.TEMPLATE_SERVICE_ID) template = templateService.createTemplate() templateDir = template.getTemplateDirectory() try: webPageGetter = ZHttpWebpageGetter(url, templateDir, self) webPageGetter.setBasePathToken(IZTemplateConstants.TEMPLATE_BASE_TOKEN) rootFilePath = webPageGetter.saveAsWebpage() if self.cancelled: raise ZTemplateFetchCancelledException() rootXHtmlDoc = loadXhtmlDocumentFromFile(rootFilePath) rootXHtmlDom = rootXHtmlDoc.getDom() titleElems = self._findTitleElems(rootXHtmlDom, title) if not titleElems: return None contentElems = self._findContentElems(rootXHtmlDom, bodyStr) if not contentElems: return None indexAllUTFileName = os.path.join(templateDir, u"index-all-untrimmed.html") #$NON-NLS-1$ rootXHtmlDom.save(indexAllUTFileName) if self.cancelled: raise ZTemplateFetchCancelledException() for titleElem in titleElems: titleElem.removeAllChildren() titleMarkerElem = rootXHtmlDom.createElement(u"ravenTitle", titleElem.getNamespaceUri()) #$NON-NLS-1$ titleElem.appendChild(titleMarkerElem) for contentElem in contentElems: contentElem.removeAllChildren() contentMarkerElem = rootXHtmlDom.createElement(u"ravenBody", contentElem.getNamespaceUri()) #$NON-NLS-1$ contentElem.appendChild(contentMarkerElem) rootFile = makeRelativePath(templateDir, rootFilePath) indexAllFileName = u"index-all.html" #$NON-NLS-1$ indexBodyOnlyFileName = u"index-body.html" #$NON-NLS-1$ indexTitleAndBodyFileName = u"index-titleBody.html" #$NON-NLS-1$ template.setRootFileName(rootFile) template.setAllFileName(indexAllFileName) template.setBodyOnlyFileName(indexBodyOnlyFileName) template.setTitleAndBodyFileName(indexTitleAndBodyFileName) indexAll = os.path.join(templateDir, indexAllFileName) indexTitleAndBody = os.path.join(templateDir, indexTitleAndBodyFileName) indexBodyOnly = os.path.join(templateDir, indexBodyOnlyFileName) self.listener.grabFeedback(_extstr(u"templategrabber.TemplateDownloaded_Trimming")) #$NON-NLS-1$ if self.cancelled: raise ZTemplateFetchCancelledException() # Save the 'all' variant rootXHtmlDom.save(indexAll) if self.cancelled: raise ZTemplateFetchCancelledException() # Trim the dom - remove everything except the title and body self._trimStageOne(titleElems, contentElems) rootXHtmlDom.save(indexTitleAndBody) if self.cancelled: raise ZTemplateFetchCancelledException() # Trim the dom again - remove everything except the body self._trimStageTwo(contentElems) rootXHtmlDom.save(indexBodyOnly) if self.cancelled: raise ZTemplateFetchCancelledException() self.listener.grabFeedback(_extstr(u"templategrabber.TemplateTrimmedAndSaved")) #$NON-NLS-1$ return template except Exception, e: getLoggerService().exception(e) deleteDirectory(templateDir, True) raise e
def _downloadTemplate(self, permaLink, template): templateDir = template.getTemplateDirectory() webPageGetter = ZHttpWebpageGetter(permaLink, templateDir, self) rootFilePath = webPageGetter.saveAsWebpage() rootFile = makeRelativePath(templateDir, rootFilePath) template.setRootFileName(rootFile)