diff options
Diffstat (limited to 'src/pyssg/database.py')
-rw-r--r-- | src/pyssg/database.py | 34 |
1 files changed, 12 insertions, 22 deletions
diff --git a/src/pyssg/database.py b/src/pyssg/database.py index d9c6467..40065ef 100644 --- a/src/pyssg/database.py +++ b/src/pyssg/database.py @@ -19,31 +19,25 @@ class Database: self.db_path: str = db_path self.e: dict[str, DatabaseEntry] = dict() - # updates the tags for a specific entry (file) - # file_name only contains the entry name (not an absolute path) - # TODO: make the function return true/false if updated def update_tags(self, file_name: str, new_tags: list[str]) -> None: # technically, I should ensure this function can only run if self.e is populated if file_name in self.e: log.debug('updating tags for entry "%s"', file_name) - log.debug('entry "%s" old content: %s', + log.debug('entry "%s" old tags: %s', file_name, self.e[file_name]) self.e[file_name].update_tags(new_tags) - log.debug('entry "%s" new content: %s', + log.debug('entry "%s" new tags: %s', file_name, self.e[file_name]) else: log.error('can\'t update tags for entry "%s",' ' as it is not present in db', file_name) sys.exit(1) - # returns a bool that indicates if the entry - # was (includes new entries) or wasn't updated def update(self, file_name: str, - remove: str='') -> bool: + remove: str='') -> None: log.debug('updating entry for file "%s"', file_name) - # initial default values f: str = file_name tags: list[str] = [] if remove != '': @@ -52,20 +46,20 @@ class Database: # get current time, needs actual file name time: float = os.stat(file_name).st_mtime - log.debug('modified time for "%s": %s', file_name, time) + log.debug('time for "%s": %s', file_name, time) # calculate current checksum, also needs actual file name checksum: str = get_checksum(file_name) - log.debug('current checksum for "%s": "%s"', file_name, checksum) + log.debug('checksum for "%s": "%s"', file_name, checksum) - # two cases, 1) entry didn't exist, + # three cases, 1) entry didn't exist, # 2) entry has been mod and, # 3) entry hasn't been mod #1) if f not in self.e: log.debug('entry "%s" didn\'t exist, adding with defaults', f) self.e[f] = DatabaseEntry([f, time, 0.0, checksum, tags]) - return True + return # old_e is old entity old_e: DatabaseEntry = self.e[f] @@ -73,25 +67,21 @@ class Database: # 2) if checksum != old_e.checksum: - if old_e.mtimestamp == 0.0: - log.debug('entry "%s" has been modified for the first' - ' time, updating', f) - else: - log.debug('entry "%s" has been modified, updating', f) + log.debug('entry "%s" has been modified, updating', f) self.e[f] = DatabaseEntry([f, old_e.ctimestamp, time, checksum, tags]) log.debug('entry "%s" new content: (%s, %s, %s, (%s))', f, self.e[f]) - return True + return # 3) else: log.debug('entry "%s" hasn\'t been modified', f) - return False + return def write(self) -> None: log.debug('writing db') with open(self.db_path, 'w') as file: + csv_writer = csv.writer(file, delimiter=self.__COLUMN_DELIMITER) for _, v in self.e.items(): log.debug('writing row: %s', v) - csv_writer = csv.writer(file, delimiter=self.__COLUMN_DELIMITER) csv_writer.writerow(v.get_raw_entry()) def _db_path_exists(self) -> bool: @@ -101,7 +91,6 @@ class Database: ' created once process finishes,' ' ignore if it\'s the first run', self.db_path) return False - if not os.path.isfile(self.db_path): log.error('"%s" is not a file"', self.db_path) sys.exit(1) @@ -115,6 +104,7 @@ class Database: log.debug('db contains %d rows', len(rows)) return rows + # TODO: don't include files that are not in the db anymore def read(self) -> None: log.debug('reading db') if not self._db_path_exists(): |