Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def fromproxyurl(url):  # QUrl -> QUrl or None
    if not isinstance(url, QUrl):
        url = QUrl(url)
    if url.scheme() in ('http', 'https'):
        host = url.host()
        if host == config.PROXY_HOST:
            path = url.path()
            for rx in _re_proxy_key, _re_proxy_key2:
                m = rx.match(path)
                if m:
                    key = m.group(1)
                    if key:
                        host = config.PROXY_SITES.get(key)
                        if host:
                            url = QUrl(url)
                            url.setHost(host)
                            path = m.group(2) or '/'
                            if path[0] != '/':
                                path = '/' + path
                            url.setPath(path)
                            return url
                    #elif _MAINLAND and key == 'dlsite':
                    #  host = None
                    #  path = m.group(2) or '/'
                    #  if path.startswith('/www'):
                    #    host = _DLSITE_PROXY_SITES['/dlsite/www']
                    #    path = path[4:] or '/'
                    #  elif path.startswith('/img'):
                    #    host = _DLSITE_PROXY_SITES['/dlsite/img']
                    #    path = path[4:] or '/'
                    #  if host:
                    #    url = QUrl(url)
                    #    url.setHost(host)
                    #    url.setPath(path)
                    #    return url
        elif _MAINLAND:
            host = _PROXY_IPS.get(host)
            if host:
                url = QUrl(url)
                url.setHost(host)
                return url
Ejemplo n.º 3
0
   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)
Ejemplo n.º 4
0
    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)