def filter_parse(self, command): """ Parses and validates the filter command Input: command - the user command Return: True on valid and False otherwise """ self.set_command(command) # filter by total amount greater than pattern_total_greater = self._regex_search(self._PATTERN_FILTER_GREATER_THAN_TOTAL) if pattern_total_greater: greater = pattern_total_greater.group(1) self._parsed_command = {"greater": greater, "type": None} return True # filter by only apartments having that type pattern_by_type = self._regex_search(self._PATTERN_FILTER_BY_TYPE) if pattern_by_type: expense_type = pattern_by_type.group(1) if not Apartment.is_expense_type(expense_type): UI.set_message(UI.get_error_types()) return False self._parsed_command = {"greater": None, "type": expense_type} return True UI.set_message(UI.get_error_filter()) return False
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
def _insert_or_replace_apartment_parse(self, command, command_type): """ Protected parse method for insert and replace Input: command - the user command command_type - the command_type, can be insert or replace Return: True on valid and False otherwise Raises: Exception on invalid command_type """ self.set_command(command) # get the correct pattern if command_type is "insert": pattern = self._regex_search(self._PATTERN_INSERT) if not pattern: UI.set_message(UI.get_error("insert <amount>, <expense_type> at <apartment_id>")) return False elif command_type is "replace": pattern = self._regex_search(self._PATTERN_REPLACE) if not pattern: UI.set_message(UI.get_error("replace <amount>, <expense_type> at <apartment_id>")) return False else: raise Exception("command type is incorrect") # get the data amount = pattern.group(1) expense_type = pattern.group(2) apartment_id = pattern.group(3) # wrong type if not Apartment.is_expense_type(expense_type): UI.set_message(UI.get_error_types()) return False if command_type is "insert": # apartment does exist in the list if self.is_apartment(apartment_id): UI.set_message("Apartment " + apartment_id + " does exist in the list. " "Use this command to replace: replace " + amount + ", " + expense_type + " at " + apartment_id) return False elif command_type is "replace": # apartment does not exist in the list if not self.is_apartment(apartment_id): UI.set_message("Apartment " + apartment_id + " does not exist in the list. " "Use this command to add: insert " + amount + ", " + expense_type + " at " + apartment_id) return False # all good self._parsed_command = {"type": expense_type, "amount": amount, "id": apartment_id} return True
def remove_apartment_parse(self, command): """ Parses and validates the remove command Input: command - the user command Return: True on valid and False otherwise """ self.set_command(command) # first case by apartment id pattern_remove_by_id = self._regex_search(self._PATTERN_REMOVE_BY_ID) if pattern_remove_by_id: apartment_id = pattern_remove_by_id.group(1) if not self.is_apartment(apartment_id): UI.set_message( "Apartment " + apartment_id + " does not exist in this list so all the expenses are by default 0") return False self._parsed_command = {"id": apartment_id} return True # second case remove from interval pattern_remove_by_interval = self._regex_search(self._PATTERN_REMOVE_BY_INTERVAL) if pattern_remove_by_interval: id_min = pattern_remove_by_interval.group(1) id_max = pattern_remove_by_interval.group(2) self._parsed_command = {"id_min": id_min, "id_max": id_max} return True # third case by type pattern_remove_by_type = self._regex_search(self._PATTERN_REMOVE_BY_TYPE) if pattern_remove_by_type: expense_type = pattern_remove_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} return True UI.set_message(UI.get_error("remove <apartment_id> \n\tremove from 5 to 10 \n\tremove type")) return False
def stat_total_type_parse(self, command): """ Parses and validates the 'sum type' command Input: command - the user command Return: True on valid and False otherwise """ self.set_command(command) # sum <type> pattern_sum = self._regex_search(self._PATTERN_STAT_TOTAL_TYPE) if pattern_sum: expense_type = pattern_sum.group(1) if not Apartment.is_expense_type(expense_type): UI.set_message(UI.get_error_types()) return False self._parsed_command = {"expense_type": expense_type} return True UI.set_message(UI.get_error("sum <type>")) return False
def list_by_type_parse(self, command): """ Parses and validates the list command Input: command - the user command Return: True on valid and False otherwise """ self.set_command(command) # list <type> pattern_type = self._regex_search(self._PATTERN_LIST_BY_TYPE) if pattern_type: expense_type = pattern_type.group(1) if not Apartment.is_expense_type(expense_type): UI.set_message(UI.get_error_types()) return False self._parsed_command = {"expense_type": expense_type} return True UI.set_message(UI.get_error("list <expense_type>")) return False