File: groupsettings.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 (26 lines) | stat: -rw-r--r-- 1,057 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
"""
Extensions to override scrapy settings with per-group settings according to the
group the spider belongs to. It only overrides the settings when running the
crawl command with *only one domain as argument*.
"""

from scrapy.conf import settings
from scrapy.core.exceptions import NotConfigured
from scrapy.command.cmdline import command_executed

class GroupSettings(object):

    def __init__(self):
        if not settings.getbool("GROUPSETTINGS_ENABLED"):
            raise NotConfigured

        if command_executed and command_executed['name'] == 'crawl':
            mod = __import__(settings['GROUPSETTINGS_MODULE'], {}, {}, [''])
            args = command_executed['args']
            if len(args) == 1 and not args[0].startswith('http://'):
                domain = args[0]
                settings.overrides.update(mod.default_settings)
                for group, domains in mod.group_spiders.iteritems():
                    if domain in domains:
                        settings.overrides.update(mod.group_settings.get(group, {}))