Debian Codegraph @

wxgeometrie/API/console.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##########################################
4 # CONSOLE
5 ##########################################
6 # WxGeometrie
7 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
8 # Copyright (C) 2005-2013 Nicolas Pourcelot
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24
25 import sys
26 import traceback
27
28
29 def stacktraces():
30 code = []
31 for threadId, stack in sys._current_frames().items():
32 code.append("\n# ThreadID: %s" % threadId)
33 for filename, lineno, name, line in traceback.extract_stack(stack):
34 code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
35 if line:
36 code.append(" %s" % (line.strip()))
37 return "\n".join(code)
38
39
40 from .. import param
41
42
43 class Console:
44 def __init__(self, parent):
45 self.parent = parent
46 self.locals = {'main': self.parent,
47 'onglets': self.parent.onglets,
48 'param': param}
49
50 def executer(self, commande):
51 "La commande est exécutée dans l'espace de nom du panel."
52 commande = commande.strip()
53
54 # & est un raccourci pour print
55 if commande[0] == '&':
56 commande = 'print(' + commande[1:] + ')'
57
58 # Les racourcis suivants sont utilisables :
59 # Panel actuel :
60 commande = commande.replace("!p.", "panel.")
61 # Canvas actuel :
62 commande = commande.replace("!c.", "canvas.")
63 # Feuille utilisée actuellement :
64 commande = commande.replace("!f.", "feuille.")
65 # Objets de la feuille :
66 commande = commande.replace("!o.", "objets.")
67 commande = commande.replace("!g.", "moteur_graphique.")
68 # Fenêtre principale :
69 commande = commande.replace("!m.", "main.")
70 # Threads :
71 commande = commande.replace("!t", "print(threads)")
72
73 if commande in ('quit', 'exit', 'close'):
74 self.parent.close()
75 return
76 elif commande in ('restart', '!!!'):
77 self.parent.restart()
78 return
79
80 # if param.debug:
81 # self.parent.onglets.onglet_actuel.action_effectuee(u"REQUETE CONSOLE:" + commande)
82 # print u"REQUETE CONSOLE:" + commande
83 print("REQUETE CONSOLE [" + self.parent.onglets.onglet_actuel.titre + "]:\n>>> "
84 + commande)
85
86 self.locals.update({'panel': self.parent.onglets.onglet_actuel,
87 'canvas': self.parent.onglets.onglet_actuel.canvas,
88 'feuille': self.parent.onglets.onglet_actuel.feuille_actuelle,
89 'threads': stacktraces(),
90 })
91 for nom in param.modules:
92 if param.modules_actifs[nom]:
93 self.locals[nom] = getattr(self.parent.onglets, nom)
94 if self.parent.onglets.onglet_actuel.canvas is not None:
95 self.locals['objets'] = self.parent.onglets.onglet_actuel.feuille_actuelle.objets
96 self.locals['moteur_graphique'] = self.parent.onglets.onglet_actuel.canvas.graph
97 exec(commande, self.parent.__dict__, self.locals)