DocScoDoc/csv2rules.py

118 lines
3.2 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
#!/usr/bin/env python
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
# Copyright (c) 1999 - 2020 Emmanuel Viennet. All rights reserved.
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Emmanuel Viennet emmanuel.viennet@viennet.net
#
##############################################################################
"""Converti fichier CSV decrivant regles parcours DUT
en "regles" python.
CSV: utf-8/Euro, tab, no text delimiter
(le faire avec OpenOffice ou NeoOffice...)
"""
import sys, pdb
sourcefile = sys.argv[1] # fichier CSV
ALL = "ALL"
HEAD = """# -*- coding: utf-8 -*-
#
# Generated by csv2rules.py *** DO NOT EDIT ***
#
# Command: %s %s
#
from sco_codes_parcours import *
rules_source_file='%s'
""" % (
sys.argv[0],
sys.argv[1],
sourcefile,
)
from sco_codes_parcours import *
def _fmt(s):
if not s:
return None
if strlower(s) in ("ok", "oui", "o", "y", "yes"):
return True
if strlower(s) in ("no", "non"):
return False
if s == "*":
return ALL
return s
colidx_code_sem = 7 # index colonne 'code_sem' dans feuille CSV
iue = colidx_code_sem + 1
icodeprev = 1 # idex col "Prev"
def genrules(csv):
r = []
linenum = 1
try:
for line in csv:
line = line.strip()
if line:
if line[0] == "#":
r.append(line)
else:
fs = [_fmt(s) for s in line.split("\t")]
if not "," in fs[icodeprev]:
fs[icodeprev] = fs[icodeprev] + "," # liste codes prev
if fs[iue] and not "," in fs[iue]:
fs[iue] = fs[iue] + "," # liste codes UE
if fs[iue]:
fs[iue] = "(" + fs[iue] + ")"
else:
fs[iue] = "()"
fs[-1] = "'" + fs[-1].replace("'", "\\'") + "'"
r.append(
"( '%s', ((%s), %s, %s, %s, %s, %s),"
% tuple(fs[:colidx_code_sem])
)
r.append(
" (%s, %s, %s, %s, %s, %s) )," % tuple(fs[colidx_code_sem:])
)
linenum += 1
except:
sys.stderr.write("error line %d on\n%s\n" % (linenum, csv[linenum]))
raise
return (
HEAD
+ "DUTRules = [ DUTRule(rid, p, c) for (rid, p,c) in (\n"
+ "\n".join(r)
+ "\n)]"
)
print(genrules(open(sourcefile).readlines()))