from PyQt4 import QtGui
class Scrutin:
@staticmethod
def read( filename = None ):
h = {}
if filename is None:
filename = QtGui.QFileDialog.getOpenFileName ( None, "Charger un scrutin existant...")
if filename is None or filename == "":
return
if isinstance(filename, file):
print "already a file"
f = filename
else:
print "just a filename"
f = open(filename, 'r')
if eval(f.readline()) != "SCRUTIN FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Lecture Scrutin", u"Fichier "+unicode(filename)+u" invalide : en-tête incorrect !")
return
h["Header"] = "SCRUTIN FILE version 1.0"
h["Titre Scrutin"] = eval(f.readline().replace ( "\n", "" ))
h["Date Debut"] = eval(f.readline().replace ( "\n", "" ))
h["Date Fin"] = eval(f.readline().replace ( "\n", "" ))
h["Type Scrutin"] = eval(f.readline().replace ( "\n", "" ))
h["Parametres Scrutin"] = eval(f.readline().replace ( "\n", "" ))
h["Electeurs"] = eval(f.readline())
h["Candidats"] = eval(f.readline())
h["Description Scrutin Html"] = eval(f.readline())
if eval(f.readline()) != "SCRUTIN FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Lecture Scrutin", u"Fichier invalide : la dernière ligne est incorrecte !")
return
if not isinstance(filename, file):
f.close()
return h
@staticmethod
def write( h , filename = None ):
if h["Header"] != "SCRUTIN FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Sauvegarde Scrutin", u"Données à sauver invalides (mauvais header) !")
return
if filename is None:
filename = QtGui.QFileDialog.getSaveFileName ( None, "Enregistrer le scrutin")
if filename is None or filename == "":
return
if isinstance(filename, file):
f = filename
else:
f = open(filename, 'w')
f.write(repr(h["Header"])+"\n")
f.write(repr(unicode(h["Titre Scrutin"]))+"\n")
f.write(repr(str(h["Date Debut"]))+"\n")
f.write(repr(str(h["Date Fin"]))+"\n")
f.write(repr(h["Type Scrutin"])+"\n")
f.write(repr(h["Parametres Scrutin"])+"\n")
f.write(repr(h["Electeurs"])+"\n")
f.write(repr(h["Candidats"])+"\n")
f.write(repr(h["Description Scrutin Html"].replace ( "\n", " " )) + "\n")
f.write(repr(h["Header"])+"\n")
if not isinstance(filename, file):
f.close()
return True
class Votes:
@staticmethod
def read( filename = None ):
h = {}
if filename is None:
filename = QtGui.QFileDialog.getOpenFileName ( None, "Charger un fichier de votes existant...")
if filename is None or filename == "":
return
if isinstance(filename, file):
f = filename
else:
f = open(filename, 'r')
print "Fichier ouvert"
if eval(f.readline()) != "VOTES FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Lecture Votes", u"Fichier "+unicode(filename)+u" invalide : en-tête incorrect !")
return False
print "Header OK"
h["Header"] = "VOTES FILE version 1.0"
h["Titre Votes"] = eval(f.readline().replace ( "\n", "" ))
h["Commentaires"] = eval(f.readline().replace ( "\n", "" ))
h["IdVotesFile"] = eval(f.readline().replace ( "\n", "" ))
h["Date Debut"] = eval(f.readline().replace ( "\n", "" ))
h["Date Fin"] = eval(f.readline().replace ( "\n", "" ))
print "Debut de lecture du scrutin de titre "+h["Titre Votes"]+" et finissant le "+h["Date Fin"]
h["Scrutin"] = Scrutin.read(f)
print "Scrutin ok"
h["Votes"] = eval(f.readline().replace ( "\n", "" ))
if eval(f.readline()) != "VOTES FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Lecture Votes", u"Fichier invalide : la dernière ligne est incorrecte !")
return
if not isinstance(filename, file):
f.close()
return h
@staticmethod
def write( h, filename = None ):
if h["Header"] != "VOTES FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Sauvegarde Votes", u"Données à sauver invalides (mauvais header) !")
return False
if filename is None:
filename = QtGui.QFileDialog.getSaveFileName ( None, u"Enregistrer les votes")
if filename is None or filename == "":
return False
if isinstance(filename, file):
f = filename
else:
f = open(filename, 'w')
f.write(repr(h["Header"])+"\n")
f.write(repr(unicode(h["Titre Votes"]))+"\n")
f.write(repr(unicode(h["Commentaires"]).replace ( "\n", "" ))+"\n")
f.write(repr(h["IdVotesFile"])+"\n")
f.write(repr(str(h["Date Debut"]))+"\n")
f.write(repr(str(h["Date Fin"]))+"\n")
Scrutin.write( h["Scrutin"], f)
f.write(repr(h["Votes"])+"\n")
f.write(repr(h["Header"])+"\n")
if not isinstance(filename, file):
f.close()
return True
class Registre:
@staticmethod
def read( filename = None ):
h = {}
if filename == None:
filename = QtGui.QFileDialog.getOpenFileName ( None, "Charger un fichier registre existant...")
if filename is None or filename == "":
return
if isinstance(filename, file):
f = filename
else:
f = open(filename, 'r')
if eval(f.readline()) != "REGISTRE FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Lecture Registre", u"Fichier "+unicode(filename)+u" invalide : en-tête incorrect !")
return
h["Header"] = "REGISTRE FILE version 1.0"
h["Titre Votes"] = eval(f.readline().replace ( "\n", "" ))
h["Commentaires"] = eval(f.readline().replace ( "\n", "" ))
h["IdVotesFile"] = eval(f.readline().replace ( "\n", "" ))
h["Date Debut"] = eval(f.readline().replace ( "\n", "" ))
h["Date Fin"] = eval(f.readline().replace ( "\n", "" ))
h["Scrutin"] = Scrutin.read(f)
h["Votants"] = eval(f.readline().replace ( "\n", "" ))
if eval(f.readline()) != "REGISTRE FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Lecture Registre", u"Fichier invalide : la dernière ligne est incorrecte !")
return
if not isinstance(filename, file):
f.close()
return h
@staticmethod
def write( h, filename = None ):
if h["Header"] != "REGISTRE FILE version 1.0":
QtGui.QMessageBox.warning(None, u"Sauvegarde Registre", u"Données à sauver invalides (mauvais header) !")
return
if filename is None:
filename = QtGui.QFileDialog.getSaveFileName ( None, u"Enregistrer le registre des votes")
if filename is None or filename == "":
return
if isinstance(filename, file):
f = filename
else:
f = open(filename, 'w')
f.write(repr(h["Header"])+"\n")
f.write(repr(unicode(h["Titre Votes"]))+"\n")
f.write(repr(unicode(h["Commentaires"]).replace ( "\n", "" ))+"\n")
f.write(repr(h["IdVotesFile"])+"\n")
f.write(repr(str(h["Date Debut"]))+"\n")
f.write(repr(str(h["Date Fin"]))+"\n")
Scrutin.write( h["Scrutin"], f)
h["Votants"].sort()
f.write(repr(h["Votants"])+"\n")
f.write(repr(h["Header"])+"\n")
if not isinstance(filename, file):
f.close()
return True