ScoDoc/misc/csv2rules.py

139 lines
3.5 KiB
Python
Raw Permalink Normal View History

2020-09-26 16:19:37 +02:00
#!/usr/bin/env python
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
2023-12-31 23:04:06 +01:00
# Copyright (c) 1999 - 2024 Emmanuel Viennet. All rights reserved.
2020-09-26 16:19:37 +02:00
#
# 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...)
"""
2021-07-09 21:44:39 +02:00
from __future__ import print_function
import sys
import app.scodoc.sco_utils as scu
2020-09-26 16:19:37 +02:00
sourcefile = sys.argv[1] # fichier CSV
ALL = "ALL"
HEAD = """# -*- coding: utf-8 -*-
#
# Generated by csv2rules.py *** DO NOT EDIT ***
#
# Command: %s %s
2023-12-31 23:04:06 +01:00
#
from codes_cursus import (
2021-02-02 14:49:49 +01:00
DUTRule,
ADC,
ADJ,
ADM,
AJ,
ALL,
ATB,
ATJ,
ATT,
CMP,
NAR,
NEXT,
RA_OR_NEXT,
RA_OR_RS,
RAT,
REO,
REDOANNEE,
REDOSEM,
RS_OR_NEXT,
)
2020-09-26 16:19:37 +02:00
rules_source_file='%s'
""" % (
sys.argv[0],
sys.argv[1],
sourcefile,
)
def _fmt(s):
if not s:
return None
if s.upper() in ("ok", "oui", "o", "y", "yes"):
2020-09-26 16:19:37 +02:00
return True
if s.upper() in ("no", "non"):
2020-09-26 16:19:37 +02:00
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)]"
)
2021-07-09 21:44:39 +02:00
print((genrules(open(sourcefile).readlines())))