From 70215ecd6848c6139efa2d24b0f3facd84dca799 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Mon, 18 Apr 2022 01:15:09 -0600 Subject: fix db write bug missing newline, minor refactor --- src/pyssg/database.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/pyssg/database.py b/src/pyssg/database.py index 40e9ca2..837fac7 100644 --- a/src/pyssg/database.py +++ b/src/pyssg/database.py @@ -89,20 +89,20 @@ class Database: def write(self) -> None: log.debug('writing db') - with open(self.db_path, 'w', newline='\n') as file: - # write each k,v pair in dict to db file - for k, v in self.e.items(): - log.debug('parsing row for page "%s"', k) - t: str = None - row: str = None - if len(v[2]) == 0: - t = '-' - else: - t = ','.join(v[2]) - - row = f'{k} {v[0]} {v[1]} {t}' - log.debug('writing row: "%s"', row) - file.write(row) + for k, v in self.e.items(): + log.debug('parsing row for page "%s"', k) + t: str = None + row: str = None + if len(v[2]) == 0: + t = '-' + else: + t = ','.join(v[2]) + + row = f'{k} {v[0]} {v[1]} {t}' + + log.debug('writing row: "%s\\n"', row) + with open(self.db_path, 'w') as file: + file.write(f'{row}\n') def read(self) -> None: @@ -126,13 +126,14 @@ class Database: l: list[str] = None # l=list of values in entry log.debug('parsing rows from db') - for i, row in enumerate(rows): + for it, row in enumerate(rows): + i = it + 1 log.debug('row %d content: "%s"', i, row) l = tuple(row.strip().split()) if len(l) != self.COLUMN_NUM: - log.critical('row doesn\t contain %s columns,' + log.critical('row %d doesn\'t contain %s columns,' ' contains %d elements; row %d content: "%s"', - self.COLUMN_NUM, len(l), i, row) + i, self.COLUMN_NUM, len(l), i, row) sys.exit(1) t: list[str] = None -- cgit v1.2.3-70-g09d2