File: gui_cmdwin.py

package info (click to toggle)
openipmi 2.0.7-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,200 kB
  • ctags: 14,662
  • sloc: ansic: 126,919; sh: 9,454; python: 6,885; perl: 5,838; makefile: 507
file content (302 lines) | stat: -rw-r--r-- 10,484 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# gui_cmdwin.py
#
# openipmi GUI command window handling
#
# Author: MontaVista Software, Inc.
#         Corey Minyard <minyard@mvista.com>
#         source@mvista.com
#
# Copyright 2006 MontaVista Software Inc.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License
#  as published by the Free Software Foundation; either version 2 of
#  the License, or (at your option) any later version.
#
#
#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
#  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this program; if not, write to the Free
#  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import Tix
import xml.dom
import xml.dom.minidom
import OpenIPMI
import _saveprefs

init_history = [ ]

class CommandWindow(Tix.ScrolledText):
    def __init__(self, parent, ui):
        global init_history
        Tix.ScrolledText.__init__(self, parent)
        self.ui = ui
        self.currow = 0
        self.max_lines = 1000
        self.max_history = 100
        self.text.bind("<Key>", self.HandleChar)
        self.text.bind("<Control-Key>", self.HandleCtrlChar)
        self.text.insert("end", "> ")
        self.history = [ ]
        self.lasthist = 0
        for cmd in init_history:
            self.history.append(cmd[1])
            self.lasthist += 1
            pass
        self.history.append("")
        init_history = None
        self.currhist = self.lasthist

        self.cmdlang = OpenIPMI.alloc_cmdlang(self)
        self.indent = 0;
        self.cmd_in_progress = False

        self.bind("<Destroy>", self.OnDestroy)

        OpenIPMI.set_cmdlang_global_err_handler(self)
        return

    def global_cmdlang_err(self, objstr, location, errstr, errval):
        log = "Global cmdlang err: " + errstr;
        if (len(location) > 0) or (len(objstr) > 0):
            log += " at " + objstr + "(" + location + ")"
            pass
        log += ": " + errstr + " (" + str(errval) + ")"
        self.ui.new_log(log)
        return
    
    def OnDestroy(self, event):
        self.cmdlang = None
        return

    def cmdlang_down(self, cmdlang):
        self.indent += 2
        return
    
    def cmdlang_up(self, cmdlang):
        if (self.indent >= 2):
            self.indent -= 2
            pass
        return
    
    def HandleNewLines(self):
        lastline = int(self.text.index("end").split(".")[0]) - 1
        while (lastline > self.max_lines):
            self.delete("1.0", "2.0")
            lastline = int(self.text.index("end").split(".")[0]) - 1
            pass
        return

    def InsertString(self, string):
        (lastrow, lastcol) = self.text.index("end").split(".")
        lastrow = str(int(lastrow)-1)
        self.text.insert(lastrow + ".0", string)
        self.HandleNewLines()
        self.text.see("insert")
        return
    
    def cmdlang_done(self, cmdlang):
        err = cmdlang.get_err()
        if (err != 0):
            errtext = cmdlang.get_errstr()
            objstr = cmdlang.get_objstr()
            location = cmdlang.get_location()
            if (location == None):
                location = ""
                pass
            if (objstr == ""):
                str = ("error: %s: %s (0x%x, %s)\n"
                       % (location, errtext, err,
                          OpenIPMI.get_error_string(err)))
                pass
            else:
                str = ("error: %s %s: %s (0x%x, %s)\n"
                       % (location, objstr, errtext, err,
                          OpenIPMI.get_error_string(err)))
                pass
            self.InsertString(str)
            pass
        self.cmd_in_progress = False
        self.text.insert("end", "> ")
        return

    def cmdlang_out(self, cmdlang, name, value):
        if (cmdlang.is_help()):
            self.InsertString("%*s%s %s\n" % (self.indent, "", name, value))
            pass
        else:
            self.InsertString("%*s%s: %s\n" % (self.indent, "", name, value))
            pass
        return
    
    def cmdlang_out_binary(self, cmdlang, name, value):
        self.InsertString("%*s%s: %s\n" % (self.indent, "", name, str(value)))
        return
    
    def cmdlang_out_unicode(self, cmdlang, name, value):
        self.InsertString("%*s%s:U: %s\n" % (self.indent, "", name, str(value)))
        return
    
    def HandleNewHistory(self):
        self.history.append("")
        if (self.lasthist >= self.max_history):
            del self.history[0]
            pass
        else:
            self.lasthist += 1
            pass
        return
    
    def HandleCtrlChar(self, event):
        # This is here to catch the control characters and pass them
        # on so HandleChar() doesn't trap and throw them away.
        return
    
    def HandleChar(self, event):
        key = event.keysym
        if ((key == "Backspace") or (key == "Delete")):
            # A key that will result in a backspace.  Make sure it
            # only occurs on the last line and not in the prompt area.
            if (self.cmd_in_progress):
                return "break"
            (lastrow, lastcol) = self.text.index("end").split(".")
            lastrow = str(int(lastrow)-1)
            (currrow, currcol) = self.text.index("insert").split(".")
            if ((lastrow != currrow) or (col <= 2)):
                # Ignore the keypress
                return "break"
            pass
        elif (key == "Return"):
            # Enter the command...
            if (self.cmd_in_progress):
                return "break"
            (lastrow, lastcol) = self.text.index("end").split(".")
            lastrow = str(int(lastrow)-1)
            (currrow, currcol) = self.text.index("insert").split(".")
            if ((lastrow != currrow) or (int(currcol) <= 2)):
                # Ignore the keypress
                return "break"

            command = self.text.get(lastrow + ".2", lastrow + ".end")
            self.HandleNewLines();
            if (command != ""):
                self.text.insert("end", "\n")
                self.history[self.lasthist] = command
                self.HandleNewHistory()
                self.cmdlang.handle(str(command))
                pass
            else:
                self.text.insert("end", "\n> ")
                pass
            self.text.mark_set("insert", "end")
            self.currhist = self.lasthist
            self.text.see("insert")
            return "break"
        elif (key == "Up"):
            # Previous history
            if (self.cmd_in_progress):
                return "break"
            if (self.currhist == 0):
                return "break"
            (lastrow, lastcol) = self.text.index("end").split(".")
            lastrow = str(int(lastrow)-1)
            if (self.currhist == self.lasthist):
                command = self.text.get(lastrow + ".2", lastrow + ".end")
                self.history[self.lasthist] = command
                pass
            self.text.delete(lastrow + ".2", lastrow + ".end")
            self.currhist -= 1
            self.text.insert(lastrow + ".2", self.history[self.currhist])
            return "break"
        elif (key == "Down"):
            if (self.cmd_in_progress):
                return "break"
            # Next history
            if (self.currhist == self.lasthist):
                return "break"
            (lastrow, lastcol) = self.text.index("end").split(".")
            lastrow = str(int(lastrow)-1)
            self.text.delete(lastrow + ".2", lastrow + ".end")
            self.currhist += 1
            self.text.insert(lastrow + ".2", self.history[self.currhist])
            return "break"
        elif (len(event.char) == 1) and (event.char < chr(255)):
            # A key that will result in text addition.  Make sure it
            # only occurs on the last line and not in the prompt area.
            if (self.cmd_in_progress):
                return "break"
            (lastrow, lastcol) = self.text.index("end").split(".")
            lastrow = str(int(lastrow)-1)
            (currrow, currcol) = self.text.index("insert").split(".")
            if ((lastrow != currrow) or (int(currcol) < 2)):
                # Ignore the keypress
                return "break"
            pass
        elif ((key == "Left") or (key == "Right") or
              (key == "Insert") or
              (key == "End") or (key == "Home") or
              (key == "Prior") or (key == "Next")):
            # Pass these through
            return
        else:
            return "break"
        return

    pass

def cmphist(a, b):
    return cmp(a[0], b[0])
    
def _HistorySave(file):
    if (not init_history):
        return
    domimpl = xml.dom.getDOMImplementation()
    doc = domimpl.createDocument(None, "IPMIHistory", None)
    main = doc.documentElement
    i = 0
    for cmd in init_history:
        if (cmd != ""):
            helem = doc.createElement("hval")
            helem.setAttribute("idx", str(i))
            helem.setAttribute("val", cmd)
            main.appendChild(helem)
            i += 1
            pass
        pass
    try:
        f = open(file, 'w')
        doc.writexml(f, indent='', addindent='\t', newl='\n')
    except:
        pass
    return

def _HistoryRestore(file):
    try:
        doc = xml.dom.minidom.parse(file).documentElement
    except:
        return
    for c in doc.childNodes:
        if (c.nodeType == c.ELEMENT_NODE):
            try:
                idx = int(c.getAttribute("idx"))
                val = c.getAttribute("val")
                init_history.append( (idx, val) )
                pass
            except:
                pass
            pass
        pass
    init_history.sort(cmphist)
    return