Compare View
Commits (3)
Showing
3 changed files
Show diff stats
.gitignore
aprendizations/initdb.py
| @@ -5,8 +5,9 @@ Initializes or updates database | @@ -5,8 +5,9 @@ Initializes or updates database | ||
| 5 | ''' | 5 | ''' |
| 6 | 6 | ||
| 7 | # python standard libraries | 7 | # python standard libraries |
| 8 | -import csv | ||
| 9 | import argparse | 8 | import argparse |
| 9 | +import csv | ||
| 10 | +from pandas import read_excel | ||
| 10 | import re | 11 | import re |
| 11 | from string import capwords | 12 | from string import capwords |
| 12 | 13 | ||
| @@ -32,11 +33,11 @@ def parse_commandline_arguments(): | @@ -32,11 +33,11 @@ def parse_commandline_arguments(): | ||
| 32 | 'command line. If the database does not exist, a new one' | 33 | 'command line. If the database does not exist, a new one' |
| 33 | ' is created.') | 34 | ' is created.') |
| 34 | 35 | ||
| 35 | - argparser.add_argument('csvfile', | 36 | + argparser.add_argument('files', |
| 36 | nargs='*', | 37 | nargs='*', |
| 37 | type=str, | 38 | type=str, |
| 38 | default='', | 39 | default='', |
| 39 | - help='CSV file to import (SIIUE)') | 40 | + help='CSV or Excel files to import (SIIUE)') |
| 40 | 41 | ||
| 41 | argparser.add_argument('--db', | 42 | argparser.add_argument('--db', |
| 42 | default='students.db', | 43 | default='students.db', |
| @@ -72,6 +73,30 @@ def parse_commandline_arguments(): | @@ -72,6 +73,30 @@ def parse_commandline_arguments(): | ||
| 72 | 73 | ||
| 73 | 74 | ||
| 74 | # =========================================================================== | 75 | # =========================================================================== |
| 76 | +def get_students_from_xlsx(filename): | ||
| 77 | + excel_settings = { | ||
| 78 | + 'skiprows': 3, | ||
| 79 | + 'converters': { "Número" : str } | ||
| 80 | + } | ||
| 81 | + students = [] | ||
| 82 | + try: | ||
| 83 | + file = read_excel(filename, **excel_settings) | ||
| 84 | + names = file['Aluno'] | ||
| 85 | + nums = file['Número'] | ||
| 86 | + students = [{ | ||
| 87 | + 'uid': num, | ||
| 88 | + 'name': capwords(re.sub(r'\(.*\)', '', name).strip()) | ||
| 89 | + } for num, name in zip(nums, names)] | ||
| 90 | + except FileNotFoundError as e: | ||
| 91 | + print(f'!!! File {filename} not found !!!') | ||
| 92 | + except ValueError as e: | ||
| 93 | + print(f'!!! File {filename} has wrong format !!!') | ||
| 94 | + except Exception as e: | ||
| 95 | + raise e | ||
| 96 | + | ||
| 97 | + return students | ||
| 98 | + | ||
| 99 | +# =========================================================================== | ||
| 75 | def get_students_from_csv(filename): | 100 | def get_students_from_csv(filename): |
| 76 | '''Reads CSV file with enrolled students in SIIUE format. | 101 | '''Reads CSV file with enrolled students in SIIUE format. |
| 77 | SIIUE names can have suffixes like "(TE)" and are sometimes capitalized. | 102 | SIIUE names can have suffixes like "(TE)" and are sometimes capitalized. |
| @@ -138,8 +163,13 @@ def main(): | @@ -138,8 +163,13 @@ def main(): | ||
| 138 | # --- make list of students to insert/update | 163 | # --- make list of students to insert/update |
| 139 | students = [] | 164 | students = [] |
| 140 | 165 | ||
| 141 | - for csvfile in args.csvfile: | ||
| 142 | - students += get_students_from_csv(csvfile) | 166 | + for file in args.files: |
| 167 | + if file.endswith('.csv'): | ||
| 168 | + students += get_students_from_csv(file) | ||
| 169 | + elif file.endswith('.xlsx'): | ||
| 170 | + students += get_students_from_xlsx(file) | ||
| 171 | + else: | ||
| 172 | + print(f'!!! Ignoring file {file} !!!') | ||
| 143 | 173 | ||
| 144 | if args.admin: | 174 | if args.admin: |
| 145 | students.append({'uid': '0', 'name': 'Admin'}) | 175 | students.append({'uid': '0', 'name': 'Admin'}) |
setup.py
| @@ -26,7 +26,9 @@ setup( | @@ -26,7 +26,9 @@ setup( | ||
| 26 | 'pygments>=2.14', | 26 | 'pygments>=2.14', |
| 27 | 'sqlalchemy>=2.0.0', | 27 | 'sqlalchemy>=2.0.0', |
| 28 | 'bcrypt>=4.0.1', | 28 | 'bcrypt>=4.0.1', |
| 29 | - 'networkx>=3.0' | 29 | + 'networkx>=3.0', |
| 30 | + 'pandas>=2.3', | ||
| 31 | + 'openpyxl' | ||
| 30 | ], | 32 | ], |
| 31 | entry_points={ | 33 | entry_points={ |
| 32 | 'console_scripts': [ | 34 | 'console_scripts': [ |