Salome HOME
PN : Choix automatique du concept lorsqu'un seul choix est possible.
[tools/eficas.git] / Editeur / Interp.py
1 #            CONFIGURATION MANAGEMENT OF EDF VERSION
2 # ======================================================================
3 # COPYRIGHT (C) 1991 - 2002  EDF R&D                  WWW.CODE-ASTER.ORG
4 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
5 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
6 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
7 # (AT YOUR OPTION) ANY LATER VERSION.
8 #
9 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
10 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
11 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
12 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
13 #
14 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
15 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
16 #    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
17 #
18 #
19 # ======================================================================
20
21 import Tkinter,ScrolledText
22 import os, sys, string, traceback 
23 import code
24
25 sys.ps1 = ">>> "
26 sys.ps2 = "... "
27
28 class PythonInterpreter( code.InteractiveConsole):
29     def __init__( self, text, namespace = None):
30         code.InteractiveConsole.__init__( self, namespace)
31         self.text = text
32
33     def showtraceback( self):
34         start = self.text.pos + " - 1 lines"
35         code.InteractiveConsole.showtraceback( self)
36         end = self.text.pos
37         self.text.tag_add( "exception", start, end)
38
39 class InterpWindow(Tkinter.Toplevel):
40     def __init__(self,namespace, parent=None):
41         Tkinter.Toplevel.__init__(self,parent)
42         self._initTkWidgets()
43         self.stdout = self.stderr = self
44         self.pos = '1.0'
45         self.history = [ '' ]
46         self.hpos = 0
47         self.tabCount = 0
48         self.shell = PythonInterpreter( self,namespace)
49         self.write("Python %s on %s\n%s\n(%s)\n" %
50                        (sys.version, sys.platform, sys.copyright,
51                         self.__class__.__name__))
52         self.write( sys.ps1)
53         self.text.focus_set()
54
55     def _initTkWidgets( self):
56         self.text = ScrolledText.ScrolledText( self, bg = "white",fg="black", wrap="word")
57         self.text.pack( fill='both', expand = 1)
58         self.text.bind( '<KeyPress>', self.clearMsg)
59         self.text.bind( '<Return>', self.inputhandler)
60         self.text.bind( '<Up>', self.uphistory)
61         self.text.bind( '<Down>', self.downhistory)
62         self.text.bind( '<Control-a>', self.goto_sol)
63         self.text.bind( '<Control-d>', self.sendeof)
64         self.text.tag_config("exception", foreground = "red")
65
66     def swapStdFiles(self):
67         sys.stdout,self.stdout = self.stdout,sys.stdout
68         sys.stderr,self.stderr = self.stderr,sys.stderr
69
70     def write(self, data):
71         self.text.insert("end", data)
72         self.pos = self.text.index("end - 1 char")
73         self.text.yview_pickplace("end")
74
75     def tag_add( self, tag, start, end):
76         self.text.tag_add( tag, start, end)
77
78     def inputhandler(self, *args):
79         # Remove any extraneous stuff
80         self.text.delete( self.pos + " lineend", "end")
81         # Now get the line
82         line = self.text.get(self.pos, "end - 1 char")
83         self.text.insert("end", "\n")
84         self.pos = self.text.index("end")
85         self.addHistory( line)
86         self.swapStdFiles()
87         if self.shell.push( line):
88             self.write(sys.ps2)
89         else:
90             self.write(sys.ps1)
91         self.swapStdFiles()
92         self.text.mark_set("insert", "end")
93         return "break"
94
95     def addHistory( self, line):
96         if line:
97             self.history.insert( len( self.history) - 1, line)
98             self.hpos = len( self.history) - 1
99
100     def sendeof(self, *args):
101         self.destroy()
102         return "break"
103
104     def uphistory(self, event=None):
105         if not self.history: return "break"
106
107         if self.hpos > 0:
108             self.hpos = self.hpos - 1
109
110         line = self.history[ self.hpos]
111         self.text.delete( self.pos, "end")
112         self.text.insert( self.pos, line)
113
114         return "break"
115
116     def downhistory( self, event=None):
117         if not self.history: return "break"
118
119         if self.hpos < (len( self.history) - 1):
120             self.hpos = self.hpos + 1
121
122         line = self.history[ self.hpos]
123         self.text.delete( self.pos, "end")
124         self.text.insert( self.pos, line)
125
126         return "break"
127
128     def goto_sol( self, event=None):
129         """
130         Met en mode edition la ligne courante
131         """
132         self.text.mark_set( 'insert', 'insert linestart + 4 chars')
133         return "break"
134         
135     def clearMsg( self, event=None):
136         index = self.text.index( "insert")
137         self.text.delete( "insert lineend", "end")
138         self.tabCount = 0
139
140 if __name__ == "__main__":
141     app = Tkinter.Tk()
142     d={'a':1}
143
144     def go():
145       InterpWindow(d,parent=app)
146
147     Tkinter.Button(app,text="Interp",command=go).pack()
148     Tkinter.Button(app,text="Quit",command=app.destroy).pack()
149
150     app.mainloop()
151