File: rapport.py

package info (click to toggle)
geophar 18.08.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,932 kB
  • sloc: python: 40,979; xml: 97; makefile: 95; sh: 45
file content (77 lines) | stat: -rw-r--r-- 2,698 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-

#    WxGeometrie
#    Dynamic geometry, graph plotter, and more for french mathematic teachers.
#    Copyright (C) 2005-2013  Nicolas Pourcelot
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import time
from ..pylib import print_error
from .. import param

class Rapport(list):
    def __init__(self, fichier_log = None, frequence_archivage = 100):
        list.__init__(self)
        self.fichier_log = fichier_log
        self.frequence_archivage = frequence_archivage
        try:
            # Créer un fichier vierge.
            f = None
            f = open(self.fichier_log, 'w')
            f.write(time.strftime("%d/%m/%Y - %H:%M:%S") + '\n')
            f.close()
        except:
            # Impossible de créer le fichier (problème de permissions, etc.)
            self.fichier_log = None
            print_error()
        finally:
            if f is not None:
                f.close()


    def append(self, valeur):
        if param.debug:
            print('')
            print(valeur)
            print('')
        list.append(self, valeur)
        if len(self) > self.frequence_archivage:
            self.archiver()

    def extend(self, liste):
        list.extend(self, liste)
        if len(self) > self.frequence_archivage:
            self.archiver()

    def _contenu(self):
        "Récupère le contenu récent (c-à-d. non archivé)."
        return '\n'.join(self) + '\n'

    def archiver(self):
        "Copie les derniers enregistrements vers le fichier log."
        if self.fichier_log is not None:
            with open(self.fichier_log, 'a', 'utf8') as f:
                f.write(self._contenu())
                self[:] = []

    def contenu(self):
        "Récupère le contenu complet, y compris ce qui a déjà été archivé."
        if self.fichier_log is None:
            return self._contenu()
        else:
            self.archiver()
            with open(self.fichier_log, 'r', 'utf8') as f:
                return f.read()