weboobdebug.py 3.57 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3
# Copyright(C) 2010-2011 Christophe Benz
4
#
5
# This file is part of weboob.
6
#
7
# weboob is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9 10 11 12
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
19

20
from __future__ import print_function
21

22
from optparse import OptionGroup
23

24
from weboob.tools.application.base import Application
25
from weboob.browser.elements import generate_table_element
26 27


28
class WeboobDebug(Application):
29
    APPNAME = 'weboobdebug'
Romain Bignon's avatar
Romain Bignon committed
30
    VERSION = '1.5'
31
    COPYRIGHT = 'Copyright(C) 2010-YEAR Christophe Benz'
32
    DESCRIPTION = "Weboob-Debug is a console application to debug backends."
33
    SHORT_DESCRIPTION = "debug backends"
34

35 36 37 38 39 40
    def __init__(self, option_parser=None):
        super(WeboobDebug, self).__init__(option_parser)
        options = OptionGroup(self._parser, 'Weboob-Debug options')
        options.add_option('-B', '--bpython', action='store_true', help='Prefer bpython over ipython')
        self._parser.add_option_group(options)

41 42
    def load_default_backends(self):
        pass
43

44
    def main(self, argv):
45
        """
46
        BACKEND
47

48
        Debug BACKEND.
49
        """
50 51 52
        try:
            backend_name = argv[1]
        except IndexError:
53
            print('Usage: %s BACKEND' % argv[0], file=self.stderr)
54
            return 1
55
        try:
Christophe Benz's avatar
Christophe Benz committed
56
            backend = self.weboob.load_backends(names=[backend_name])[backend_name]
57
        except KeyError:
58
            print(u'Unable to load backend "%s"' % backend_name, file=self.stderr)
59
            return 1
60

61 62 63
        locs = dict(backend=backend, browser=backend.browser,
                    application=self, weboob=self.weboob,
                    generate_table_element=generate_table_element)
64
        banner = 'Weboob debug shell\nBackend "%s" loaded.\nAvailable variables:\n' % backend_name \
65
                 + '\n'.join(['  %s: %s' % (k, v) for k, v in locs.items()])
66

67
        if self.options.bpython:
68 69 70
            funcs = [self.bpython, self.ipython, self.python]
        else:
            funcs = [self.ipython, self.bpython, self.python]
71
        self.launch(funcs, locs, banner)
72

73
    def launch(self, funcs, locs, banner):
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        for func in funcs:
            try:
                func(locs, banner)
            except ImportError:
                continue
            else:
                break

    def ipython(self, locs, banner):
        try:
            from IPython import embed
            embed(user_ns=locs, banner2=banner)
        except ImportError:
            from IPython.Shell import IPShellEmbed
            shell = IPShellEmbed(argv=[])
            shell.set_banner(shell.IP.BANNER + '\n\n' + banner)
            shell(local_ns=locs, global_ns={})

    def bpython(self, locs, banner):
        from bpython import embed
        embed(locs, banner=banner)

    def python(self, locs, banner):
        import code
        try:
            import readline
            import rlcompleter
            readline.set_completer(rlcompleter.Completer(locs).complete)
            readline.parse_and_bind("tab:complete")
        except ImportError:
            pass
        code.interact(banner=banner, local=locs)