summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/arg_parser.py18
-rw-r--r--src/program.py52
-rw-r--r--src/table.py11
3 files changed, 70 insertions, 11 deletions
diff --git a/src/arg_parser.py b/src/arg_parser.py
index c2db3bd..a5fdb40 100644
--- a/src/arg_parser.py
+++ b/src/arg_parser.py
@@ -1,4 +1,5 @@
import argparse
+import sys
AVAILABLE_COMMANDS_HELP_STRING = """Available commands:
@@ -6,13 +7,24 @@ AVAILABLE_COMMANDS_HELP_STRING = """Available commands:
get-rows n: print first n rows
insert "csv string": insert row at the end, the row must have exactly 3 elements.
search "lookup": search "lookup" in the table.
+ delete-at n: delete row at nth position
"""
class ArgParser:
- def __init__(self):
+ def __init__(self, args=sys.argv):
self.parser = argparse.ArgumentParser()
self.parser.add_argument('-f', required=True, help='CSV file location')
self.parser.add_argument('-c', required=True, help=AVAILABLE_COMMANDS_HELP_STRING, nargs=2)
- self.args = self.parser.parse_args()
- print(self.args) \ No newline at end of file
+ self.args = self.parser.parse_args(args)
+
+ @property
+ def file(self):
+ return self.args.f
+
+ @property
+ def command(self):
+ return self.args.c
+
+ def print_help(self):
+ self.parser.print_help() \ No newline at end of file
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
diff --git a/src/table.py b/src/table.py
index 798fc9f..004bd4d 100644
--- a/src/table.py
+++ b/src/table.py
@@ -9,8 +9,7 @@ ROW_SIZE = 3
class Table:
def __init__(self, file_path: str):
self.csv_params = { 'delimiter': ',', 'quotechar': '"' }
- self.csv_file = open(file_path, 'r+', newline='')
- self.writer = csv.writer(self.csv_file, **self.csv_params)
+ self.csv_file = open(file_path, 'r', newline='')
self.rows = [tuple(row) for row in csv.reader(self.csv_file, **self.csv_params)]
def __del__(self):
@@ -18,7 +17,7 @@ class Table:
self.csv_file.close()
def __write_changes(self):
- with open(f"new_{token_hex(8)}.csv", 'w', newline='') as new_table:
+ with open(f"./new_{token_hex(8)}.csv", 'w', newline='') as new_table:
writer = csv.writer(new_table, **self.csv_params)
for row in self.rows:
writer.writerow(row)
@@ -37,13 +36,9 @@ class Table:
raise IncompatibleRowLengthError(len(new_row))
self.rows.append(new_row)
- self.writer.writerow(new_row)
def delete_at(self, to_delete: int):
- try:
- self.rows.pop(to_delete)
- except IndexError:
- return None
+ self.rows.pop(to_delete)
def search(self, lookup: str):
for row in self.rows: