Proyecto

General

Perfil

276 JuanAntoni
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# horarioXml.py
#
# Copyright 2011 Juan Antonio Cristín<programador@informatico>
#
# 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 Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

from xml.etree import ElementTree
from xml.dom.minidom import parse
import sys,os,time,errno,re

#De linea rayuela crea diccionario
def extraeDatosLineaRayuela(linea,dia):
datos = linea.split("|")
return { "dni_profesor" : datos[0], "nie_alumno" : datos[1], "inicio" : int(datos[2]), "final" : int(datos[3]), "dia": dia}

def transformaArchivos(datosrayuela):
#comprobar que existe dir y q contiene los ficheros y limpiar linea de los for
if os.path.isdir(datosrayuela):
try:
archivosRayuela = os.listdir(datosrayuela)
except:
return 3
print datosrayuela
#Limpiemos las listas de archivos
archivosRayuela = [ archivo for archivo in archivosRayuela if archivo in ["Lunes.txt","Martes.txt","Miercoles.txt","Jueves.txt","Viernes.txt"]]
listaLineas = []
#Procesemos cada archivo de Rayuela
for archivoRayuela in archivosRayuela:
filearchivoRayuela = open(os.path.join(datosrayuela,archivoRayuela), 'r')
while True:
linea = filearchivoRayuela.readline()
if not linea:
break
if linea[:6] == "D.N.I.":
continue
datosLineaRayuela = extraeDatosLineaRayuela(linea,archivoRayuela)
listaLineas.append(datosLineaRayuela)
filearchivoRayuela.close()
return listaLineas


#Parametro indice 6 de tupla resultado de: time.localtime()
def diaSemana(dia):
dias = ['Lunes','Martes','Miercoles','Jueves','Viernes']
return dias[dia]

def main():
dias = []
ahora = time.localtime()
minutoActual = int(ahora[3]*60+ahora[4])
diadeSemana = diaSemana(ahora[6])
arbolxml = ElementTree.parse('horarios.xml')
raiz = arbolxml.getroot()
lineas = transformaArchivos("/media/juancristin/xml/datos")
for i in lineas:
#cada dia un fichero
diaProcesado = i['dia']
# = i['dni_profesor']
nif = i['dni_profesor'][0:9]
inicio = i['inicio']
final = i['final']
niealumno = i['nie_alumno']
inicioFinal = [str(inicio),str(final)]
#nuevo (nodo) fichero de dia
if not diaProcesado in dias:
dia = ElementTree.SubElement(raiz,diaProcesado)
dias.append(diaProcesado)
profesores = []
#nuevo nodo profesor para un dia
if not nif in profesores:
profesores.append(nif)
profesor = ElementTree.SubElement(dia,'profesor',{'nif':nif})
horas = []
#nuevo nodo hora
if not inicioFinal in horas:
alumnos = []
horas.append(inicioFinal)
#comprobar en el nodo si es el 1º hijo
hora = ElementTree.SubElement(profesor,'hora',{'inicio':str(inicio),'fin':str(final)})
#nuevo nodo alumno
if not niealumno in alumnos:
addAlum = ElementTree.SubElement(hora,'alumno')
addAlum.set('nie',niealumno[0:7])
alumnos.append(niealumno)
arbolxml.write('horarios.xml', encoding='UTF-8')
return 0

if __name__ == '__main__': main()