Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

import sys 

import os 

from ConfigParser import SafeConfigParser 

from operator import itemgetter 

 

def build_component_list(base, custom): 

    """Compose a component list based on a custom and base dict of components 

    (typically middlewares or extensions), unless custom is already a list, in 

    which case it's returned. 

    """ 

    if isinstance(custom, (list, tuple)): 

        return custom 

    compdict = base.copy() 

    compdict.update(custom) 

    return [k for k, v in sorted(compdict.items(), key=itemgetter(1)) \ 

        if v is not None] 

 

def arglist_to_dict(arglist): 

    """Convert a list of arguments like ['arg1=val1', 'arg2=val2', ...] to a 

    dict 

    """ 

    return dict(x.split('=', 1) for x in arglist) 

 

def closest_scrapy_cfg(path='.', prevpath=None): 

    """Return the path to the closest scrapy.cfg file by traversing the current 

    directory and its parents 

    """ 

    if path == prevpath: 

        return '' 

    path = os.path.abspath(path) 

    cfgfile = os.path.join(path, 'scrapy.cfg') 

33    if os.path.exists(cfgfile): 

        return cfgfile 

    return closest_scrapy_cfg(os.path.dirname(path), path) 

 

def init_env(project='default', set_syspath=True): 

    """Initialize environment to use command-line tool from inside a project 

    dir. This sets the Scrapy settings module and modifies the Python path to 

    be able to locate the project module. 

    """ 

    cfg = get_config() 

43    if cfg.has_option('settings', project): 

        os.environ['SCRAPY_SETTINGS_MODULE'] = cfg.get('settings', project) 

    closest = closest_scrapy_cfg() 

46    if closest: 

        projdir = os.path.dirname(closest) 

        if set_syspath and projdir not in sys.path: 

            sys.path.append(projdir) 

 

def get_config(use_closest=True): 

    """Get Scrapy config file as a SafeConfigParser""" 

    sources = get_sources(use_closest) 

    cfg = SafeConfigParser() 

    cfg.read(sources) 

    return cfg 

 

def get_sources(use_closest=True): 

    sources = ['/etc/scrapy.cfg', r'c:\scrapy\scrapy.cfg', \ 

        os.path.expanduser('~/.scrapy.cfg')] 

62    if use_closest: 

        sources.append(closest_scrapy_cfg()) 

    return sources