Debian Codegraph @

wxgeometrie/pylib/rapport.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # WxGeometrie
4 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
5 # Copyright (C) 2005-2013 Nicolas Pourcelot
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21 import time
22 from ..pylib import print_error
23 from .. import param
24
25 class Rapport(list):
26 def __init__(self, fichier_log = None, frequence_archivage = 100):
27 list.__init__(self)
28 self.fichier_log = fichier_log
29 self.frequence_archivage = frequence_archivage
30 try:
31 # Créer un fichier vierge.
32 f = None
33 f = open(self.fichier_log, 'w')
34 f.write(time.strftime("%d/%m/%Y - %H:%M:%S") + '\n')
35 f.close()
36 except:
37 # Impossible de créer le fichier (problème de permissions, etc.)
38 self.fichier_log = None
39 print_error()
40 finally:
41 if f is not None:
42 f.close()
43
44
45 def append(self, valeur):
46 if param.debug:
47 print('')
48 print(valeur)
49 print('')
50 list.append(self, valeur)
51 if len(self) > self.frequence_archivage:
52 self.archiver()
53
54 def extend(self, liste):
55 list.extend(self, liste)
56 if len(self) > self.frequence_archivage:
57 self.archiver()
58
59 def _contenu(self):
60 "Récupère le contenu récent (c-à-d. non archivé)."
61 return '\n'.join(self) + '\n'
62
63 def archiver(self):
64 "Copie les derniers enregistrements vers le fichier log."
65 if self.fichier_log is not None:
66 with open(self.fichier_log, 'a', 'utf8') as f:
67 f.write(self._contenu())
68 self[:] = []
69
70 def contenu(self):
71 "Récupère le contenu complet, y compris ce qui a déjà été archivé."
72 if self.fichier_log is None:
73 return self._contenu()
74 else:
75 self.archiver()
76 with open(self.fichier_log, 'r', 'utf8') as f:
77 return f.read()