~ubuntu-branches/ubuntu/vivid/openipmi/vivid

« back to all changes in this revision

Viewing changes to swig/python/openipmigui/gui_cmdwin.py

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2006-09-15 17:56:24 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060915175624-ljk0mg3xtcm65tvm
Tags: 2.0.7-1
* new upstream release from 2006-06-08
  Thanks to John Wright <john.wright hp.com> for initial work
  (closes: Bug#380149)
* updated Standards Version
* new binaries openipmicmd, openipmish, rmcp_ping

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gui_cmdwin.py
 
2
#
 
3
# openipmi GUI command window handling
 
4
#
 
5
# Author: MontaVista Software, Inc.
 
6
#         Corey Minyard <minyard@mvista.com>
 
7
#         source@mvista.com
 
8
#
 
9
# Copyright 2006 MontaVista Software Inc.
 
10
#
 
11
#  This program is free software; you can redistribute it and/or
 
12
#  modify it under the terms of the GNU Lesser General Public License
 
13
#  as published by the Free Software Foundation; either version 2 of
 
14
#  the License, or (at your option) any later version.
 
15
#
 
16
#
 
17
#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
18
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
19
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
20
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
22
#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 
23
#  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
24
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 
25
#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 
26
#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
#
 
28
#  You should have received a copy of the GNU Lesser General Public
 
29
#  License along with this program; if not, write to the Free
 
30
#  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
31
#
 
32
 
 
33
import Tix
 
34
import xml.dom
 
35
import xml.dom.minidom
 
36
import OpenIPMI
 
37
import _saveprefs
 
38
 
 
39
init_history = [ ]
 
40
 
 
41
class CommandWindow(Tix.ScrolledText):
 
42
    def __init__(self, parent, ui):
 
43
        global init_history
 
44
        Tix.ScrolledText.__init__(self, parent)
 
45
        self.ui = ui
 
46
        self.currow = 0
 
47
        self.max_lines = 1000
 
48
        self.max_history = 100
 
49
        self.text.bind("<Key>", self.HandleChar)
 
50
        self.text.bind("<Control-Key>", self.HandleCtrlChar)
 
51
        self.text.insert("end", "> ")
 
52
        self.history = [ ]
 
53
        self.lasthist = 0
 
54
        for cmd in init_history:
 
55
            self.history.append(cmd[1])
 
56
            self.lasthist += 1
 
57
            pass
 
58
        self.history.append("")
 
59
        init_history = None
 
60
        self.currhist = self.lasthist
 
61
 
 
62
        self.cmdlang = OpenIPMI.alloc_cmdlang(self)
 
63
        self.indent = 0;
 
64
        self.cmd_in_progress = False
 
65
 
 
66
        self.bind("<Destroy>", self.OnDestroy)
 
67
 
 
68
        OpenIPMI.set_cmdlang_global_err_handler(self)
 
69
        return
 
70
 
 
71
    def global_cmdlang_err(self, objstr, location, errstr, errval):
 
72
        log = "Global cmdlang err: " + errstr;
 
73
        if (len(location) > 0) or (len(objstr) > 0):
 
74
            log += " at " + objstr + "(" + location + ")"
 
75
            pass
 
76
        log += ": " + errstr + " (" + str(errval) + ")"
 
77
        self.ui.new_log(log)
 
78
        return
 
79
    
 
80
    def OnDestroy(self, event):
 
81
        self.cmdlang = None
 
82
        return
 
83
 
 
84
    def cmdlang_down(self, cmdlang):
 
85
        self.indent += 2
 
86
        return
 
87
    
 
88
    def cmdlang_up(self, cmdlang):
 
89
        if (self.indent >= 2):
 
90
            self.indent -= 2
 
91
            pass
 
92
        return
 
93
    
 
94
    def HandleNewLines(self):
 
95
        lastline = int(self.text.index("end").split(".")[0]) - 1
 
96
        while (lastline > self.max_lines):
 
97
            self.delete("1.0", "2.0")
 
98
            lastline = int(self.text.index("end").split(".")[0]) - 1
 
99
            pass
 
100
        return
 
101
 
 
102
    def InsertString(self, string):
 
103
        (lastrow, lastcol) = self.text.index("end").split(".")
 
104
        lastrow = str(int(lastrow)-1)
 
105
        self.text.insert(lastrow + ".0", string)
 
106
        self.HandleNewLines()
 
107
        self.text.see("insert")
 
108
        return
 
109
    
 
110
    def cmdlang_done(self, cmdlang):
 
111
        err = cmdlang.get_err()
 
112
        if (err != 0):
 
113
            errtext = cmdlang.get_errstr()
 
114
            objstr = cmdlang.get_objstr()
 
115
            location = cmdlang.get_location()
 
116
            if (location == None):
 
117
                location = ""
 
118
                pass
 
119
            if (objstr == ""):
 
120
                str = ("error: %s: %s (0x%x, %s)\n"
 
121
                       % (location, errtext, err,
 
122
                          OpenIPMI.get_error_string(err)))
 
123
                pass
 
124
            else:
 
125
                str = ("error: %s %s: %s (0x%x, %s)\n"
 
126
                       % (location, objstr, errtext, err,
 
127
                          OpenIPMI.get_error_string(err)))
 
128
                pass
 
129
            self.InsertString(str)
 
130
            pass
 
131
        self.cmd_in_progress = False
 
132
        self.text.insert("end", "> ")
 
133
        return
 
134
 
 
135
    def cmdlang_out(self, cmdlang, name, value):
 
136
        if (cmdlang.is_help()):
 
137
            self.InsertString("%*s%s %s\n" % (self.indent, "", name, value))
 
138
            pass
 
139
        else:
 
140
            self.InsertString("%*s%s: %s\n" % (self.indent, "", name, value))
 
141
            pass
 
142
        return
 
143
    
 
144
    def cmdlang_out_binary(self, cmdlang, name, value):
 
145
        self.InsertString("%*s%s: %s\n" % (self.indent, "", name, str(value)))
 
146
        return
 
147
    
 
148
    def cmdlang_out_unicode(self, cmdlang, name, value):
 
149
        self.InsertString("%*s%s:U: %s\n" % (self.indent, "", name, str(value)))
 
150
        return
 
151
    
 
152
    def HandleNewHistory(self):
 
153
        self.history.append("")
 
154
        if (self.lasthist >= self.max_history):
 
155
            del self.history[0]
 
156
            pass
 
157
        else:
 
158
            self.lasthist += 1
 
159
            pass
 
160
        return
 
161
    
 
162
    def HandleCtrlChar(self, event):
 
163
        # This is here to catch the control characters and pass them
 
164
        # on so HandleChar() doesn't trap and throw them away.
 
165
        return
 
166
    
 
167
    def HandleChar(self, event):
 
168
        key = event.keysym
 
169
        if ((key == "Backspace") or (key == "Delete")):
 
170
            # A key that will result in a backspace.  Make sure it
 
171
            # only occurs on the last line and not in the prompt area.
 
172
            if (self.cmd_in_progress):
 
173
                return "break"
 
174
            (lastrow, lastcol) = self.text.index("end").split(".")
 
175
            lastrow = str(int(lastrow)-1)
 
176
            (currrow, currcol) = self.text.index("insert").split(".")
 
177
            if ((lastrow != currrow) or (col <= 2)):
 
178
                # Ignore the keypress
 
179
                return "break"
 
180
            pass
 
181
        elif (key == "Return"):
 
182
            # Enter the command...
 
183
            if (self.cmd_in_progress):
 
184
                return "break"
 
185
            (lastrow, lastcol) = self.text.index("end").split(".")
 
186
            lastrow = str(int(lastrow)-1)
 
187
            (currrow, currcol) = self.text.index("insert").split(".")
 
188
            if ((lastrow != currrow) or (int(currcol) <= 2)):
 
189
                # Ignore the keypress
 
190
                return "break"
 
191
 
 
192
            command = self.text.get(lastrow + ".2", lastrow + ".end")
 
193
            self.HandleNewLines();
 
194
            if (command != ""):
 
195
                self.text.insert("end", "\n")
 
196
                self.history[self.lasthist] = command
 
197
                self.HandleNewHistory()
 
198
                self.cmdlang.handle(str(command))
 
199
                pass
 
200
            else:
 
201
                self.text.insert("end", "\n> ")
 
202
                pass
 
203
            self.text.mark_set("insert", "end")
 
204
            self.currhist = self.lasthist
 
205
            self.text.see("insert")
 
206
            return "break"
 
207
        elif (key == "Up"):
 
208
            # Previous history
 
209
            if (self.cmd_in_progress):
 
210
                return "break"
 
211
            if (self.currhist == 0):
 
212
                return "break"
 
213
            (lastrow, lastcol) = self.text.index("end").split(".")
 
214
            lastrow = str(int(lastrow)-1)
 
215
            if (self.currhist == self.lasthist):
 
216
                command = self.text.get(lastrow + ".2", lastrow + ".end")
 
217
                self.history[self.lasthist] = command
 
218
                pass
 
219
            self.text.delete(lastrow + ".2", lastrow + ".end")
 
220
            self.currhist -= 1
 
221
            self.text.insert(lastrow + ".2", self.history[self.currhist])
 
222
            return "break"
 
223
        elif (key == "Down"):
 
224
            if (self.cmd_in_progress):
 
225
                return "break"
 
226
            # Next history
 
227
            if (self.currhist == self.lasthist):
 
228
                return "break"
 
229
            (lastrow, lastcol) = self.text.index("end").split(".")
 
230
            lastrow = str(int(lastrow)-1)
 
231
            self.text.delete(lastrow + ".2", lastrow + ".end")
 
232
            self.currhist += 1
 
233
            self.text.insert(lastrow + ".2", self.history[self.currhist])
 
234
            return "break"
 
235
        elif (len(event.char) == 1) and (event.char < chr(255)):
 
236
            # A key that will result in text addition.  Make sure it
 
237
            # only occurs on the last line and not in the prompt area.
 
238
            if (self.cmd_in_progress):
 
239
                return "break"
 
240
            (lastrow, lastcol) = self.text.index("end").split(".")
 
241
            lastrow = str(int(lastrow)-1)
 
242
            (currrow, currcol) = self.text.index("insert").split(".")
 
243
            if ((lastrow != currrow) or (int(currcol) < 2)):
 
244
                # Ignore the keypress
 
245
                return "break"
 
246
            pass
 
247
        elif ((key == "Left") or (key == "Right") or
 
248
              (key == "Insert") or
 
249
              (key == "End") or (key == "Home") or
 
250
              (key == "Prior") or (key == "Next")):
 
251
            # Pass these through
 
252
            return
 
253
        else:
 
254
            return "break"
 
255
        return
 
256
 
 
257
    pass
 
258
 
 
259
def cmphist(a, b):
 
260
    return cmp(a[0], b[0])
 
261
    
 
262
def _HistorySave(file):
 
263
    if (not init_history):
 
264
        return
 
265
    domimpl = xml.dom.getDOMImplementation()
 
266
    doc = domimpl.createDocument(None, "IPMIHistory", None)
 
267
    main = doc.documentElement
 
268
    i = 0
 
269
    for cmd in init_history:
 
270
        if (cmd != ""):
 
271
            helem = doc.createElement("hval")
 
272
            helem.setAttribute("idx", str(i))
 
273
            helem.setAttribute("val", cmd)
 
274
            main.appendChild(helem)
 
275
            i += 1
 
276
            pass
 
277
        pass
 
278
    try:
 
279
        f = open(file, 'w')
 
280
        doc.writexml(f, indent='', addindent='\t', newl='\n')
 
281
    except:
 
282
        pass
 
283
    return
 
284
 
 
285
def _HistoryRestore(file):
 
286
    try:
 
287
        doc = xml.dom.minidom.parse(file).documentElement
 
288
    except:
 
289
        return
 
290
    for c in doc.childNodes:
 
291
        if (c.nodeType == c.ELEMENT_NODE):
 
292
            try:
 
293
                idx = int(c.getAttribute("idx"))
 
294
                val = c.getAttribute("val")
 
295
                init_history.append( (idx, val) )
 
296
                pass
 
297
            except:
 
298
                pass
 
299
            pass
 
300
        pass
 
301
    init_history.sort(cmphist)
 
302
    return