ScolaSync: Fichier source de src/gestClasse.py

ScolaSync 5.1 |

gestClasse.py
Aller à la documentation de ce fichier.
1 #!/usr/bin/python
2
3 #Ce module permet de gérer des classes d'élèves. La classe
4 #AbstractGestClasse définit les fonctions minimales à implémenter
5 #pour chaque gestionnaire de classes.
6 #
7
8
9 licence={}
10 licence['en']="""
11 file gestClasse.py
12 this file is part of the project scolasync
13
14 Copyright (C) 2012 Georges Khaznadar <georgesk@ofset.org>
15
16 This program is free software: you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation, either version3 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 """
29
30 import xml.dom.minidom, copy
31
32 class AbstractGestClasse:
33
38
39 def __init__(self,f):
40 return
41
42
44
45 def collectClasses(self):
46 return []
47
48
51
52 def elevesDeClasse(self, cl):
53 return[]
54
55
58
59 def unique_name(self, el):
60 return "Cette fonction est abstraite, ne l'appelez pas ainsi."
61
62
65
66 def showable_name(self,el):
67 return "Cette fonction est abstraite, ne l'appelez pas ainsi."
68
69
71
72 class Sconet(AbstractGestClasse):
73
74
77
78 def __init__(self, f):
79 AbstractGestClasse.__init__(self,f)
80 if type(f)==type(""):
81 try:
82 # python3 way
83 file=open(f, "r", encoding="iso-8859-1")
84 except:
85 # former way
86 file=open(f, "r")
87 self.donnees=xml.dom.minidom.parse(f)
88 self.makeCompact()
89
90
92
93 def makeCompact(self):
94 self.nullTexts={}
95 self.elementsWalk(self.donnees.documentElement, self.collectNullTexts)
96 for el in self.nullTexts.keys():
97 for e in self.nullTexts[el]:
98 el.removeChild(e)
99
100 def collectNullTexts(self,el):
101 self.nullTexts[el]=[]
102 for e in el.childNodes:
103 if e.nodeType==e.TEXT_NODE and e.data.strip()=="":
104 self.nullTexts[el].append(e)
105
106
108
109 def collectClasses(self):
110 self.classes=set()
111 self.elementsWalk(self.donnees.documentElement, self.collectOneClass)
112 return self.classes
113
114
117
118 def elevesDeClasse(self, className):
119 self.currentResult=[]
120 self.currentClassName=className
121 self.elementsWalk(self.donnees.documentElement, self.unIDEleveDeClasse)
122 ids=copy.copy(self.currentResult)
123 self.currentResult=[]
124 for i in ids:
125 self.currentID=i
126 self.elementsWalk(self.donnees.documentElement, self.eleveParID)
127 return copy.copy(self.currentResult)
128
129 # appends the "eleve" element to the list self.currentResult if
130 # @code
131 # self.currentID is matched
132 #
133 # @endcode
134 #
135
136 def eleveParID(self,el):
137 if el.nodeName.lower()=="eleve" and el.getAttribute("ELEVE_ID")==self.currentID:
138 self.currentResult.append(el)
139
140 # appends the ID of an "eleve" to self.currentResult if he belongs to
141 # the class self.currentClassName
142 # @param el an element
143 #
144
145 def unIDEleveDeClasse(self, el):
146 if el.nodeName.lower()=="structures_eleve":
147 structures=el.getElementsByTagName("STRUCTURE")
148 if len (structures) > 0:
149 if structures[0].getElementsByTagName("TYPE_STRUCTURE")[0].firstChild.data=="D":
150 if structures[0].getElementsByTagName("CODE_STRUCTURE")[0].firstChild.data==self.currentClassName:
151 idEleve=el.getAttribute("ELEVE_ID")
152 self.currentResult.append(idEleve)
153
154
157
158 def collectOneClass(self,el):
159 if el.nodeName.lower()=="structure":
160 if el.getElementsByTagName("TYPE_STRUCTURE")[0].firstChild.data=="D":
161 self.classes.add(el.getElementsByTagName("CODE_STRUCTURE")[0].firstChild.data)
162
163
164
169
170 def unique_name(self, el, fields=["NOM", "PRENOM"]):
171 items=[]
172 for f in fields:
173 items.append(el.getElementsByTagName(f)[0].firstChild.data)
174 items.append(el.getAttribute("ELEVE_ID"))
175 return "_".join(items)
176
177
181
182 def showable_name(self,el, fields=["NOM", "PRENOM"]):
183 items=[]
184 for f in fields:
185 items.append(el.getElementsByTagName(f)[0].firstChild.data)
186 return " ".join(items)
187
188
189 # implemente un parcour des éléments d'un arbre, pour y appliquer
190 # une procédure
191 # @param el un élément
192 # @param proc la procédure à appliquer (paramètres : l'élément)
193 #
194
195 def elementsWalk(self, el, proc):
196 proc(el)
197 for e in el.childNodes:
198 self.elementsWalk(e, proc)
199
200 def __str__(self):
201 return self.donnees.toprettyxml(indent=" ",encoding="utf-8")
202
src.gestClasse.Sconet.collectNullTexts
def collectNullTexts(self, el)
Definition: gestClasse.py:100
src.gestClasse.Sconet.currentID
currentID
Definition: gestClasse.py:125
src.gestClasse.Sconet.elevesDeClasse
def elevesDeClasse(self, className)
Definition: gestClasse.py:118
src.gestClasse.AbstractGestClasse.collectClasses
def collectClasses(self)
Definition: gestClasse.py:45
src.gestClasse.Sconet.collectOneClass
def collectOneClass(self, el)
adds one class name to the set self.classes
Definition: gestClasse.py:158
src.gestClasse.Sconet.makeCompact
def makeCompact(self)
removes useless thext nodes containing only spaces.
Definition: gestClasse.py:93
src.gestClasse.AbstractGestClasse.__init__
def __init__(self, f)
le constructeur
Definition: gestClasse.py:39
src.gestClasse.AbstractGestClasse.showable_name
def showable_name(self, el)
Definition: gestClasse.py:66
src.gestClasse.Sconet.classes
classes
Definition: gestClasse.py:110
src.gestClasse.AbstractGestClasse
Definition: gestClasse.py:32
src.gestClasse.Sconet
Une classe pour travailler avec des données Sconet.
Definition: gestClasse.py:72
src.gestClasse.Sconet.eleveParID
def eleveParID(self, el)
Definition: gestClasse.py:136
src.gestClasse.Sconet.collectClasses
def collectClasses(self)
Definition: gestClasse.py:109
src.gestClasse.Sconet.__str__
def __str__(self)
Definition: gestClasse.py:200
src.gestClasse.Sconet.unique_name
def unique_name(self, el, fields=["NOM", "PRENOM"])
a unique name for an "eleve", based on a few fields and on the ID
Definition: gestClasse.py:170
src.gestClasse.Sconet.__init__
def __init__(self, f)
Le constructeur.
Definition: gestClasse.py:78
src.gestClasse.Sconet.elementsWalk
def elementsWalk(self, el, proc)
Definition: gestClasse.py:195
src.gestClasse.Sconet.unIDEleveDeClasse
def unIDEleveDeClasse(self, el)
Definition: gestClasse.py:145
src.gestClasse.AbstractGestClasse.elevesDeClasse
def elevesDeClasse(self, cl)
Definition: gestClasse.py:52
src.gestClasse.Sconet.showable_name
def showable_name(self, el, fields=["NOM", "PRENOM"])
Definition: gestClasse.py:182
src.gestClasse.Sconet.nullTexts
nullTexts
Definition: gestClasse.py:94
src.gestClasse.Sconet.currentResult
currentResult
Definition: gestClasse.py:119
src.gestClasse.Sconet.donnees
donnees
Definition: gestClasse.py:87
src.gestClasse.AbstractGestClasse.unique_name
def unique_name(self, el)
Definition: gestClasse.py:59
src.gestClasse.Sconet.currentClassName
currentClassName
Definition: gestClasse.py:120