scrapy / scrapy
1 7
from functools import wraps
2 7
from collections import OrderedDict
3

4

5 7
def _embed_ipython_shell(namespace={}, banner=''):
6
    """Start an IPython Shell"""
7 7
    try:
8 7
        from IPython.terminal.embed import InteractiveShellEmbed
9 7
        from IPython.terminal.ipapp import load_default_config
10 0
    except ImportError:
11 0
        from IPython.frontend.terminal.embed import InteractiveShellEmbed
12 0
        from IPython.frontend.terminal.ipapp import load_default_config
13

14 7
    @wraps(_embed_ipython_shell)
15 7
    def wrapper(namespace=namespace, banner=''):
16 0
        config = load_default_config()
17
        # Always use .instace() to ensure _instance propagation to all parents
18
        # this is needed for <TAB> completion works well for new imports
19
        # and clear the instance to always have the fresh env
20
        # on repeated breaks like with inspect_response()
21 0
        InteractiveShellEmbed.clear_instance()
22 0
        shell = InteractiveShellEmbed.instance(
23
            banner1=banner, user_ns=namespace, config=config)
24 0
        shell()
25 7
    return wrapper
26

27

28 7
def _embed_bpython_shell(namespace={}, banner=''):
29
    """Start a bpython shell"""
30 7
    import bpython
31

32 7
    @wraps(_embed_bpython_shell)
33 7
    def wrapper(namespace=namespace, banner=''):
34 0
        bpython.embed(locals_=namespace, banner=banner)
35 7
    return wrapper
36

37

38 7
def _embed_ptpython_shell(namespace={}, banner=''):
39
    """Start a ptpython shell"""
40 7
    import ptpython.repl
41

42 0
    @wraps(_embed_ptpython_shell)
43 0
    def wrapper(namespace=namespace, banner=''):
44 0
        print(banner)
45 0
        ptpython.repl.embed(locals=namespace)
46 0
    return wrapper
47

48

49 7
def _embed_standard_shell(namespace={}, banner=''):
50
    """Start a standard python shell"""
51 7
    import code
52 7
    try:  # readline module is only available on unix systems
53 7
        import readline
54 0
    except ImportError:
55 0
        pass
56
    else:
57 7
        import rlcompleter  # noqa: F401
58 7
        readline.parse_and_bind("tab:complete")
59

60 7
    @wraps(_embed_standard_shell)
61 7
    def wrapper(namespace=namespace, banner=''):
62 0
        code.interact(banner=banner, local=namespace)
63 7
    return wrapper
64

65

66 7
DEFAULT_PYTHON_SHELLS = OrderedDict([
67
    ('ptpython', _embed_ptpython_shell),
68
    ('ipython', _embed_ipython_shell),
69
    ('bpython', _embed_bpython_shell),
70
    ('python', _embed_standard_shell),
71
])
72

73

74 7
def get_shell_embed_func(shells=None, known_shells=None):
75
    """Return the first acceptable shell-embed function
76
    from a given list of shell names.
77
    """
78 7
    if shells is None:  # list, preference order of shells
79 7
        shells = DEFAULT_PYTHON_SHELLS.keys()
80 7
    if known_shells is None:  # available embeddable shells
81 7
        known_shells = DEFAULT_PYTHON_SHELLS.copy()
82 7
    for shell in shells:
83 7
        if shell in known_shells:
84 7
            try:
85
                # function test: run all setup code (imports),
86
                # but dont fall into the shell
87 7
                return known_shells[shell]()
88 7
            except ImportError:
89 7
                continue
90

91

92 7
def start_python_console(namespace=None, banner='', shells=None):
93
    """Start Python console bound to the given namespace.
94
    Readline support and tab completion will be used on Unix, if available.
95
    """
96 7
    if namespace is None:
97 0
        namespace = {}
98

99 0
    try:
100 0
        shell = get_shell_embed_func(shells)
101 7
        if shell is not None:
102 0
            shell(namespace=namespace, banner=banner)
103 0
    except SystemExit:  # raised when using exit() in python code.interact
104 0
        pass

Read our documentation on viewing source code .

Loading