1"""
2Copyright (c) 2017 Eliakin Costa <eliakim170@gmail.com>
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17"""
18from .debugger_scripter import debugger
19import sys
20if sys.version_info[0] > 2:
21    import asyncio
22else:
23    # trollius is a port of asyncio for python2.
24    import trollius as asyncio
25
26
27class DebugController (object):
28
29    def __init__(self, scripter):
30        self._debugger = None
31        self._cmd = None
32        self.scripter = scripter
33
34    def start(self, document):
35        self.setCmd(compile(document.data, document.filePath, "exec"))
36        self._debugger = debugger.Debugger(self.scripter, self._cmd)
37        self._debugger.debugprocess.start()
38        loop = asyncio.get_event_loop()
39        loop.run_until_complete(self._debugger.start())
40        self.updateUIDebugger()
41
42    def step(self):
43        loop = asyncio.get_event_loop()
44        loop.run_until_complete(self._debugger.step())
45        self.scripter.uicontroller.setStepped(True)
46        self.updateUIDebugger()
47
48    def stop(self):
49        loop = asyncio.get_event_loop()
50        loop.run_until_complete(self._debugger.stop())
51        self.updateUIDebugger()
52        self._debugger = None
53
54    def setCmd(self, cmd):
55        self._cmd = cmd
56
57    @property
58    def isActive(self):
59        try:
60            if self._debugger:
61                return self._debugger.debugprocess.is_alive()
62            return False
63        except Exception:
64            return False
65
66    @property
67    def currentLine(self):
68        try:
69            if self._debugger:
70                return int(self.debuggerData['code']['lineNumber'])
71        except Exception:
72            return 0
73
74    def updateUIDebugger(self):
75        widget = self.scripter.uicontroller.findTabWidget(i18n('Debugger'))
76        exception = self._debuggerException()
77
78        if exception:
79            self.scripter.uicontroller.showException(exception)
80        if not self.isActive or self._quitDebugger():
81            widget.disableToolbar(True)
82
83        self.scripter.uicontroller.repaintDebugArea()
84        widget.updateWidget()
85
86    @property
87    def debuggerData(self):
88        try:
89            if self._debugger:
90                return self._debugger.application_data
91        except Exception:
92            return
93
94    def _quitDebugger(self):
95        try:
96            return self.debuggerData['quit']
97        except Exception:
98            return False
99
100    def _debuggerException(self):
101        try:
102            return self.debuggerData['exception']
103        except Exception:
104            return False
105