config, defaults: add configuration for backups

Add configuration options for creating backups during the
synchronisation.
This commit is contained in:
2026-01-05 17:17:41 +01:00
parent 78a4d9df36
commit f5e455fc79
2 changed files with 30 additions and 1 deletions

View File

@@ -49,6 +49,18 @@ class UnisonConfig:
bools: list bools: list
values: dict values: dict
@dataclass
class BackupConfig:
"""
Configuration options relative to backing up the files.
"""
enabled: bool
selection: str
location: str
max_backups: int
backupsuffix: str
backupprefix: str
@dataclass @dataclass
class OtherConfig: class OtherConfig:
""" """
@@ -64,6 +76,7 @@ class Config:
server: ServerConfig server: ServerConfig
roots: RootsConfig roots: RootsConfig
unison: UnisonConfig unison: UnisonConfig
backup: BackupConfig
other: OtherConfig other: OtherConfig
@@ -81,6 +94,7 @@ def load_config(config_path:str) -> Config:
# Check if sections are provided # Check if sections are provided
server_section = "Server" if "Server" in config.sections() else UNNAMED_SECTION server_section = "Server" if "Server" in config.sections() else UNNAMED_SECTION
roots_section = "Roots" if "Roots" in config.sections() else UNNAMED_SECTION roots_section = "Roots" if "Roots" in config.sections() else UNNAMED_SECTION
backup_section = "Backup"
other_section = "Other" if "Other" in config.sections() else UNNAMED_SECTION other_section = "Other" if "Other" in config.sections() else UNNAMED_SECTION
server_config = ServerConfig( server_config = ServerConfig(
@@ -94,6 +108,14 @@ def load_config(config_path:str) -> Config:
config.get(roots_section, "local", fallback=DEFAULT_ROOTS_LOCAL), config.get(roots_section, "local", fallback=DEFAULT_ROOTS_LOCAL),
config.get(roots_section, "remote") config.get(roots_section, "remote")
) )
backup_config = BackupConfig(
config.getboolean(backup_section, "enabled", fallback=DEFAULT_BACKUP_ENABLED),
config.get(backup_section, "selection", fallback=DEFAULT_BACKUP_SELECTION),
config.get(backup_section, "loction", fallback=DEFAULT_BACKUP_LOC),
config.getint(backup_section, "max_backups", fallback=DEFAULT_BACKUP_MAX_BACKUPS),
config.get(backup_section, "backupsuffix", fallback=DEFAULT_BACKUP_BACKUPSUFFIX),
config.get(backup_section, "backupprefix", fallback=DEFAULT_BACKUP_BACKUPPREFIX)
)
other_config = OtherConfig( other_config = OtherConfig(
Path(config.get(other_section, "cache_dir_path", fallback=DEFAULT_MISC_CACHE_DIR_PATH)).expanduser() Path(config.get(other_section, "cache_dir_path", fallback=DEFAULT_MISC_CACHE_DIR_PATH)).expanduser()
) )
@@ -110,4 +132,4 @@ def load_config(config_path:str) -> Config:
args_val[key] = val args_val[key] = val
unison_config = UnisonConfig(args_bool, args_val) unison_config = UnisonConfig(args_bool, args_val)
return Config(server_config, roots_config, unison_config, other_config) return Config(server_config, roots_config, unison_config, backup_config, other_config)

View File

@@ -16,3 +16,10 @@ DEFAULT_ROOTS_LOCAL: str = str(Path("~/files").expanduser())
# DEFAULT_ROOTS_REMOTE: str = "" # DEFAULT_ROOTS_REMOTE: str = ""
DEFAULT_MISC_CACHE_DIR_PATH: str = "~/.unisync" DEFAULT_MISC_CACHE_DIR_PATH: str = "~/.unisync"
DEFAULT_BACKUP_ENABLED: bool = False
DEFAULT_BACKUP_SELECTION: str = ""
DEFAULT_BACKUP_LOC: str = "local"
DEFAULT_BACKUP_MAX_BACKUPS: int = 2
DEFAULT_BACKUP_BACKUPSUFFIX: str = ".unison_backups/"
DEFAULT_BACKUP_BACKUPPREFIX: str = ".$VERSION.bak"