def analyseText(self, node): if not self.inBody or len(self.summary) > self.maxLength: return text = convertToUnicode(node.nodeValue) if text: # use transformer to remove newlines and repeating white spaces. self.summary = self.normalizer.transform(self.summary + text)
def getUrlFromShortcut(shortcutFile): u"""Returns tuple (url, name) given a IE shortcut file or (None,None) if failed.""" #$NON-NLS-1$ url = None fname = None if not shortcutFile or not shortcutFile.lower().endswith( u".url"): #$NON-NLS-1$ return (None, None) try: fname = shortcutFile.replace( u'\\', u'/').split(u'/')[-1] #$NON-NLS-1$ #$NON-NLS-2$ #$NON-NLS-3$ (fname, fExt) = os.path.splitext(fname) #@UnusedVariable file = open(shortcutFile, u'r') #$NON-NLS-1$ count = 0 scFound = False for line in file.readlines(): if not line or count > 25: break count = count + 1 line = line.strip() if line == u"[InternetShortcut]": #$NON-NLS-1$ scFound = True continue if scFound and line.lower().startswith(u"url="): #$NON-NLS-1$ url = line[4:] break file.close() except Exception: pass if fname: fname = convertToUnicode(fname) return (url, fname)
def analyseText(self, node): if not self.inBody or len(self.summary) > self.maxLength: return text = convertToUnicode(node.nodeValue) if text: # use transformer to remove newlines and repeating white spaces. self.summary = self.normalizer.transform( self.summary + text)
def _logData(self, prefix, data, ext): #@UnusedVariable if self.getLogger() and data: fname = u"atom_" + prefix + u"_" + time.strftime(str(u"%Y-%m-%dT%H.%M.%SZ"), time.gmtime()) + u"." + ext #$NON-NLS-4$ #$NON-NLS-3$ #$NON-NLS-2$ #$NON-NLS-1$ data = convertToUnicode(data) self.getLogger().logData(fname, data) else: self._error(u"No data found for _logData.") #$NON-NLS-1$
def loadUnicodeContent(fileName, enc=None): checkFile(fileName) if not enc: enc = u"utf-8" #$NON-NLS-1$ file = None text = None try: file = codecs.open(fileName, u"r", enc, u"replace") #$NON-NLS-1$ #$NON-NLS-2$ text = file.read() file.close() # if unicode string begins with the BOM (Py 2.3 bug?) remove the BOM character. if text and text[0] == unicode(codecs.BOM_UTF8, u"utf-8"): #$NON-NLS-1$ text = text.lstrip(unicode(codecs.BOM_UTF8, u"utf-8")) #$NON-NLS-1$ except: # try loading without codecs try: file = open(fileName, u'r') #$NON-NLS-1$ text = file.read() file.close() text = convertToUnicode(text) except: raise return text
def getUrlFromShortcut(shortcutFile): u"""Returns tuple (url, name) given a IE shortcut file or (None,None) if failed.""" #$NON-NLS-1$ url = None fname = None if not shortcutFile or not shortcutFile.lower().endswith(u".url"): #$NON-NLS-1$ return (None,None) try : fname = shortcutFile.replace(u'\\', u'/').split(u'/')[-1] #$NON-NLS-1$ #$NON-NLS-2$ #$NON-NLS-3$ (fname, fExt) = os.path.splitext(fname) #@UnusedVariable file = open(shortcutFile, u'r') #$NON-NLS-1$ count = 0 scFound = False for line in file.readlines(): if not line or count > 25: break count = count + 1 line = line.strip() if line == u"[InternetShortcut]": #$NON-NLS-1$ scFound = True continue if scFound and line.lower().startswith(u"url="): #$NON-NLS-1$ url = line[4:] break file.close() except Exception: pass if fname: fname = convertToUnicode(fname) return (url, fname)
def setText(self, value): value = convertToUnicode(value) try: self.attr.nodeValue = value except Exception, e: raise ZException(u"Error setting the text of an attribute.", e) #$NON-NLS-1$
def boundary(): u"""Generate a random boundary, a bit like Python 2.5's uuid module.""" #$NON-NLS-1$ bytes = os.urandom(16) b64str = base64.b64encode(bytes, str(u'ab')) #$NON-NLS-1$ b64stru = convertToUnicode(b64str) return b64stru.strip(u'=') #$NON-NLS-1$
def _format(zschemaDateTime, pattern, localTime=False): if not pattern: pattern = u"%c"#$NON-NLS-1$ if zschemaDateTime is not None: return convertToUnicode(zschemaDateTime.toString(pattern, localTime)) else: return u"" #$NON-NLS-1$
def _internalPing(self, extendedPing, pingServerUrl, weblogName, weblogUrl, checkUrl=None, rssUrl=None): pingServerUrl = getNoneString(pingServerUrl) weblogName = getNoneString(weblogName) weblogUrl = getNoneString(weblogUrl) checkUrl = getSafeString(checkUrl).strip() rssUrl = getSafeString(rssUrl).strip() if pingServerUrl is None: return ZWeblogPingResponse( False, u"Weblog ping URL is required.") #$NON-NLS-1$ if weblogName is None: return ZWeblogPingResponse( False, u"Weblog name or post title is required.") #$NON-NLS-1$ if weblogUrl is None: return ZWeblogPingResponse( False, u"Weblog URL or post permanent link is required." ) #$NON-NLS-1$ if extendedPing and (checkUrl == u"" or rssUrl == u""): #$NON-NLS-1$ #$NON-NLS-2$ return ZWeblogPingResponse( False, u"URL to RSS feed or Check URL parameter is required for ExtendedPings." ) #$NON-NLS-1$ success = False message = u"" #$NON-NLS-1$ try: remoteServer = Server(pingServerUrl) result = None if extendedPing: result = remoteServer.weblogUpdate.extendedPing( weblogName, weblogUrl, checkUrl, rssUrl) else: result = remoteServer.weblogUpdates.ping(weblogName, weblogUrl) if result is not None and result.has_key(u"flerror"): #$NON-NLS-1$ success = not result[u"flerror"] #$NON-NLS-1$ if result is not None and result.has_key(u"message"): #$NON-NLS-1$ message = result[u"message"] #$NON-NLS-1$ elif not result or not result.has_key(u"message"): #$NON-NLS-1$ message = u"Weblog ping response message not available after pinging %s" % pingServerUrl #$NON-NLS-1$ except Fault, fault: fcode = u"" #$NON-NLS-1$ if fault.faultCode: fcode = unicode(fault.faultCode) fstr = u"" #$NON-NLS-1$ if fault.faultString: fstr = convertToUnicode(fault.faultString) success = False message = u"Weblog ping faulted when pinging %s (code: %s, reason: %s)" % ( pingServerUrl, fcode, fstr) #$NON-NLS-1$
def _fetchContent(self, url): u"""Retrieves the text content from the url.""" #$NON-NLS-1$ request = ZSimpleHTTPRequest(url) content = None if request.send(): content = request.getResponse() content = convertToUnicode(content) return content
def toUnicode(s): rVal = None if s is not None: if isinstance(s,basestring): rVal = convertToUnicode(s) elif isinstance(s, zoundry.blogpub.xmlrpc.zpatch.xmlrpclib.Binary): rVal = convertToUnicode(str(s)) else: try: rVal = unicode(s) except: try: rVal = convertToUnicode(str(s)) except: pass if rVal: return rVal else: return u"" #$NON-NLS-1$
def toUnicode(s): rVal = None if s is not None: if isinstance(s, basestring): rVal = convertToUnicode(s) elif isinstance(s, zoundry.blogpub.xmlrpc.zpatch.xmlrpclib.Binary): rVal = convertToUnicode(str(s)) else: try: rVal = unicode(s) except: try: rVal = convertToUnicode(str(s)) except: pass if rVal: return rVal else: return u"" #$NON-NLS-1$
def _parsePatternThree(self, match, remainingParams): key = match.group(1).lower() value = None rval = 1 if remainingParams: arg2 = convertToUnicode(remainingParams[0]) if not arg2.startswith(u"-"): #$NON-NLS-1$ value = arg2 rval = 2 self.paramMap[key] = value return rval
def _getAttribute(self, name): rval = None if self.node.nodeType == 1 and name: if name == u"style": #$NON-NLS-1$ rval = self.node.style.cssText else: attrs = self.node.attributes namedItem = attrs.getNamedItem(name) if namedItem: rval = convertToUnicode(namedItem.value) return rval
def loadStaticHtml(path, params=None): if not os.path.isfile(path): raise ZBlogAppException(_extstr(u"viewutil.HtmlFileNotFoundError") % path) # $NON-NLS-1$ f = open(path, u"rb") # $NON-NLS-1$ html = f.read() f.close() rval = convertToUnicode(html) if params: rval = rval % params return rval
def loadStaticHtml(path, params=None): if not os.path.isfile(path): raise ZBlogAppException( _extstr(u"viewutil.HtmlFileNotFoundError") % path) #$NON-NLS-1$ f = open(path, u"rb") #$NON-NLS-1$ html = f.read() f.close() rval = convertToUnicode(html) if params: rval = rval % params return rval
def _setAttribute(self, name, value): if self.node.nodeType == 1 and name and value is not None: value = convertToUnicode(value) if name == u"style": #$NON-NLS-1$ self.node.style.cssText = value else: attrs = self.node.attributes # remove old attr attrs.removeNamedItem(name) # create new attr namedItem = self.ownerDom.createAttribute(name) namedItem.value = value attrs.setNamedItem(namedItem);
def _setAttribute(self, name, value): if self.node.nodeType == 1 and name and value is not None: value = convertToUnicode(value) if name == u"style": #$NON-NLS-1$ self.node.style.cssText = value else: attrs = self.node.attributes # remove old attr attrs.removeNamedItem(name) # create new attr namedItem = self.ownerDom.createAttribute(name) namedItem.value = value attrs.setNamedItem(namedItem)
def getTextFromClipboard(): u"""Returns clipboard text if available or None otherwise.""" #$NON-NLS-1$ text = None try: data = wx.TextDataObject() wx.TheClipboard.Open() ok = wx.TheClipboard.GetData(data) wx.TheClipboard.Close() if ok: text = data.GetText() if text: text = convertToUnicode(text) except: pass return text
def _decodeCfHtml(self, cfHtml): cfHtml = convertToUnicode(cfHtml) # Try the extended format first (which has an explicit selection) matches = MARKER_BLOCK_EX_RE.match(cfHtml) if matches: html = cfHtml[int(matches.group(6)):int(matches.group(7))] return html else: # Failing that, try the version without a selection matches = MARKER_BLOCK_RE.match(cfHtml) if matches: html = cfHtml[int(matches.group(4)):int(matches.group(5))] return html return None
def _getFileMetaData(self, fileName): u"""Returns (url, shortName, absPath, size, schemaDate).""" #$NON-NLS-1$ url = None shortName = None absPath = None size = None schemaDate = None try: (shortName, absPath, size, schemaDate) = getFileMetaData(fileName) #@UnusedVariable if shortName: shortName = convertToUnicode(shortName) url = getUriFromFilePath(absPath) except: pass return (url, shortName, absPath, size, schemaDate)
def _internalRunTidy(htmlSrc, options=XHTML_OPTIONS): # Runs tidy and returns tuple (html, errorList) unsupportedOptions = ["raw", "output_error", "show_warnings"] try: # remove unsupported options. if options: options['tidy_mark'] = 0 for s in unsupportedOptions: if options.has_key(s): del options[s] except: pass lineOffset = 0 if htmlSrc: # escape illegal entities. E.g. convert &##! to &##! try: htmlSrc = ILLEGAL_ENTITY_RE.sub(u"&\g<2>", htmlSrc) #$NON-NLS-1$ except: pass if not hasBody(htmlSrc): # wrap content inside a <html><head/><body> [CONTENT] </body></html> htmlSrc = XHTML_TEMPLATE % htmlSrc lineOffset = XHTML_TEMPLATE_LINE_OFFSET tidySrc = convertToUtf8(htmlSrc) tidyRet = tidy.parseString(tidySrc, **options) errList = [] severities = dict(W=ZTidyError.WARN, E=ZTidyError.ERROR, C=ZTidyError.OTHER) for err in tidyRet.get_errors(): te = ZTidyError() if err.line is not None: te.line = err.line - lineOffset if err.col is not None: te.col = err.col if err.message is not None: te.message = err.message te.severity = ZTidyError.NONE if severities.has_key(err.severity): te.severity = severities[err.severity] errList.append(te) outHtml = str(tidyRet) return (convertToUnicode(outHtml), errList)
def _internalRunTidy(htmlSrc, options = XHTML_OPTIONS): # Runs tidy and returns tuple (html, errorList) unsupportedOptions = ["raw", "output_error", "show_warnings"] try: # remove unsupported options. if options: options['tidy_mark'] = 0 for s in unsupportedOptions: if options.has_key(s): del options[s] except: pass lineOffset = 0 if htmlSrc: # escape illegal entities. E.g. convert &##! to &##! try: htmlSrc = ILLEGAL_ENTITY_RE.sub(u"&\g<2>", htmlSrc) #$NON-NLS-1$ except: pass if not hasBody(htmlSrc): # wrap content inside a <html><head/><body> [CONTENT] </body></html> htmlSrc = XHTML_TEMPLATE % htmlSrc lineOffset = XHTML_TEMPLATE_LINE_OFFSET tidySrc = convertToUtf8(htmlSrc) tidyRet = tidy.parseString(tidySrc, **options) errList = [] severities = dict(W=ZTidyError.WARN, E=ZTidyError.ERROR, C=ZTidyError.OTHER) for err in tidyRet.get_errors(): te = ZTidyError() if err.line is not None: te.line = err.line - lineOffset if err.col is not None: te.col = err.col if err.message is not None: te.message = err.message te.severity = ZTidyError.NONE if severities.has_key(err.severity): te.severity = severities[err.severity] errList.append(te) outHtml = str(tidyRet) return (convertToUnicode(outHtml), errList)
def insertImageFile(self, parentWindow, imageContext): u"""insertImageFile(wxWindow, IZXHTMLEditControlImageContext) -> void Shows the File open dialog to display an image. """ #$NON-NLS-1$ file = None wildcard = u"Image files|*.gif;*.jpg;*.png;*.jpeg" #$NON-NLS-1$ dialog = wx.FileDialog(parentWindow, u"Choose an image file.", u"", u"", wildcard, wx.OPEN) #$NON-NLS-4$ #$NON-NLS-3$ #$NON-NLS-2$ #$NON-NLS-1$ if dialog.ShowModal() == wx.ID_OK: file = getNoneString(dialog.GetPath()) dialog.Destroy() if file: (shortName, absPath, size, schemaDate) = getFileMetaData(file) #@UnusedVariable if shortName: shortName = convertToUnicode(shortName) else: shortName = u"" #$NON-NLS-1$ url = getUriFromFilePath(absPath) attrs = { u"src" : url, u"alt" : shortName} #$NON-NLS-1$ #$NON-NLS-2$ imageContext.insertImage(attrs)
def saveUnicodeContent(fileName, text, enc=None): u"""Saves the given text to a file as a utf-8 stream. This method re-throws exceptions - so calling code should catch and handle them.""" #$NON-NLS-1$ file = None if not enc: enc = u"utf-8" #$NON-NLS-1$ if not text: # TODO (PJ) throw here? text = u"" #$NON-NLS-1$ try: file = codecs.open(fileName, u"w", enc, u"replace") #$NON-NLS-1$ #$NON-NLS-2$ # make sure the text is unicode before writing to file via codecs. text = convertToUnicode(text) # # write the utf-8 byte order marker for wintel platforms. # file.write(codecs.BOM_UTF8) file.write(text) file.close() except Exception, e: raise e
def _internalPing(self, extendedPing, pingServerUrl, weblogName, weblogUrl, checkUrl = None, rssUrl = None): pingServerUrl = getNoneString(pingServerUrl) weblogName = getNoneString(weblogName) weblogUrl = getNoneString(weblogUrl) checkUrl = getSafeString(checkUrl).strip() rssUrl = getSafeString(rssUrl).strip() if pingServerUrl is None: return ZWeblogPingResponse(False, u"Weblog ping URL is required.") #$NON-NLS-1$ if weblogName is None: return ZWeblogPingResponse(False, u"Weblog name or post title is required.") #$NON-NLS-1$ if weblogUrl is None: return ZWeblogPingResponse(False, u"Weblog URL or post permanent link is required.") #$NON-NLS-1$ if extendedPing and (checkUrl == u"" or rssUrl == u""): #$NON-NLS-1$ #$NON-NLS-2$ return ZWeblogPingResponse(False, u"URL to RSS feed or Check URL parameter is required for ExtendedPings.") #$NON-NLS-1$ success = False message = u"" #$NON-NLS-1$ try: remoteServer = Server(pingServerUrl) result = None if extendedPing: result = remoteServer.weblogUpdate.extendedPing(weblogName, weblogUrl, checkUrl, rssUrl) else: result = remoteServer.weblogUpdates.ping(weblogName, weblogUrl) if result is not None and result.has_key(u"flerror"): #$NON-NLS-1$ success = not result[u"flerror"] #$NON-NLS-1$ if result is not None and result.has_key(u"message"): #$NON-NLS-1$ message = result[u"message"] #$NON-NLS-1$ elif not result or not result.has_key(u"message"): #$NON-NLS-1$ message = u"Weblog ping response message not available after pinging %s" % pingServerUrl #$NON-NLS-1$ except Fault, fault: fcode = u"" #$NON-NLS-1$ if fault.faultCode: fcode = unicode(fault.faultCode) fstr = u"" #$NON-NLS-1$ if fault.faultString: fstr = convertToUnicode(fault.faultString) success = False message = u"Weblog ping faulted when pinging %s (code: %s, reason: %s)" % (pingServerUrl, fcode, fstr) #$NON-NLS-1$
def _parseParamList(self, paramList): i = 0 while i < len(paramList): arg = convertToUnicode(paramList[i]) match = re.match(CMD_LINE_PARAM_PATTERN_ONE, arg) if match: i = i + self._parsePatternOne(match) continue match = re.match(CMD_LINE_PARAM_PATTERN_TWO, arg) if match: i = i + self._parsePatternTwo(match) continue match = re.match(CMD_LINE_PARAM_PATTERN_THREE, arg) if match: i = i + self._parsePatternThree(match, paramList[i + 1:]) continue match = re.match(CMD_LINE_PARAM_PATTERN_FOUR, arg) if match: i = i + self._parsePatternFour(match, paramList[i + 1:]) continue raise ZAppFrameworkException(_extstr(u"cmdline.InvalidCmdLineError") % arg) #$NON-NLS-1$
def _parseParamList(self, paramList): i = 0 while i < len(paramList): arg = convertToUnicode(paramList[i]) match = re.match(CMD_LINE_PARAM_PATTERN_ONE, arg) if match: i = i + self._parsePatternOne(match) continue match = re.match(CMD_LINE_PARAM_PATTERN_TWO, arg) if match: i = i + self._parsePatternTwo(match) continue match = re.match(CMD_LINE_PARAM_PATTERN_THREE, arg) if match: i = i + self._parsePatternThree(match, paramList[i + 1:]) continue match = re.match(CMD_LINE_PARAM_PATTERN_FOUR, arg) if match: i = i + self._parsePatternFour(match, paramList[i + 1:]) continue raise ZAppFrameworkException( _extstr(u"cmdline.InvalidCmdLineError") % arg) #$NON-NLS-1$
def loadUnicodeContent(fileName, enc=None): checkFile(fileName) if not enc: enc = u"utf-8" #$NON-NLS-1$ file = None text = None try: file = codecs.open(fileName, u"r", enc, u"replace") #$NON-NLS-1$ #$NON-NLS-2$ text = file.read() file.close() # if unicode string begins with the BOM (Py 2.3 bug?) remove the BOM character. if text and text[0] == unicode( codecs.BOM_UTF8, u"utf-8" ): #$NON-NLS-1$ text = text.lstrip( unicode( codecs.BOM_UTF8, u"utf-8" ) ) #$NON-NLS-1$ except: # try loading without codecs try: file = open(fileName, u'r') #$NON-NLS-1$ text = file.read() file.close() text = convertToUnicode(text) except: raise return text
def _loadContent(self, jbeHtmlFileName): f = open(jbeHtmlFileName, u"r") #$NON-NLS-1$ try: return convertToUnicode(f.read()) finally: f.close()
def decryptCipherText(ciphertext, key): obj = Blowfish.new(key, Blowfish.MODE_ECB) return convertToUnicode(obj.decrypt( base64.decodestring(ciphertext))).rstrip()
def _decodeZipFilename(filename): return convertToUnicode(filename, u"cp437") #$NON-NLS-1$
def getText(self, default = u""): #$NON-NLS-1$ try: return convertToUnicode(self.attr.nodeValue) except: return default
def onTag(self, tagId): #Call back when tag link is clicked on tagId = convertToUnicode(tagId) tagIDO = self.model.getTagIDO(tagId) fireViewSelectionEvent(ZTagSelection(tagIDO, self.blog), self) return False
def decryptCipherText(ciphertext, key): obj = Blowfish.new(key, Blowfish.MODE_ECB) return convertToUnicode(obj.decrypt(base64.decodestring(ciphertext))).rstrip()
def getText(self, default=u""): #$NON-NLS-1$ try: return convertToUnicode(self.attr.nodeValue) except: return default