make validators idempotent

This commit is contained in:
Thomas Waldmann 2026-02-24 14:57:36 +01:00
parent 2e7ce45904
commit ef0eca0469
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
3 changed files with 16 additions and 0 deletions

View file

@ -627,6 +627,9 @@ class Compressor:
class CompressionSpec:
def __init__(self, s):
if isinstance(s, CompressionSpec):
self.__dict__.update(s.__dict__)
return
values = s.split(',')
count = len(values)
if count < 1:

View file

@ -130,6 +130,8 @@ def positive_int_validator(value):
def interval(s):
"""Convert a string representing a valid interval to a number of seconds."""
if isinstance(s, int):
return s
seconds_in_a_minute = 60
seconds_in_an_hour = 60 * seconds_in_a_minute
seconds_in_a_day = 24 * seconds_in_an_hour
@ -164,6 +166,8 @@ def interval(s):
def ChunkerParams(s):
if isinstance(s, tuple):
return s
params = s.strip().split(",")
count = len(params)
if count == 0:
@ -228,6 +232,8 @@ def ChunkerParams(s):
def FilesCacheMode(s):
ENTRIES_MAP = dict(ctime="c", mtime="m", size="s", inode="i", rechunk="r", disabled="d")
VALID_MODES = ("cis", "ims", "cs", "ms", "cr", "mr", "d", "s") # letters in alpha order
if s in VALID_MODES:
return s
entries = set(s.strip().split(","))
if not entries <= set(ENTRIES_MAP):
raise argparse.ArgumentTypeError(
@ -369,6 +375,8 @@ class FileSize(int):
def parse_file_size(s):
"""Return int from file size (1234, 55G, 1.7T)."""
if isinstance(s, int):
return s
if not s:
return int(s) # will raise
s = s.upper()
@ -507,6 +515,9 @@ class Location:
local_re = re.compile(local_path_re, re.VERBOSE)
def __init__(self, text="", overrides={}, other=False):
if isinstance(text, Location):
self.__dict__.update(text.__dict__)
return
self.repo_env_var = "BORG_OTHER_REPO" if other else "BORG_REPO"
self.valid = False
self.proto = None

View file

@ -37,6 +37,8 @@ def utcfromtimestampns(ts_ns: int) -> datetime:
def timestamp(s):
"""Convert a --timestamp=s argument to a datetime object."""
if isinstance(s, datetime):
return s
try:
# is it pointing to a file / directory?
ts_ns = safe_ns(os.stat(s).st_mtime_ns)