Ejemplo n.º 1
0
def _defaultFetcher(url):
    """Retrieve data from ``url``. cssutils default implementation of fetch
    URL function.

    Returns ``(encoding, string)`` or ``None``
    """
    try:
        request = urllib.request.Request(url)
        request.add_header('User-agent',
                           'cssutils %s (http://www.cthedot.de/cssutils/)' % VERSION)
        res = urllib.request.urlopen(request)
    except urllib.error.HTTPError as e:
        # http error, e.g. 404, e can be raised
        log.warn('HTTPError opening url=%s: %s %s' %
                          (url, e.code, e.msg), error=e)
    except urllib.error.URLError as e:
        # URLError like mailto: or other IO errors, e can be raised
        log.warn('URLError, %s' % e.reason, error=e)
    except OSError as e:
        # e.g if file URL and not found
        log.warn(e, error=OSError)
    except ValueError as e:
        # invalid url, e.g. "1"
        log.warn('ValueError, %s' % e.args[0], error=ValueError)
    else:
        if res:
            mimeType, encoding = encutils.getHTTPInfo(res)
            if mimeType != 'text/css':
                log.error('Expected "text/css" mime type for url=%r but found: %r' %
                                  (url, mimeType), error=ValueError)
            content = res.read()
            if hasattr(res, 'close'):
                res.close()
            return encoding, content
Ejemplo n.º 2
0
 def _createStyleSheet(self, href=None, 
                       media=None, 
                       parentStyleSheet=None, 
                       title=u'',
                       cssText=None,
                       encoding=None):
     """
     returns CSSStyleSheet read from href or if cssText is given use that
     
     encoding
         used if inline style found, same as self.docencoding
     """
     if not cssText:
         res, href = self._doRequest(href)
         if res:
             if not encoding:
                 media_type, encoding = encutils.getHTTPInfo(res)
                 if media_type != u'text/css':
                     self._log.warn(u'    WARNING: HTTP media type is different than expected "text/css": %r' % 
                                    media_type)
             try:
                 cssText = codecs.getreader('css')(res, 
                                                   encoding=encoding).read()
             except UnicodeDecodeError, e:
                 self._log.error(u'    Error retrieving CSS, probably encoding mismatch:\n\t%s\n\t%s'
                                  % (href, e))
                 return None
         else: 
             self._log.error(u'    ERROR accessing CSS\n\t' % href)
             return None
Ejemplo n.º 3
0
def _defaultFetcher(url):
    """Retrieve data from ``url``. cssutils default implementation of fetch
    URL function.

    Returns ``(encoding, string)`` or ``None``
    """
    try:
        request = urllib.request.Request(url)
        request.add_header(
            'User-agent',
            'cssutils %s (http://www.cthedot.de/cssutils/)' % VERSION)
        res = urllib.request.urlopen(request)
    except urllib.error.HTTPError as e:
        # http error, e.g. 404, e can be raised
        log.warn('HTTPError opening url=%s: %s %s' % (url, e.code, e.msg),
                 error=e)
    except urllib.error.URLError as e:
        # URLError like mailto: or other IO errors, e can be raised
        log.warn('URLError, %s' % e.reason, error=e)
    except OSError as e:
        # e.g if file URL and not found
        log.warn(e, error=OSError)
    except ValueError as e:
        # invalid url, e.g. "1"
        log.warn('ValueError, %s' % e.args[0], error=ValueError)
    else:
        if res:
            mimeType, encoding = encutils.getHTTPInfo(res)
            if mimeType != 'text/css':
                log.error(
                    'Expected "text/css" mime type for url=%r but found: %r' %
                    (url, mimeType),
                    error=ValueError)
            content = res.read()
            if hasattr(res, 'close'):
                res.close()
            return encoding, content
Ejemplo n.º 4
0
    Returns ``(encoding, string)`` or ``None``
    """
    try:
        request = urllib2.Request(url)
        request.add_header(
            'User-agent',
            'cssutils %s (http://www.cthedot.de/cssutils/)' % VERSION)
        res = urllib2.urlopen(request)
    except OSError, e:
        # e.g if file URL and not found
        log.warn(e, error=OSError)
    except (OSError, ValueError), e:
        # invalid url, e.g. "1"
        log.warn(u'ValueError, %s' % e.args[0], error=ValueError)
    except urllib2.HTTPError, e:
        # http error, e.g. 404, e can be raised
        log.warn(u'HTTPError opening url=%s: %s %s' % (url, e.code, e.msg),
                 error=e)
    except urllib2.URLError, e:
        # URLError like mailto: or other IO errors, e can be raised
        log.warn(u'URLError, %s' % e.reason, error=e)
    else:
        if res:
            mimeType, encoding = encutils.getHTTPInfo(res)
            if mimeType != u'text/css':
                log.error(
                    u'Expected "text/css" mime type for url=%r but found: %r' %
                    (url, mimeType),
                    error=ValueError)
            return encoding, res.read()
Ejemplo n.º 5
0
    """Retrieve data from ``url``. cssutils default implementation of fetch
    URL function.

    Returns ``(encoding, string)`` or ``None``
    """
    request = urllib2.Request(url)
    request.add_header('User-agent', 
                       'cssutils %s (http://www.cthedot.de/cssutils/)' % VERSION)
    try:        
        res = urllib2.urlopen(request)
    except OSError, e:
        # e.g if file URL and not found
        log.warn(e, error=OSError)
    except (OSError, ValueError), e:
        # invalid url, e.g. "1"
        log.warn(u'ValueError, %s' % e.args[0], error=ValueError)
    except urllib2.HTTPError, e:
        # http error, e.g. 404, e can be raised
        log.warn(u'HTTPError opening url=%r: %s %s' % 
                          (url, e.code, e.msg), error=e)
    except urllib2.URLError, e:
        # URLError like mailto: or other IO errors, e can be raised
        log.warn(u'URLError, %s' % e.reason, error=e)
    else:
        if res:
            mimeType, encoding = encutils.getHTTPInfo(res)
            if mimeType != u'text/css':
                log.error(u'Expected "text/css" mime type for url=%r but found: %r' % 
                                  (url, mimeType), error=ValueError)
            return encoding, res.read()