Esempio n. 1
0
    def sort_parse(self, command, reverse=False):
        """
        Parses and validates the sort command
        Input:
            command - the user command
            reverse - the order of the sort
        Return:
            True on valid and False otherwise
        """
        self.set_command(command)

        # simple sort no type use total expense
        pattern_sort = self._regex_search(self._PATTERN_SORT)
        if pattern_sort:
            self._parsed_command = {"type": None, "reverse": reverse}
            return True

        # use an expense type for the sort
        pattern_sort_by_type = self._regex_search(self._PATTERN_SORT_BY_TYPE)
        if pattern_sort_by_type:
            expense_type = pattern_sort_by_type.group(1)
            if not Apartment.is_expense_type(expense_type):
                UI.set_message(UI.get_error_types())
                return False

            self._parsed_command = {"type": expense_type, "reverse": reverse}
            return True

        UI.set_message(UI.get_error_sort())
        return False
Esempio n. 2
0
    def run(self):
        """
        Main loop of the application
        """
        print(UI.get_help_menu())

        while True:
            # get the command from the user
            command = UI.get_command()

            # find out the command type and handle each command correctly
            if "help" in command:
                UI.set_message(UI.get_help_menu())

            elif ("quit" in command) or ("exit" in command):
                self.exit()

            elif "insert" in command:
                if self.bloc.insert_apartment_parse(command):
                    self._undo_start("insert")
                    self.bloc.insert_apartment()

            elif "replace" in command:
                if self.bloc.replace_apartment_parse(command):
                    self._undo_start("replace")
                    self.bloc.insert_apartment()

            elif "remove" in command:
                if self.bloc.remove_apartment_parse(command):
                    self._undo_start("remove")
                    self.bloc.remove_apartment()

            elif "list" in command:
                if "list" == command:
                    self.bloc.list_all()
                elif "greater" in command:
                    if self.bloc.list_greater_than_parse(command):
                        self.bloc.list_greater_than()

                elif "less" in command:
                    if self.bloc.list_less_than_parse(command):
                        self.bloc.list_less_than()

                elif ("sold" in command) or ("total" in command):
                    if self.bloc.list_total_apartment_parse(command):
                        self.bloc.list_total_apartment()

                else:  # check list by type
                    if self.bloc.list_by_type_parse(command):
                        self.bloc.list_by_type()

            elif "sum" in command:
                if self.bloc.stat_total_type_parse(command):
                    self.bloc.stat_total_type()

            elif "max" in command:
                if self.bloc.stat_max_apartment_parse(command):
                    self.bloc.stat_max_apartment()

            elif "filter" in command:
                if self.bloc.filter_parse(command):
                    self._undo_start("filter")
                    self.bloc.filter()

            elif "sort" in command:
                if "asc" in command:
                    if self.bloc.sort_parse(command, reverse=False):
                        self.bloc.sort()

                elif "desc" in command:
                    if self.bloc.sort_parse(command, reverse=True):
                        self.bloc.sort()

                else:
                    UI.set_message(UI.get_error_sort())

            elif "undo" in command:
                if self._undo_last_operation:
                    UI.set_message("Undo operation '" + self._undo_last_operation + "' finished")
                    self._undo_end()
                else:
                    UI.set_message("Nothing to undo")

            elif "save" == command:
                self.save()
                UI.set_message("Current bloc state saved to file.")

            else:
                UI.set_message("Command unknown. type help for a list of commands")

            # print all messages
            print(UI.get_message())