ScolaSync: Fichier source de src/sconet.py

ScolaSync 5.1 |

sconet.py
Aller à la documentation de ce fichier.
1 # -*- coding: utf-8 -*-
2
3 licence={}
4 licence['en']="""
5 file sconet.py
6 this file is part of the project scolasync
7
8 Copyright (C) 2012 Georges Khaznadar <georgesk@ofset.org>
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 version3 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, see <http://www.gnu.org/licenses/>.
22 """
23
24 import xml.dom.minidom
25
26 ##
27 #
28 # Une classe pour travailler avec des données Sconet
29 #
30 class Sconet:
31
32 ##
33 #
34 # Le constructeur
35 # @param file le nom d'un fichier, ou un fichier ouvert en lecture
36 #
37 def __init__(self, file):
38 if type(file)==type(""):
39 try:
40 # python3 way
41 file=open(file, "r", encoding="iso-8859-1")
42 except:
43 # former way
44 file=open(file, "r")
45 self.donnees=xml.dom.minidom.parse(file)
46 self.makeCompact()
47
48 ##
49 #
50 # removes useless thext nodes containing only spaces.
51 #
52 def makeCompact(self):
53 self.nullTexts={}
54 self.elementsWalk(self.donnees.documentElement, self.collectNullTexts)
55 for el in self.nullTexts.keys():
56 for e in self.nullTexts[el]:
57 el.removeChild(e)
58
59 def collectNullTexts(self,el):
60 self.nullTexts[el]=[]
61 for e in el.childNodes:
62 if e.nodeType==e.TEXT_NODE and e.data.strip()=="":
63 self.nullTexts[el].append(e)
64
65 ##
66 #
67 # @return the list of classes containg students
68 #
69 def collectClasses(self):
70 self.classes=set()
71 self.elementsWalk(self.donnees.documentElement, self.collectOneClass)
72 return self.classes
73
74 ##
75 #
76 # @return the name of a class if it is a class with students
77 #
78 def collectOneClass(self,el):
79 if el.nodeName.lower()=="structure":
80 if el.getElementsByTagName("TYPE_STRUCTURE")[0].firstChild.data=="D":
81 self.classes.add(el.getElementsByTagName("CODE_STRUCTURE")[0].firstChild.data)
82
83
84
85 ##
86 #
87 # implemente un parcour des éléments d'un arbre, pour y appliquer
88 # une procédure
89 # @param el un élément
90 # @param proc la procédure à appliquer (paramètres : l'élément)
91 #
92 def elementsWalk(self, el, proc):
93 proc(el)
94 for e in el.childNodes:
95 self.elementsWalk(e, proc)
96
97 def __str__(self):
98 return self.donnees.toprettyxml(indent=" ",encoding="utf-8")
99
100
101 if __name__=="__main__":
102 s=Sconet("../exemples/SCONET_test.xml")
103 print (s.collectClasses())
src.sconet.Sconet.nullTexts
nullTexts
Definition: sconet.py:53
src.sconet.Sconet.collectNullTexts
def collectNullTexts(self, el)
Definition: sconet.py:59
src.sconet.Sconet.makeCompact
def makeCompact(self)
removes useless thext nodes containing only spaces.
Definition: sconet.py:52
src.sconet.Sconet.__str__
def __str__(self)
Definition: sconet.py:97
src.sconet.Sconet.__init__
def __init__(self, file)
Le constructeur.
Definition: sconet.py:37
src.sconet.Sconet.collectClasses
def collectClasses(self)
Definition: sconet.py:69
src.sconet.Sconet.collectOneClass
def collectOneClass(self, el)
Definition: sconet.py:78
src.sconet.Sconet
Une classe pour travailler avec des données Sconet.
Definition: sconet.py:30
src.sconet.Sconet.elementsWalk
def elementsWalk(self, el, proc)
implemente un parcour des éléments d'un arbre, pour y appliquer une procédure
Definition: sconet.py:92
src.sconet.Sconet.donnees
donnees
Definition: sconet.py:45
src.sconet.Sconet.classes
classes
Definition: sconet.py:70