File: __init__.py

package info (click to toggle)
python-scrapy 0.8-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,904 kB
  • ctags: 2,981
  • sloc: python: 15,349; xml: 199; makefile: 68; sql: 64; sh: 34
file content (27 lines) | stat: -rw-r--r-- 896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Download handlers for different schemes"""

from scrapy.core.exceptions import NotSupported
from scrapy.utils.httpobj import urlparse_cached
from scrapy.conf import settings
from scrapy.utils.misc import load_object


class RequestHandlers(object):

    def __init__(self):
        self._handlers = {}
        handlers = settings.get('REQUEST_HANDLERS_BASE')
        handlers.update(settings.get('REQUEST_HANDLERS', {}))
        for scheme, cls in handlers.iteritems():
            self._handlers[scheme] = load_object(cls)

    def download_request(self, request, spider):
        scheme = urlparse_cached(request).scheme
        try:
            handler = self._handlers[scheme]
        except KeyError:
            raise NotSupported("Unsupported URL scheme '%s' in: <%s>" % (scheme, request.url))
        return handler(request, spider)


download_any = RequestHandlers().download_request