Debian Codegraph @

wxgeometrie/geolib/contexte.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##--------------------------------------#######
4 # Geolib #
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 from .. import param
26
27
28 class Contexte(dict):
29 """Gestionnaire de contexte.
30
31 Exemple d'usage:
32 >>> from wxgeometrie.geolib.contexte import Contexte
33 >>> # Contexte global
34 >>> contexte = Contexte(exact = False, decimales = 7)
35 >>> # Contexte local
36 >>> with contexte(exact = True):
37 ... print contexte['exact']
38 True
39 >>> print contexte['exact']
40 False
41 """
42
43 __slots__ = '__local_dicts', '__no_direct_call', '__parent'
44
45 def __init__(self, parent=None, **kw):
46 dict.__init__(self, **kw)
47 self.__local_dicts = []
48 self.__parent = parent
49 # Surveille que la méthode .__call__() ne soit jamais appelée directement
50 self.__no_direct_call = True
51
52 def __getitem__(self, key):
53 # On cherche d'abord dans les contextes locaux (en commençant par le dernier)
54 for dico in reversed(self.__local_dicts):
55 if key in dico:
56 return dico[key]
57 if key in self:
58 return dict.__getitem__(self, key)
59 elif self.__parent is not None:
60 return self.__parent[key]
61 raise KeyError(key)
62
63 def get(self, key, default=None):
64 try:
65 return self[key]
66 except KeyError:
67 return default
68
69 def new(self):
70 """Crée un nouveau contexte-fils, qui hérite de celui-ci.
71
72 Toutes les clés non trouvées du contexte-fils sont ensuite
73 cherchées dans le contexte père."""
74 return self.__class__(parent=self)
75
76 def __call__(self, **kw):
77 """Cette méthode ne doit *JAMAIS* être appelée en dehors d'un contexte 'with'.
78
79 Exemple d'usage:
80 >>> from wxgeometrie.geolib.contexte import Contexte
81 >>> contexte = Contexte(exact = False, decimales = 7)
82 >>> with contexte(exact = True):
83 ... print contexte['exact']
84 True
85 """
86 # On ajoute un contexte local
87 self.__local_dicts.append(kw)
88 # On surveille que la méthode .__call__() ne soit jamais appelée directement
89 # Cela conduirait à des memory leaks (empilement de contextes locaux jamais effacés)
90 assert self.__no_direct_call, "Utilisation precedente en dehors d'un contexte 'with'."
91 self.__no_direct_call = False
92 return self
93
94 def __enter__(self):
95 # La méthode __enter__() est appelée juste après la méthode __call__()
96 self.__no_direct_call = True
97
98 def __exit__(self, type, value, traceback):
99 # On supprime le dernier contexte local
100 self.__local_dicts.pop()
101
102
103
104
105 contexte = Contexte(exact = True,
106 decimales = param.decimales,
107 unite_angle = param.unite_angle,
108 tolerance = param.tolerance,
109 afficher_messages = param.afficher_messages,
110 graduation = param.graduation,
111 separateur_decimal = param.separateur_decimal,
112 )