File: wb_execute_window.py

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (176 lines) | stat: -rw-r--r-- 5,706 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) 2012, 2013 Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301  USA


import mforms
import threading
from Queue import Queue, Empty
from wb_admin_utils import weakcb


class Task(threading.Thread):
    def __init__(self, ctrl_be, command, queue):
        threading.Thread.__init__(self)
        self.ctrl_be = ctrl_be
        self.command = command
        self.queue = queue
        self.returncode = None

    def handle_output(self, text):
        if text is not None:
            self.queue.put(text)


    def run(self):
        try:
            self.returncode = self.command(output_handler = self.handle_output)
            if self.returncode != 0:
                self.queue.put("Command exited with status %s\n" % self.returncode)
            self.queue.put(None)
        except Exception:
            import traceback
            self.queue.put("\nError executing command:\n"+traceback.format_exc())
            self.queue.put(None)



class CommandExecutePanel(mforms.Form):
    def __init__(self, ctrl_be, title, descr, stop_callback = None, close_callback=None, progress_parser_callback=None):
        mforms.Form.__init__(self, mforms.Form.main_form(), mforms.FormDialogFrame)
        self.ctrl_be = ctrl_be

        self._done = False
        self._update_tm = None

        self.finished_callback = None
        self.stop_callback = stop_callback
        self.close_callback = close_callback
        self.progress_parser_callback = progress_parser_callback

        self.show(False)

        self.box = mforms.newBox(False)
        self.set_content(self.box)
        self.box.set_padding(12)
        self.box.set_spacing(20)

        self.set_title(title)
        self.label = mforms.newLabel(descr)
        self.box.add(self.label, False, True)

        hb = mforms.newBox(True)
        self.progress = mforms.newProgressBar()
        self.progress_label = mforms.newLabel("")
        self.progress_label.set_size(100, -1)
        hb.add(self.progress_label, False, True)
        hb.add(self.progress, True, True)
        self.box.add(hb, False, True)

        self.logbox = mforms.newTextBox(mforms.VerticalScrollBar)
        self.logbox.set_read_only(True)
        panel = mforms.newPanel(mforms.TitledBoxPanel)
        panel.set_title("Command Output")

        self.logbox.set_padding(8)
        panel.add(self.logbox)
        self.box.add(panel, True, True)


        bbox = mforms.newBox(True)
        self.box.add_end(bbox, False, True)

        self.stop = mforms.newButton()
        if stop_callback:
            self.stop.set_text("Stop")
        else:
            self.stop.set_text("Close")
        self.stop.add_clicked_callback(self.do_stop)
        bbox.add_end(self.stop, False, True)

        self.set_size(700, 500)
        self.center()


    def _update(self):
        while True:
            try:
                msg = self.queue.get_nowait()
            except Empty:
                break
            if msg is None:
                self.progress.stop()
                self._done = True
                self.stop.set_text("Close")
                self.stop.set_enabled(True)

                if self.finished_callback(self.task.returncode):
                    self.progress_label.set_text("Finished")
                else:
                    self.progress_label.set_text("FAILED")
                self._update_tm = None
                return False
            else:
                if self.progress_parser_callback:
                    progress = self.progress_parser_callback(msg)
                    if progress is not None:
                        self.progress.set_value(float(progress))
                self.logbox.append_text_and_scroll(msg, True)
        return True

    def close(self):
        if self._update_tm:
            mforms.Utilities.cancel_timeout(self._update_tm)
            self._update_tm = None
        mforms.Form.close(self)


    def do_stop(self):
        if self.stop_callback:
            self.stop_callback()
        if self._done:
            self.close_callback()


    def run_command(self, command, on_finish=lambda: None):
        if self.stop_callback:
            self.stop.set_text("Stop")
            self.stop.set_enabled(True)
        else:
            self.stop.set_text("Close")
            self.stop.set_enabled(False)
        self.progress_label.set_text("Executing...")
        if self.progress_parser_callback:
            self.progress.set_indeterminate(False)
            self.progress.set_value(0)
        else:
            self.progress.set_indeterminate(True)
        self.progress.start()
        self._done = False
        self.queue = Queue()
        self.finished_callback = on_finish
        self.task = Task(self.ctrl_be, command, self.queue)
        self._update_tm = mforms.Utilities.add_timeout(0.5, weakcb(self, "_update"))
        self.task.start()


    def get_log_text(self):
        return self.logbox.get_string_value().encode("utf8")


    def append_log(self, text):
        self.logbox.append_text_and_scroll(text, True)