diff options
author | HombreLaser <buran@silosneeded.com> | 2024-03-12 23:46:02 -0600 |
---|---|---|
committer | HombreLaser <buran@silosneeded.com> | 2024-03-12 23:46:02 -0600 |
commit | a0fcdc83cbc2b440a5836313b2afd1a35698f5ef (patch) | |
tree | 60cf56be85de882b8999d990feaaf2cf4f683dba /src/program.py | |
parent | afd4e795e3a7e7b34e0d3127f11c05c0e7a2fede (diff) |
Añade pruebas
Diffstat (limited to 'src/program.py')
-rw-r--r-- | src/program.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/program.py b/src/program.py new file mode 100644 index 0000000..1c43ea7 --- /dev/null +++ b/src/program.py @@ -0,0 +1,52 @@ +from src.arg_parser import ArgParser +from src.table import Table +from src.exceptions import IncompatibleRowLengthError +import sys + +class Program(): + def __init__(self, args=sys.argv[1:]): + self.arg_parser = ArgParser(args=args) + self.table = Table(self.arg_parser.file) + + def _command_arg_to_int(self): + try: + return int(self.arg_parser.command[1]) + except ValueError: + self.arg_parser.print_help() + exit() + + def _print_table_result(self, table): + for row in table: + print(row) + + def run(self): + match self.arg_parser.command[0]: + case 'row-at': + print(self.table.row_at(self._command_arg_to_int())) + case 'get-rows': + result = self.table.get_rows(self._command_arg_to_int()) + + self._print_table_result(result) + case 'insert': + new_row = tuple(self.arg_parser.command[1].rsplit(',')) + + try: + self.table.insert(new_row) + print('Row added!') + except IncompatibleRowLengthError as error: + print(error) + case 'search': + result = self.table.search(self.arg_parser.command[1]) + + if result is not None: + print(result) + else: + print('Not found!') + case 'delete-at': + try: + self.table.delete_at(self._command_arg_to_int()) + print(f"Row at {self._command_arg_to_int()} deleted") + except IndexError: + print(f"Couldn't find row at {self._command_arg_to_int()}") + case _: + arg_parser.print_help()
\ No newline at end of file |