Debian Codegraph @

wxgeometrie/API/parametres.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##########################################################################
4 #
5 # Sauvegarde et chargement des paramètres
6 #
7 ##########################################################################
8 # WxGeometrie
9 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
10 # Copyright (C) 2005-2013 Nicolas Pourcelot
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 import time
27 NoneType = type(None)
28
29 from . import sauvegarde
30 from ..pylib import print_error, eval_safe
31 from .. import param
32
33 types_supportes = (int, str, float, bool, NoneType, list, tuple, dict)
34
35 # TO DO (?) :
36 # - rajouter le support des types array et complex dans securite.eval_safe
37 # - gérer correctement l'encodage dans save.py
38 # (tout convertir en unicode, puis en utf-8)
39
40
41 def sauvegarder_module(module, nom = "main"):
42 """Renvoie le contenu d'un module sous forme d'un fichier XML.
43
44 Au lieu du module lui-même, 'module' peut être un dictionnaire
45 correspondant au dictionnaire du module (éventuellement modifié).
46 """
47 dico = module.__dict__.copy() if not isinstance(module, dict) else module
48 for key in param.valeurs_a_ne_pas_sauver:
49 dico.pop(key, None)
50 f = sauvegarde.FichierGEO(type = 'Options WxGeometrie', module = nom)
51 m = f.ajouter("Meta")
52 f.ajouter("date", m, time.strftime("%d/%m/%Y - %H:%M:%S",time.localtime()))
53 p = f.ajouter("Parametres")
54 for key, value in dico.items():
55 if not key.startswith("_") and isinstance(value, types_supportes):
56 f.ajouter(key, p, repr(value))
57 return f
58
59
60 def actualiser_module(module, fichier):
61 "Rafraichit le contenu d'un module à partir d'un fichier XML."
62 fgeo, msg = sauvegarde.ouvrir_fichierGEO(fichier)
63 if fgeo is not None:
64 parametres = fgeo.contenu["Parametres"][-1]
65 try:
66 for key in parametres:
67 setattr(module, key, eval_safe(parametres[key][-1]))
68 except:
69 print("Module %s: impossible de lire le paramètre %s !" % (module, repr(key)))
70 print_error()
71
72
73 #def extraire_parametre(fichier, parametre):
74 # fgeo, msg = sauvegarde.ouvrir_fichierGEO(fichier)
75 # parametres = fgeo.contenu["Parametres"][-1]
76 # return parametres[parametre][-1]