Debian Codegraph @

scripts/modules/console.py

raw symbols SCIP
1 #
2 # $Id$
3 #
4 # (C) Copyright 2001 Kai Sterker <[email protected]>
5 # Part of the Adonthell Project http://adonthell.linuxgames.com
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY.
11 #
12 # See the COPYING file for more details
13 #
14
15 import sys
16 import adonthell
17
18 # -- A simple python console with command history
19 class console (adonthell.win_container):
20
21 # -- Constructor
22 def __init__(self, ns):
23 adonthell.win_container.__init__(self)
24
25 self.namespace = ns
26 self.history = []
27 self.hist_idx = 0
28
29 # read the old history
30 self.read_history ()
31
32 self.py_signal_connect (self.on_update, adonthell.win_event_UPDATE)
33
34 # -- get font and theme
35 self.font = adonthell.win_manager_get_font ("silverleaf")
36 self.theme = adonthell.win_manager_get_theme ("silverleaf")
37
38 self.move (10, 150)
39 self.resize (300, 80)
40 self.set_border (self.theme)
41 self.set_background (self.theme)
42 self.set_trans_background (True)
43
44 self.info = adonthell.win_label ()
45 self.info.thisown = 0
46 self.info.move (5, 5)
47 self.info.resize (290, 15)
48 self.info.set_cursor_visible (False)
49 self.info.set_cursor_moveable (False)
50 self.info.set_font (adonthell.win_manager_get_font ("blue"))
51 self.info.set_text ("Python console (type 'quit' to exit)")
52 self.info.pack ()
53
54 self.entry = adonthell.win_write ()
55 self.entry.thisown = 0
56
57 self.entry.py_signal_connect (self.on_execute, adonthell.win_event_ACTIVATE_KEY)
58 self.entry.move (5, 20)
59 self.entry.resize (290, 55)
60 # -- causes a crash:
61 # self.entry.set_form (label_AUTO_HEIGHT)
62 self.entry.set_font (self.font)
63 self.entry.set_cursor_visible (True)
64 self.entry.set_cursor_moveable (True)
65 self.entry.set_text ("")
66 self.entry.pack ()
67
68 self.add (self.info)
69 self.add (self.entry)
70 self.set_focus_object (self.entry)
71
72 self.set_visible_background (True);
73 self.set_visible_border (True);
74 self.set_visible_all (True);
75 self.set_activate (True)
76
77 self.entry.set_focus (True)
78 self.entry.set_activate (True)
79
80 # -- callback for command execution
81 def on_execute (self):
82 text = self.entry.text_char ()
83
84 # -- if we have a command ...
85 if text != None:
86
87 # -- ... add it to command history ...
88 if self.hist_idx == 0 or text != self.history[-1]:
89 self.history.append (text + '\n')
90 self.hist_idx = len (self.history)
91
92 # -- quit?
93 if text == "quit" or text == "exit":
94 self.write_history ()
95 adonthell.gamedata_engine ().main_quit ()
96
97 # -- ... and try to execute it
98 try:
99 result = eval (text, self.namespace)
100 self.entry.set_text (str (result))
101 except:
102 type, value = sys.exc_info()[:2]
103 error = "Error:\n " + str (type) + ":\n \"" + str (value) + "\""
104 self.entry.set_text (error)
105
106 # -- catch relevant keypresses
107 def on_update (self):
108 # -- quit
109 if adonthell.input_has_been_pushed (adonthell.SDLK_TAB):
110 self.write_history ()
111 adonthell.gamedata_engine ().main_quit ()
112
113 # -- clear screen
114 elif adonthell.input_has_been_pushed (adonthell.SDLK_DELETE):
115 self.entry.set_text ("")
116
117 # -- previous command
118 elif adonthell.input_has_been_pushed (adonthell.SDLK_UP):
119 if self.hist_idx > 0:
120 self.hist_idx = self.hist_idx - 1
121 self.entry.set_text (self.history[ self.hist_idx ][:-1])
122
123 # -- next command
124 elif adonthell.input_has_been_pushed (adonthell.SDLK_DOWN):
125 if self.hist_idx < len (self.history) - 1:
126 self.hist_idx = self.hist_idx + 1
127 self.entry.set_text (self.history[ self.hist_idx ][:-1])
128
129 # -- Read the old history from ~/.adonthell/history
130 def read_history (self):
131 dir = adonthell.gamedata_user_data_dir ()
132 dir = dir + "/history"
133
134 # -- try to open the file
135 try:
136 file = open (dir, 'r')
137 except IOError:
138 return
139
140 if file != None:
141 self.history = file.readlines ()
142 self.hist_idx = len (self.history)
143 file.close ()
144
145 # -- Write the last 50 commands to ~/.adonthell/history
146 def write_history (self):
147 dir = adonthell.gamedata_user_data_dir ()
148 dir = dir + "/history"
149
150 # -- try to open the file
151 file = open (dir, 'w')
152 if file != None:
153 file.writelines (self.history[-50:])
154 file.close ()