Beispiel #1
0
    def execute_cycle(self):
        str_command = input(">: ")
        with self._printer as printer:
            try:
                name, args = parse_command(str_command)
                command = self._command_register.get_command(name)
            except KeyError as k:
                self._printer.error(normalize_string(str(k)))
                self._printer.error(
                    "Please refer to 'help' / '?' to list all available commands."
                )
                return

            if command.min_arg_count <= len(args) <= command.max_arg_count:
                try:
                    command(*args)
                except Exception as e:
                    self._printer.error(
                        f'{e.__class__.__name__}: {e}\n{format_exc()}')
            else:
                if command.min_arg_count == command.max_arg_count:
                    limitation = f"exactly {command.min_arg_count}"
                else:
                    limitation = f"between {command.min_arg_count} and {command.max_arg_count}"

                printer.error(
                    f"The command '{command.name}' needs {limitation} arguments, but got {len(args)}."
                )

        self._printer.inform()
Beispiel #2
0
def select_student_by_name(value,
                           storage: InteractiveDataStorage,
                           printer,
                           mode='all',
                           too_much_limit=11):
    value = normalize_string(value)
    possible_students = storage.get_students_by_name(value, mode=mode)

    if 1 < len(possible_students) < too_much_limit:
        selected_index = single_choice(
            f"Found {len(possible_students)} possible choices for '{value}'",
            possible_students, printer)
        if selected_index is not None:
            return possible_students[selected_index]
    elif too_much_limit <= len(possible_students):
        if printer.yes_no(
                f"There are {len(possible_students)} of possibilities for '{value}'. Show them all? (y/n)",
                default=None):
            selected_index = single_choice(
                f"Found {len(possible_students)} possible choices",
                possible_students, printer)
            if selected_index is not None:
                return possible_students[selected_index]
    elif len(possible_students) == 1:
        return possible_students[0]
Beispiel #3
0
    def _print_tutor_info(self, value):
        value = normalize_string(value)
        all_tutors = self._storage.get_all_tutors()
        possible_tutors = [tutor for tutor in all_tutors if value in tutor]

        if len(possible_tutors) == 0:
            self.printer.error(f"No tutor found containing '{value}'")
            tutor = None
        elif len(possible_tutors) == 1:
            tutor = possible_tutors[0]
        else:
            selected_index = single_choice(
                f"Found {len(possible_tutors)} possible tutors",
                possible_tutors, self.printer)
            if selected_index is not None:
                tutor = possible_tutors[selected_index]
            else:
                tutor = None

        if tutor is None:
            self.printer.warning("Canceled")
        else:
            tutorials = self._storage.get_all_tutorials_of_tutor(tutor)
            mail = tutorials[0].tutor_mail

            data = {"E-Mail": mail}
            for tutorial in tutorials:
                data[
                    f'Tutorial-{tutorial.tutorial_id}'] = f'{tutorial.time} - {tutorial.location}'
            for line in string_card(tutor,
                                    data,
                                    length=120,
                                    title_orientation='^'):
                self.printer.inform(line)
Beispiel #4
0
def select_student_by_name(value,
                           storage,
                           printer,
                           action,
                           mode='all',
                           too_much_limit=11):
    value = normalize_string(value)
    possible_students = storage.get_students_by_name(value, mode=mode)
    student = None

    if 1 < len(possible_students) < too_much_limit:
        selected_index = single_choice(
            f"Found {len(possible_students)} possible choices",
            possible_students, printer)
        if selected_index is not None:
            student = possible_students[selected_index]
    elif too_much_limit <= len(possible_students):
        printer.inform(
            "There are a lot of possibilities. Show them all? (y/n)")
        if printer.input(">: ") == 'y':
            selected_index = single_choice(
                f"Found {len(possible_students)} possible choices",
                possible_students, printer)
            if selected_index is not None:
                student = possible_students[selected_index]
    elif len(possible_students) == 1:
        student = possible_students[0]

    if student is None:
        printer.warning("Canceled")
    else:
        action(student)

    return student