From 2f706bacf64772cbd113566814aeeccbfc6dae41 Mon Sep 17 00:00:00 2001 From: mike Date: Sun, 9 Jun 2024 09:41:00 -0700 Subject: [PATCH] Bug squashing --- domain/BackupRotator.py | 51 ++++++++++++++++++------------------- domain/config/Config.py | 8 +++--- domain/config/ConfigFile.py | 41 +++++++++++++++++++++-------- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/domain/BackupRotator.py b/domain/BackupRotator.py index b9e58b2..96b6291 100755 --- a/domain/BackupRotator.py +++ b/domain/BackupRotator.py @@ -128,36 +128,29 @@ class BackupRotator: f"Rotating path: {path}" ) - found_any_rotation_keys = False - if config.maximum_items: - - found_any_rotation_keys = True - - self._rotate_path_for_maximum_items( - config=config, - path=path, - ) + self._rotate_path_for_maximum_items( + config=config, + path=path, + ) - if config.maximum_age: - - found_any_rotation_keys = True - - self._rotate_path_for_maximum_age( - config=config, - path=path, - ) - - assert found_any_rotation_keys is True, ( - "Config needs one of the following keys: \"maximum-items\"" + self._rotate_path_for_maximum_age( + config=config, + path=path, ) def _rotate_path_for_maximum_items(self, config: ConfigFile, path: Path): assert path.is_dir(), f"Path should be a directory: {path}" - self.info( - f"Rotating path for a maximum of {config.maximum_items} items: {path}" - ) + if config.maximum_items: + self.info( + f"Rotating path for a maximum of {config.maximum_items} items: {path}" + ) + else: + self.info( + f"Not configured to rotate for maximum number of items." + ) + return candidate_items = self._gather_rotation_candidates(config=config, path=path) @@ -230,9 +223,15 @@ class BackupRotator: assert path.is_dir(), f"Path should be a directory: {path}" - self.info( - f"Rotating path for max age of {config.maximum_age} days: {path}" - ) + if config.maximum_age: + self.info( + f"Rotating path for max age of {config.maximum_age} days: {path}" + ) + else: + self.info( + f"Not configured to rotate for a maximum number of days." + ) + return candidate_items = self._gather_rotation_candidates(config=config, path=path) minimum_items = self._determine_minimum_items(config=config) diff --git a/domain/config/Config.py b/domain/config/Config.py index 10c5a44..4a63598 100644 --- a/domain/config/Config.py +++ b/domain/config/Config.py @@ -15,7 +15,7 @@ class Config: self.__logger = logger self.__config_files_paths: [Path] = config_files_paths - self.__configs: {} = None + self.__configs = {} self.__scanner = Scanner( logger=self.__logger @@ -23,9 +23,11 @@ class Config: self._consume_configs() - def _consume_configs(self, paths: [Path] = None): + def _consume_configs(self): - config_paths = self.__scanner.gather_valid_config_paths(paths=paths) + config_paths = self.__scanner.gather_valid_config_paths( + paths=self.__config_files_paths + ) for config_path in config_paths: diff --git a/domain/config/ConfigFile.py b/domain/config/ConfigFile.py index b5d069f..180c614 100644 --- a/domain/config/ConfigFile.py +++ b/domain/config/ConfigFile.py @@ -53,19 +53,19 @@ class ConfigFile: s = "" s += "*** Config File ***" - s += f"> Path: {self.__path}" - s += f"> Dry run: " + ("Yes" if self.__dry_run else "No") - s += f"> Minimum items: {self.__minimum_items}" - s += f"> Maximum items: {self.__maximum_items}" - s += f"> Maximum age (in days): {self.__maximum_age}" - s += f"> Target type: {self.__target_type}" - s += f"> Date detection: {self.__date_detection}" - s += f"> Rotatable paths: " + s += f"\n> Path: {self.__path}" + s += f"\n> Dry run: " + ("Yes" if self.__dry_run else "No") + s += f"\n> Minimum items: {self.__minimum_items}" + s += f"\n> Maximum items: {self.__maximum_items}" + s += f"\n> Maximum age (in days): {self.__maximum_age}" + s += f"\n> Target type: {self.__target_type}" + s += f"\n> Date detection: {self.__date_detection}" + s += f"\n> Rotatable paths: " if len(self.__rotatable_paths) > 0: for p in self.__rotatable_paths: - s += f">> {p}" + s += f"\n>> {p}" else: - s += ">> [none]" + s += "\n>> [none]" return s @@ -124,6 +124,14 @@ class ConfigFile: f"No minimum-items option found; Will use default: {self.__minimum_items}" ) + assert ( + "maximum-items" in options.keys() + or + "maximum-age" in options.keys() + ), ( + "Options should include either maximum-items or maximum-age" + ) + if "maximum-items" in options.keys(): maximum_items = options["maximum-items"] @@ -183,6 +191,8 @@ class ConfigFile: self.warning( f"Option date-detection not found; Will use default: {self.__date_detection}" ) + else: + self.warning(f"No options key found!") assert "paths" in self.__data, ( f"Could not find 'paths' key" @@ -193,6 +203,17 @@ class ConfigFile: assert isinstance(rotatable_paths, list), ( "Rotatable 'paths' key must be a string or list" ) + for i in range(len(rotatable_paths)): + p = rotatable_paths[i] + if isinstance(p, Path): + continue + elif isinstance(p, str): + rotatable_paths[i] = Path(p) + else: + raise AssertionError( + f"All rotatable paths must be strings or pathlib::Path objects" + ) + self.__rotatable_paths = rotatable_paths self.info(f"Found {len(self.__rotatable_paths)} rotatable paths")