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 list_greater_than_parse(self, command): """ Parses and validates the 'list greater than' command Input: command - the user command Return: True on valid and False otherwise """ # list greater than <amount> self.set_command(command) pattern_greater = self._regex_search(self._PATTERN_LIST_GREATER) if pattern_greater: greater = pattern_greater.group(1) self._parsed_command = {"greater": greater} return True UI.set_message(UI.get_error("list greater than <amount>")) return False
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 list_less_than_parse(self, command): """ Parses and validates the 'list less than' command Input: command - the user command Return: True on valid and False otherwise """ # less than <amount> before <id> self.set_command(command) pattern_less = self._regex_search(self._PATTERN_LIST_LESS) if pattern_less: less = pattern_less.group(1) upper_id_limit = pattern_less.group(2) self._parsed_command = {"less": less, "upper_id_limit": upper_id_limit} return True UI.set_message(UI.get_error("list less than <amount> before <apartment_id>")) return False
def stat_max_apartment_parse(self, command): """ Parses and validates the 'max id' command Input: command - the user command Return: True on valid and False otherwise """ self.set_command(command) # max <id> pattern_max = self._regex_search(self._PATTERN_STAT_MAX_APARTMENT) if pattern_max: apartment_id = pattern_max.group(1) if not self.is_apartment(apartment_id): UI.set_message("Apartment " + apartment_id + " does not exist in the list") return False self._parsed_command = {"id": apartment_id} return True UI.set_message(UI.get_error("max <apartment_number>")) 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_total_apartment_parse(self, command): """ Parses and validates the 'list total' command Input: command - the user command Return: True on valid and False otherwise """ self.set_command(command) # sold|total <apartment_number> pattern_total = self._regex_search(self._PATTERN_LIST_TOTAL_APARTMENT) if pattern_total: apartment_id = pattern_total.group(1) if not self.is_apartment(apartment_id): UI.set_message("Apartment " + apartment_id + " does not exist in the list") return False self._parsed_command = {"id": apartment_id} return True UI.set_message(UI.get_error("list total <apartment_id>")) 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