def _getLocalFilePath(self, element, attrName): path = getNoneString(element.getAttribute(attrName)) # check if the path is a local resource (file://) and if it exists localFilePath = getFilePathFromUri(path) if localFilePath and os.path.isfile(localFilePath): return localFilePath else: return None
def _getLocalFilePath(self, element, attrName): path = getNoneString( element.getAttribute(attrName) ) # check if the path is a local resource (file://) and if it exists localFilePath = getFilePathFromUri(path) if localFilePath and os.path.isfile(localFilePath): return localFilePath else: return None
def getSize(self): fileSize = -1 filePath = getFilePathFromUri( self.getHref() ) if filePath: # local file exists try: fileSize = os.path.getsize(filePath) except: pass return fileSize
def getResourceName(self): u"""getResourceName() -> string Returns the resource (e.g. file) name if href is a local file and it is available or None otherwise. """ #$NON-NLS-1$ rval = None filePath = getFilePathFromUri( self.getHref() ) if filePath: # local file exists rval = filePath.replace(u'\\',u'/').split(u'/')[-1] #$NON-NLS-1$ #$NON-NLS-2$ #$NON-NLS-3$ return rval
def _checkImgUnicodePaths(self): eleList = self.mshtmlBodyElement.getElementsByTagName(u"IMG") #$NON-NLS-1$ for ele in eleList: src = getNoneString(ele.getAttribute(u"src")) #$NON-NLS-1$ if not src: continue localFilePath = getFilePathFromUri(src) if localFilePath and os.path.exists(localFilePath): # if local file path, then "set" image path - IE MSHTML quirk - to 'update' the image dispEle = getDispElement(ele) #@UnusedVariable dispEle.src = localFilePath
def isEnclosure(self): # get file path (href) filePath = getFilePathFromUri( self.getHref() ) # filepath is None if path does not exist ot if the protocol is not file:// rval = False if filePath: # filepath points to a local file (base, ext) = os.path.splitext(filePath) #@UnusedVariable # FIXME (PJ) use mime registry to determine types. EXTS = u".mp3 .wma .mov .mp4 .wav .mpg .mpeg .zip .exe .pdf .doc .xls .ppt" #$NON-NLS-1$ rval = ext and EXTS.find(ext.strip()) != -1 return rval
def _validateProtocol(self, value, message): # validate procotol in given value and return tuple (bool, message) valid = False if FILEURL_RE.match(value) is not None: valid = True filepath = None # if file://, then validate path. if valid and value.lower().startswith(u"file:"): #$NON-NLS-1$ filepath = getFilePathFromUri(value) valid = os.path.exists(filepath) if not valid: message = message + u"(invalid file path)" #$NON-NLS-1$ elif not valid and FILE_RE.match(value) is not None: # user typed in local path, eg c:/temp. valid = os.path.exists(value) if not valid: message = message + u"(invalid file path)" #$NON-NLS-1$ return (valid, message)
def _analyseZXhtmlImage(self, iZXhtmlImage): tnElem = None linkElem = None if isLocalFile( iZXhtmlImage.getSrc() ): # img source points to a local image file. tnElem = iZXhtmlImage.getNode() if iZXhtmlImage.isLinked(): # TN is linked. Check to see if the link exists and if it points to a local image iZXhtmlLink = iZXhtmlImage.getLink() if isLocalFile( iZXhtmlLink.getHref() ): # parent link points to a local file. # check if link is to an image filePath = getFilePathFromUri( iZXhtmlLink.getHref() ) (base, ext) = os.path.splitext(filePath) #@UnusedVariable if ext and IMAGE_EXTS.find(ext.strip()) != -1: linkElem = iZXhtmlLink.getNode() if tnElem and linkElem: # remove TN tnElem = tnElem.parentNode.removeChild(tnElem) # import TB back to dom tnElem = linkElem.ownerDocument.importNode(tnElem, True) # replace link if TN linkElem.parentNode.replaceChild(tnElem, linkElem)
def _analyseZXhtmlImage(self, iZXhtmlImage): tnElem = None linkElem = None if isLocalFile(iZXhtmlImage.getSrc()): # img source points to a local image file. tnElem = iZXhtmlImage.getNode() if iZXhtmlImage.isLinked(): # TN is linked. Check to see if the link exists and if it points to a local image iZXhtmlLink = iZXhtmlImage.getLink() if isLocalFile(iZXhtmlLink.getHref()): # parent link points to a local file. # check if link is to an image filePath = getFilePathFromUri(iZXhtmlLink.getHref()) (base, ext) = os.path.splitext(filePath) #@UnusedVariable if ext and IMAGE_EXTS.find(ext.strip()) != -1: linkElem = iZXhtmlLink.getNode() if tnElem and linkElem: # remove TN tnElem = tnElem.parentNode.removeChild(tnElem) # import TB back to dom tnElem = linkElem.ownerDocument.importNode(tnElem, True) # replace link if TN linkElem.parentNode.replaceChild(tnElem, linkElem)
def _checkImgAttributes(self): # 1) Set img runtime border if needed to 0 (remove border) # 2) Check img align attribtues # simple method to make sure if the image CSS has width and heigh but not image width and height # attribute, then create image width and height attributes. # (Work around for WordPress.com bug) eleList = self.mshtmlBodyElement.getElementsByTagName(u"IMG") #$NON-NLS-1$ for ele in eleList: # verify image centering attrs are correct. if ele.style and ele.style.textAlign == u"center" and ele.style.display == u"block": #$NON-NLS-1$ #$NON-NLS-2$ # img is supposed to be centered - add fire fox work around. if ele.style.marginLeft != u"auto": #$NON-NLS-1$ ele.style.marginLeft = u"auto" #$NON-NLS-1$ ele.style.marginRight = u"auto" #$NON-NLS-1$ # set runtime border = 0, if border is not set by the user. # this is so that a border for hyperlinks is not shown hasBorder = ele.style.borderWidth != u"" or ele.style.borderLeftWidth != u"" #$NON-NLS-1$ #$NON-NLS-2$ hasBorder = hasBorder or ele.style.borderRightWidth != u"" or ele.style.borderTopWidth != u"" or ele.style.borderBottomWidth != u"" #$NON-NLS-1$ #$NON-NLS-2$ #$NON-NLS-3$ hasRtBorder = ele.runtimeStyle.borderWidth != u"" or ele.runtimeStyle.borderLeftWidth != u"" #$NON-NLS-1$ #$NON-NLS-2$ hasRtBorder = hasRtBorder or ele.runtimeStyle.borderRightWidth != u"" or ele.runtimeStyle.borderTopWidth != u"" or ele.runtimeStyle.borderBottomWidth != u"" #$NON-NLS-1$ #$NON-NLS-2$ #$NON-NLS-3$ if not hasBorder and not hasRtBorder: #$NON-NLS-1$ # set implicit/runtime border =0 ele.runtimeStyle.borderWidth = u"0px"; #$NON-NLS-1$ elif hasBorder and hasRtBorder: #$NON-NLS-1$ # remove runtime border if user has defined a border. ele.runtimeStyle.borderWidth = u""; #$NON-NLS-1$ src = getNoneString(ele.getAttribute(u"src")) #$NON-NLS-1$ if not src: continue # check if the img src is a local resource (file://) and if it exists localFilePath = getFilePathFromUri(src) if localFilePath and not os.path.exists(localFilePath): # Do not modify any local files that do not exist anymore (width and height info will not be available) #(i.e. not assign random/default width and heights) continue cssWidth = None cssHeight = None # get css sizes if ele.style and ele.style.width: cssWidth = getSafeString(ele.style.width) if ele.style and ele.style.height: cssHeight = getSafeString(ele.style.height) # get explicit size (i.e. set by the user via img width/height attribute) w = getSafeString( ele.getAttribute(u"width", 2) ) #$NON-NLS-1$ # pass flag=2 so that getAttribute() returns value iff one was explicitly specified. h = getSafeString( ele.getAttribute(u"height", 2) )#$NON-NLS-1$ # note: getAttribute may also return the value found in the CSS style. # so, we should set it as an attribute in the <img> tag in case it did not exist as an attribute in the img tag. # for example, we want <img style="width:10px" src="" /> to be <img width="10" style="width:10px" src="" /> # (that is add explicit width and height attributes) # grab size if explicit size is not available actualWidth = actualHeight = -1 # if local file then get size directly from img. if localFilePath:# and (not w or not h): (actualWidth, actualHeight) = self._getImageSizeFromFile(localFilePath) # if not local file, then ask IE for implicit img size (i.e. for remote (non-local) images) if not localFilePath and not w and not cssWidth and not h and not cssHeight: # first, ask IE for implicit width and height (usually returns actual image size) w = getSafeString( ele.getAttribute(u"width") ) #$NON-NLS-1$ h = getSafeString( ele.getAttribute(u"height") ) #$NON-NLS-1$ if w: ele.setAttribute(u"width", w) #$NON-NLS-1$ elif cssWidth and not w: ele.setAttribute(u"width", cssWidth) #$NON-NLS-1$ elif actualWidth > 0: ele.setAttribute(u"width", actualWidth) #$NON-NLS-1$ if h: ele.setAttribute(u"height", h) #$NON-NLS-1$ elif cssHeight and not h: ele.setAttribute(u"height", cssHeight) #$NON-NLS-1$ elif actualHeight > 0: ele.setAttribute(u"height", actualHeight) #$NON-NLS-1$
def getFile(self): u"""getFile() -> string""" #$NON-NLS-1$ rval = self.getLocalUri() if rval: rval = getFilePathFromUri(rval) return rval