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