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 
28 
29 class Sconet:
30 
31 
34 
35  def __init__(self, file):
36  if type(file)==type(""):
37  try:
38  # python3 way
39  file=open(file, "r", encoding="iso-8859-1")
40  except:
41  # former way
42  file=open(file, "r")
43  self.donnees=xml.dom.minidom.parse(file)
44  self.makeCompact()
45 
46 
48 
49  def makeCompact(self):
50  self.nullTexts={}
51  self.elementsWalk(self.donnees.documentElement, self.collectNullTexts)
52  for el in self.nullTexts.keys():
53  for e in self.nullTexts[el]:
54  el.removeChild(e)
55 
56  def collectNullTexts(self,el):
57  self.nullTexts[el]=[]
58  for e in el.childNodes:
59  if e.nodeType==e.TEXT_NODE and e.data.strip()=="":
60  self.nullTexts[el].append(e)
61 
62 
64 
65  def collectClasses(self):
66  self.classes=set()
67  self.elementsWalk(self.donnees.documentElement, self.collectOneClass)
68  return self.classes
69 
70 
72 
73  def collectOneClass(self,el):
74  if el.nodeName.lower()=="structure":
75  if el.getElementsByTagName("TYPE_STRUCTURE")[0].firstChild.data=="D":
76  self.classes.add(el.getElementsByTagName("CODE_STRUCTURE")[0].firstChild.data)
77 
78 
79 
80  # implemente un parcour des éléments d'un arbre, pour y appliquer
81  # une procédure
82  # @param el un élément
83  # @param proc la procédure à appliquer (paramètres : l'élément)
84  #
85 
86  def elementsWalk(self, el, proc):
87  proc(el)
88  for e in el.childNodes:
89  self.elementsWalk(e, proc)
90 
91  def __str__(self):
92  return self.donnees.toprettyxml(indent=" ",encoding="utf-8")
93 
94 
95 if __name__=="__main__":
96  s=Sconet("../exemples/SCONET_test.xml")
97  print (s.collectClasses())
src.sconet.Sconet.nullTexts
nullTexts
Definition: sconet.py:50
src.sconet.Sconet.collectNullTexts
def collectNullTexts(self, el)
Definition: sconet.py:56
src.sconet.Sconet
Une classe pour travailler avec des données Sconet.
Definition: sconet.py:29
src.sconet.Sconet.makeCompact
def makeCompact(self)
removes useless thext nodes containing only spaces.
Definition: sconet.py:49
src.sconet.Sconet.__str__
def __str__(self)
Definition: sconet.py:91
src.sconet.Sconet.__init__
def __init__(self, file)
Le constructeur.
Definition: sconet.py:35
src.sconet.Sconet.elementsWalk
def elementsWalk(self, el, proc)
Definition: sconet.py:86
src.sconet.Sconet.collectClasses
def collectClasses(self)
Definition: sconet.py:65
src.sconet.Sconet.collectOneClass
def collectOneClass(self, el)
Definition: sconet.py:73
src.sconet.Sconet.classes
classes
Definition: sconet.py:66
src.sconet.Sconet.donnees
donnees
Definition: sconet.py:43