def handle_url(path, style, url, for_import=False): if ((url.startswith('"') and url.endswith('"')) or (url.startswith("'") and url.endswith("'"))): url = url[1:-1] # Make a QUrl. url = QUrl(url.decode('unicode_escape')) if url.scheme() == 'data': return data_url(url) # If it's relative, build an absolute URL. If not, return. if not url.isRelative(): return url = url.toLocalFile() if url.startswith('/'): url = url[1:] else: url = style.path.join(path, url) if for_import: return url return find_url(style, url)
def urlExists(self, value): if not value: return False url = QUrl(value) if not url.isLocalFile(): return True path = url.toLocalFile() return os.path.exists(path)
def _qss_url(self, path, url, allow_inheritance=True, _suppress=False, for_import=False): """ Process a url() and return an absolute URL, or None if the URL isn't valid. """ if (url.startswith('"') and url.endswith('"')) or \ (url.startswith("'") and url.endswith("'")): url = url[1:-1] # Make a QUrl. url = QUrl(url.decode('unicode_escape')) # Is it a data uri? if url.scheme() == 'data': # Extract the useful information from the path. format, sep, data = url.path().partition(',') if not sep and not data: data = format format = '' mimetype, _, format = format.partition(';') if not mimetype: ext = 'txt' else: _, _, ext = mimetype.rpartition('/') if not format: format = 'charset=US-ASCII' # Build the filename. fn = os.path.join(profile.cache_path, u'data-uris', '%s.%s' % (hashlib.md5(data).hexdigest(), ext)) # Ensure the path exists and write the file. try: if not os.path.exists(os.path.dirname(fn)): os.makedirs(os.path.dirname(fn)) with open(fn, 'wb') as f: if format == 'base64': f.write(base64.b64decode(data)) elif format.startswith('charset='): data = urllib.unquote(data).encode('latin1') cs = format[8:] if cs and cs.lower() not in ('utf-8','utf8'): data = data.decode(cs).encode('utf-8') f.write(data) else: return except (ValueError, OSError, IOError, TypeError): log.debug('Error parsing data URI.', exc_info=1) return # Substitute the right / on Windows, and return the path. if os.name == 'nt': fn = fn.replace('\\', '/') return fn # If it's relative, build an absolute URL. If not, return. if not url.isRelative(): return url = url.toLocalFile() if url.startswith('/'): url = url[1:] else: url = profile.join(path, url) # If we're dealing with import, return a relative path. if for_import: return url return self.get_path(url, allow_inheritance, False, _suppress)
def _qss_url(self, path, url, allow_inheritance=True, _suppress=False, for_import=False): """ Process a url() and return an absolute URL, or None if the URL isn't valid. """ if (url.startswith('"') and url.endswith('"')) or \ (url.startswith("'") and url.endswith("'")): url = url[1:-1] # Make a QUrl. url = QUrl(url.decode('unicode_escape')) # Is it a data uri? if url.scheme() == 'data': # Extract the useful information from the path. format, sep, data = url.path().partition(',') if not sep and not data: data = format format = '' mimetype, _, format = format.partition(';') if not mimetype: ext = 'txt' else: _, _, ext = mimetype.rpartition('/') if not format: format = 'charset=US-ASCII' # Build the filename. fn = os.path.join(profile.cache_path, u'data-uris', '%s.%s' % (hashlib.md5(data).hexdigest(), ext)) # Ensure the path exists and write the file. try: if not os.path.exists(os.path.dirname(fn)): os.makedirs(os.path.dirname(fn)) with open(fn, 'wb') as f: if format == 'base64': f.write(base64.b64decode(data)) elif format.startswith('charset='): data = urllib.unquote(data).encode('latin1') cs = format[8:] if cs and cs.lower() not in ('utf-8', 'utf8'): data = data.decode(cs).encode('utf-8') f.write(data) else: return except (ValueError, OSError, IOError, TypeError): log.debug('Error parsing data URI.', exc_info=1) return # Substitute the right / on Windows, and return the path. if os.name == 'nt': fn = fn.replace('\\', '/') return fn # If it's relative, build an absolute URL. If not, return. if not url.isRelative(): return url = url.toLocalFile() if url.startswith('/'): url = url[1:] else: url = profile.join(path, url) # If we're dealing with import, return a relative path. if for_import: return url return self.get_path(url, allow_inheritance, False, _suppress)