def import_repo_into_repo(import_repo, destination_repo): """ Reads a given import file in CSV format and copies it into a given directory. """ # TODO: Check whether to delete users from teachers list if they are marked as deleted. destination_file = BASE_PATH / destination_repo / TEACHER_LIST_FILENAME source_file = BASE_PATH / import_repo / TEACHER_LIST_FILENAME if destination_file.exists(): print('Fehler: Liste existiert bereits in angegebenen Repo.') return if not source_file.exists(): print('Fehler: Keine Liste in angegebenen Repo.') return # copy teachers list to new repo l = read_teacher_list(source_file) for t in l: # reset added flag because we are in new repo now if t.added: t.added = False write_teacher_list(l, destination_file) # copy blacklist to new repo destination_file = BASE_PATH / destination_repo / BLACKLIST_FILENAME source_file = BASE_PATH / import_repo / BLACKLIST_FILENAME try: shutil.copy(source_file, destination_file) except FileNotFoundError as e: logger.debug('No blacklist file was found: {}'.format(e))
def on_amend(args): if not current_repo: print('Fehler: Änderungen sind nur in Repo möglich.') return if not args: print('Fehler: Keine GUID angegeben.') return current_repo_list = current_path / TEACHER_LIST_FILENAME l = read_teacher_list(current_repo_list) # find teacher whose GUID starts with given argument chosen_teacher = [t for t in l if t.guid.startswith(args[0])] if len(chosen_teacher) != 1: print('Fehler: Kein oder zu viele Übereinstimmungen gefunden.') return # ask for changed information first_name = prompt('Geben Sie den neuen Vornamen ein: ', default=chosen_teacher[0].first_name) last_name = prompt('Geben Sie den neuen Nachnamen ein: ', default=chosen_teacher[0].last_name) email = prompt('Geben Sie die neue Email-Adresse ein: ', default=chosen_teacher[0].email) username = prompt('Geben Sie den neuen Benutzernamen ein: ', default=chosen_teacher[0].username) # remove old teacher and add amended teacher l.remove(chosen_teacher[0]) l.append( Teacher(last_name=last_name, first_name=first_name, email=email, username=username, guid=chosen_teacher[0].guid, password=chosen_teacher[0].password, added=chosen_teacher[0].added, deleted=chosen_teacher[0].deleted)) write_teacher_list(l, current_repo_list)
def teacher_list(*args, **kwds): if 'filename' in kwds: teacher_list_file = kwds['filename'] else: teacher_list_file = current_path / TEACHER_LIST_FILENAME if not teacher_list_file.exists(): print('Fehler: Aktuelles Repo enthält noch keine Listendatei.') yield [] else: try: l = read_teacher_list(teacher_list_file) yield l finally: write_teacher_list(l, teacher_list_file)
def on_stats(): if not current_repo: print('Fehler: Statistik ist nur in Repo möglich.') return current_repo_list = current_path / TEACHER_LIST_FILENAME teachers = read_teacher_list(current_repo_list) names = [t.first_name.strip() for t in teachers] occurrences = {k: v for k, v in Counter(names).items() if v > 1} occurrences = sorted(occurrences.items(), key=lambda kv: kv[1], reverse=True) maximum = max([int(x[1]) for x in occurrences]) for o in occurrences: print(' [{0: >15}] {1} ({2})'.format( o[0], '#' * int(60 / maximum * int(o[1])), o[1]))
def on_delete(args, purge=False): if not current_repo: # TODO: Add feature to delete complete Repos. print('Fehler: Löschen ist nur in Repo möglich.') return if not args: print('Fehler: Keine GUID angegeben.') return current_repo_list = current_path / TEACHER_LIST_FILENAME l = read_teacher_list(current_repo_list) # find teacher whose GUID starts with given argument chosen_teacher = [t for t in l if t.guid.startswith(args[0])] if len(chosen_teacher) != 1: print('Fehler: Kein oder zu viele Übereinstimmungen gefunden.') return really = prompt( 'Soll der Lehrer "{}" wirklich gelöscht werden? [y/N] '.format( chosen_teacher[0])) if really.lower() == 'y': l.remove(chosen_teacher[0]) if not purge: l.append(replace(chosen_teacher[0], deleted=True)) write_teacher_list(l, current_repo_list)