ScolaSync  4.0
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
sconet.py
Aller à la documentation de ce fichier.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 licence={}
5 licence['en']="""
6  file sconet.py
7  this file is part of the project scolasync
8 
9  Copyright (C) 2012 Georges Khaznadar <georgesk@ofset.org>
10 
11  This program is free software: you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version3 of the License, or
14  (at your option) any later version.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program. If not, see <http://www.gnu.org/licenses/>.
23 """
24 
25 python3safe=True
26 
27 import xml.dom.minidom
28 
29 ##
30 #
31 # Une classe pour travailler avec des données Sconet
32 #
33 class Sconet:
34 
35  ##
36  #
37  # Le constructeur
38  # @param file le nom d'un fichier, ou un fichier ouvert en lecture
39  #
40  def __init__(self, file):
41  if type(file)==type(""):
42  try:
43  # python3 way
44  file=open(file, "r", encoding="iso-8859-1")
45  except:
46  # former way
47  file=open(file, "r")
48  self.donnees=xml.dom.minidom.parse(file)
49  self.makeCompact()
50 
51  ##
52  #
53  # removes useless thext nodes containing only spaces.
54  #
55  def makeCompact(self):
56  self.nullTexts={}
57  self.elementsWalk(self.donnees.documentElement, self.collectNullTexts)
58  for el in self.nullTexts.keys():
59  for e in self.nullTexts[el]:
60  el.removeChild(e)
61 
62  def collectNullTexts(self,el):
63  self.nullTexts[el]=[]
64  for e in el.childNodes:
65  if e.nodeType==e.TEXT_NODE and e.data.strip()=="":
66  self.nullTexts[el].append(e)
67 
68  ##
69  #
70  # @return the list of classes containg students
71  #
72  def collectClasses(self):
73  self.classes=set()
74  self.elementsWalk(self.donnees.documentElement, self.collectOneClass)
75  return self.classes
76 
77  ##
78  #
79  # @return the name of a class if it is a class with students
80  #
81  def collectOneClass(self,el):
82  if el.nodeName.lower()=="structure":
83  if el.getElementsByTagName("TYPE_STRUCTURE")[0].firstChild.data=="D":
84  self.classes.add(el.getElementsByTagName("CODE_STRUCTURE")[0].firstChild.data)
85 
86 
87 
88  ##
89  #
90  # implemente un parcour des éléments d'un arbre, pour y appliquer
91  # une procédure
92  # @param el un élément
93  # @param proc la procédure à appliquer (paramètres : l'élément)
94  #
95  def elementsWalk(self, el, proc):
96  proc(el)
97  for e in el.childNodes:
98  self.elementsWalk(e, proc)
99 
100  def __str__(self):
101  return self.donnees.toprettyxml(indent=" ",encoding="utf-8")
102 
103 
104 if __name__=="__main__":
105  s=Sconet("../exemples/SCONET_test.xml")
106  print (s.collectClasses())
107