def _get_verbose_flag(self) -> List[str]: if isinstance(self.verbose, bool): return Flag(constants.PARAMETER_VERBOSE, self.verbose, 'bool').get_flags() elif isinstance(self.verbose, int): return Flag(constants.PARAMETER_VERBOSE, self.verbose, 'int').get_flags() return []
def _set_command_flag(self, option: Flag, command: str): if not option: return # command specific flags if option.key == constants.PARAMETER_SOURCE: if isinstance(option.value, str) and option.value: self._source.append(option.value) elif isinstance(option.value, list) and option.value: self._source.extend(option.value) elif option.key == constants.PARAMETER_INITIALIZE: if isinstance(option.value, bool): self.initialize = option.value elif option.key == constants.PARAMETER_HOST: # specific to 'host': if the value is a boolean True, then we replace it with the hostname if isinstance(option.value, bool) and option.value: option.value = self._gethostname() option.type = 'str' elif option.key == constants.PARAMETER_CHECK_BEFORE: if isinstance(option.value, bool) and option.value: self.check_before = True elif option.key == constants.PARAMETER_CHECK_AFTER: if isinstance(option.value, bool) and option.value: self.check_after = True elif option.key == constants.PARAMETER_RUN_BEFORE: if option.value: self.run_before = option.value elif option.key == constants.PARAMETER_RUN_AFTER: if option.value: self.run_after = option.value elif option.key == constants.PARAMETER_STDIN: if option.value: self.stdin = option.value if command not in self._command_flags: self._command_flags[command] = {} if not self._is_special_flag(option.key): self._command_flags[command][option.key] = option
def get_retention_flags(self) -> List[str]: path_not_present = True flags = self.get_global_flags() for key, flag in self._retention_flags.items(): # create a restic argument for it arguments = flag.get_flags() if arguments: flags.extend(arguments) # keep track of the 'path' flag if key == constants.PARAMETER_PATH: path_not_present = False # to make sure we only deal with the current backup, we add the backup source as 'path' argument if path_not_present and self._source: path_flag = Flag(constants.PARAMETER_PATH, self._source, 'dir') flags.extend(path_flag.get_flags()) return flags
def _set_retention_flag(self, option: Flag): if not option: return if option.key == constants.PARAMETER_FORGET_BEFORE_BACKUP: if isinstance(option.value, bool): self.forget_before = option.value elif option.key == constants.PARAMETER_FORGET_AFTER_BACKUP: if isinstance(option.value, bool): self.forget_after = option.value elif option.key == constants.PARAMETER_HOST: # specific to 'host': if the value is a boolean True, then we replace it with the hostname if isinstance(option.value, bool) and option.value: option.value = self._gethostname() option.type = 'str' # adds it to the list of flags if not self._is_special_flag(option.key): self._retention_flags[option.key] = option
def _valid_flag(self, definition, key: str, value: Union[str, int, bool, list], expected_type: str) -> Flag: if constants.DEFINITION_FLAG in definition: # the restic flag has a different name than the configuration file flag key = definition[constants.DEFINITION_FLAG] if value: if expected_type == 'file': value = self._get_file_value(value) elif expected_type == 'dir': value = self._get_dir_value(value) return Flag(key, value, expected_type)
def _get_quiet_flag(self) -> List[str]: return Flag(constants.PARAMETER_QUIET, self.quiet, 'bool').get_flags()
def _get_repository_flag(self) -> List[str]: if self.repository: return Flag(constants.PARAMETER_REPO, self.repository, 'str').get_flags() return []
def test_can_get_dir_value(self): flag = Flag('name', "/some.dir", 'dir') argument = flag.get_flags() self.assertEqual(argument, ["--name \"/some.dir\""])
def test_can_get_string_value(self): flag = Flag('key', 'value', 'str') argument = flag.get_flags() self.assertEqual(argument, ["--key \"value\""])
def test_can_get_string_with_quote_value(self): flag = Flag('name', "o'irish", 'str') argument = flag.get_flags() self.assertEqual(argument, ["--name \"o'irish\""])
def test_can_get_file_value(self): flag = Flag('name', "some.file", 'file') argument = flag.get_flags() self.assertEqual(argument, ["--name \"some.file\""])
def test_can_get_string_values(self): flag = Flag('key', ["1", "2"], 'str') argument = flag.get_flags() self.assertEqual(argument, ["--key \"1\"", "--key \"2\""])
def test_can_get_negative_int_value(self): flag = Flag('key', -1, 'int') argument = flag.get_flags() self.assertEqual(argument, ["--key -1"])
def test_can_get_zero_int_value(self): flag = Flag('key', 0, 'int') argument = flag.get_flags() self.assertEqual(argument, ["--key 0"])
def test_can_get_false_boolean_value(self): flag = Flag('key', False, 'bool') argument = flag.get_flags() self.assertEqual(argument, [])
def test_can_get_true_boolean_value(self): flag = Flag('key', True, 'bool') argument = flag.get_flags() self.assertEqual(argument, ["--key"])