Commit df573134e81d06b3134bd6cf96062d397a121d44

Authored by Miguel Barão
1 parent 1da435f2
Exists in master and in 1 other branch dev

modified initdb_from_csv.py to create fake students if no csv is provided

Showing 1 changed file with 41 additions and 17 deletions   Show diff stats
initdb_from_csv.py
1 1 #!/usr/bin/env python3
2 2 # -*- coding: utf-8 -*-
3 3  
  4 +import sys
4 5 import sqlite3
5 6 import csv
6 7 import argparse
... ... @@ -10,6 +11,7 @@ import string
10 11 import re
11 12  
12 13 # SIIUE names have alien strings like "(TE)" and are sometimes capitalized
  14 +# We remove them so that students dont keep asking what it means
13 15 def fixname(s):
14 16 return string.capwords(re.sub('\(.*\)', '', s).strip())
15 17  
... ... @@ -47,25 +49,47 @@ sql_cmd = '''PRAGMA foreign_keys = ON;
47 49 # --------- Parse command line options -----------
48 50 argparser = argparse.ArgumentParser(description='Create new database from a CSV file (SIIUE format)')
49 51 argparser.add_argument('--db', default='students.db', type=str, help='database filename')
50   -argparser.add_argument('csvfile', type=str, help='CSV filename')
  52 +argparser.add_argument('csvfile', nargs='?', type=str, default='', help='CSV filename')
51 53 args = argparser.parse_args()
52 54  
  55 +db_exists = os.path.exists(args.db)
53 56  
54   -# --------- Parse CSV --------
55   -with open(args.csvfile, encoding='iso-8859-1') as csvfile: # SIIUE format
56   - reader = csv.DictReader(csvfile, delimiter=';', quotechar='"')
57   - db_exists = os.path.exists(args.db)
  57 +with sqlite3.connect(args.db) as c:
  58 + # use existing or create new database schema
  59 + if db_exists:
  60 + print('-> Using previous database "{}"...'.format(args.db))
  61 + else:
  62 + print('-> Creating new database "{}"...'.format(args.db))
  63 + c.executescript(sql_cmd)
58 64  
59   - with sqlite3.connect(args.db) as c:
60   - if not db_exists:
61   - print('Creating new database "{}"...'.format(args.db))
62   - c.executescript(sql_cmd)
63   - c.execute('INSERT INTO students VALUES (?,?,?)', ('0', 'Professor', ''))
64   - else:
65   - print('Database "{}" already exists.'.format(args.db))
  65 + # get students
  66 + if args.csvfile:
  67 + csvfile = open(args.csvfile, encoding='iso-8859-1')
  68 + print('-> Using students from CSV file "{}"...'.format(args.csvfile))
  69 + students = genstudent(csv.DictReader(csvfile, delimiter=';', quotechar='"'))
  70 + else:
  71 + print('-> Creating fake students numbered 1 to 5...'.format(args.csvfile))
  72 + students = [
  73 + ('1', 'Student1', ''),
  74 + ('2', 'Student2', ''),
  75 + ('3', 'Student3', ''),
  76 + ('4', 'Student4', ''),
  77 + ('5', 'Student5', '')
  78 + ]
66 79  
67   - print('Inserting students into database... ', end='')
68   - try:
69   - c.executemany('INSERT INTO students VALUES (?,?,?)', genstudent(reader))
70   - except sqlite3.IntegrityError:
71   - print('\rStudents already in the database. Aborting!!!')
  80 + # insert students into database
  81 + print('-> Inserting students into database... ')
  82 + try:
  83 + c.executemany('INSERT INTO students VALUES (?,?,?)', students)
  84 + except sqlite3.IntegrityError:
  85 + print('** ERROR ** Students already exist. Aborted!')
  86 + sys.exit(1)
  87 +
  88 + # insert professor into database
  89 + print('-> Inserting professor (id=0)...')
  90 + try:
  91 + c.execute('INSERT INTO students VALUES (?,?,?)', ('0', 'Professor', ''))
  92 + except sqlite3.IntegrityError:
  93 + print('** WARNING ** Professor already exists.')
  94 +
  95 +print('Done.')
... ...