summaryrefslogtreecommitdiff
path: root/src/program.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/program.py')
-rw-r--r--src/program.py52
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