Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / cssutils / _fetchgae.py @ 475

History | View | Annotate | Download (2.58 KB)

1
"""GAE specific URL reading functions"""
2
__all__ = ['_defaultFetcher']
3
__docformat__ = 'restructuredtext'
4
__version__ = '$Id: tokenize2.py 1547 2008-12-10 20:42:26Z cthedot $'
5

    
6
# raises ImportError of not on GAE
7
from google.appengine.api import urlfetch
8
import cgi
9
import errorhandler
10
import util
11

    
12
log = errorhandler.ErrorHandler()
13

    
14
def _defaultFetcher(url):
15
    """
16
    uses GoogleAppEngine (GAE)
17
        fetch(url, payload=None, method=GET, headers={}, allow_truncated=False)
18

19
    Response
20
        content
21
            The body content of the response.
22
        content_was_truncated
23
            True if the allow_truncated parameter to fetch() was True and
24
            the response exceeded the maximum response size. In this case,
25
            the content attribute contains the truncated response.
26
        status_code
27
            The HTTP status code.
28
        headers
29
            The HTTP response headers, as a mapping of names to values.
30

31
    Exceptions
32
        exception InvalidURLError()
33
            The URL of the request was not a valid URL, or it used an
34
            unsupported method. Only http and https URLs are supported.
35
        exception DownloadError()
36
            There was an error retrieving the data.
37

38
            This exception is not raised if the server returns an HTTP
39
            error code: In that case, the response data comes back intact,
40
            including the error code.
41

42
        exception ResponseTooLargeError()
43
            The response data exceeded the maximum allowed size, and the
44
            allow_truncated parameter passed to fetch() was False.
45
    """
46
    #from google.appengine.api import urlfetch
47
    try:
48
        r = urlfetch.fetch(url, method=urlfetch.GET)
49
    except urlfetch.Error, e:
50
        log.warn(u'Error opening url=%r: %s' % (url, e),
51
                          error=IOError)
52
    else:
53
        if r.status_code == 200:
54
            # find mimetype and encoding
55
            mimetype = 'application/octet-stream'
56
            try:
57
                mimetype, params = cgi.parse_header(r.headers['content-type'])
58
                encoding = params['charset']
59
            except KeyError:
60
                encoding = None
61
            if mimetype != u'text/css':
62
                log.error(u'Expected "text/css" mime type for url %r but found: %r' % 
63
                              (url, mimetype), error=ValueError)
64
            return encoding, r.content
65
        else:
66
            # TODO: 301 etc
67
            log.warn(u'Error opening url=%r: HTTP status %s' % 
68
                              (url, r.status_code), error=IOError)