Adds unison config and test code
This adds the possiblity to pass configuration options directly to unison via the configuration file. Also adds some test code to main.py
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
# Copyright (C) 2025 Paul Retourné
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from configparser import UNNAMED_SECTION
|
||||
from dataclasses import dataclass, field
|
||||
import ipaddress
|
||||
import configparser
|
||||
from configparser import UNNAMED_SECTION
|
||||
|
||||
@dataclass
|
||||
class ServerConfig:
|
||||
@@ -29,10 +29,16 @@ class RootsConfig:
|
||||
local: str
|
||||
remote: str
|
||||
|
||||
@dataclass
|
||||
class UnisonConfig:
|
||||
bools: list = field(default_factory=list)
|
||||
values: dict = field(default_factory=dict)
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
server: ServerConfig
|
||||
roots: RootsConfig
|
||||
unison: UnisonConfig
|
||||
|
||||
def load_config(config_path:str) -> Config:
|
||||
"""
|
||||
@@ -42,7 +48,7 @@ def load_config(config_path:str) -> Config:
|
||||
Returns:
|
||||
Config: A populated Config object containing the loaded config.
|
||||
"""
|
||||
config = configparser.ConfigParser(allow_unnamed_section=True)
|
||||
config = configparser.ConfigParser(allow_unnamed_section=True, allow_no_value=True)
|
||||
config.read(config_path)
|
||||
|
||||
# Check if sections are provided
|
||||
@@ -60,4 +66,17 @@ def load_config(config_path:str) -> Config:
|
||||
config.get(roots_section, "local"),
|
||||
config.get(roots_section, "remote")
|
||||
)
|
||||
return Config(server_config, roots_config)
|
||||
|
||||
args_bool = list()
|
||||
args_val = dict()
|
||||
if "Unison" in config.sections():
|
||||
for key, val in config.items("Unison"):
|
||||
if key in config["DEFAULT"].keys():
|
||||
continue
|
||||
elif val == "" or val == None:
|
||||
args_bool.append(key)
|
||||
else:
|
||||
args_val[key] = val
|
||||
unison_config = UnisonConfig(args_bool, args_val)
|
||||
|
||||
return Config(server_config, roots_config, unison_config)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import os
|
||||
from argparser import create_argparser
|
||||
from config import RootsConfig, ServerConfig, Config, load_config
|
||||
from synchroniser import Synchroniser
|
||||
|
||||
def main():
|
||||
parser = create_argparser()
|
||||
@@ -18,5 +19,26 @@ def main():
|
||||
# TODO make the command line arguments work and override the config options
|
||||
pass
|
||||
|
||||
synchroniser = Synchroniser(
|
||||
config.roots.remote,
|
||||
config.roots.local,
|
||||
config.server.user,
|
||||
config.server.ip if config.server.ip != "" else config.server.hostname,
|
||||
config.server.port,
|
||||
config.unison.bools,
|
||||
config.unison.values
|
||||
)
|
||||
|
||||
if synchroniser.create_ssh_master_connection() != 0:
|
||||
print("Connection failed quitting")
|
||||
return 1
|
||||
print("Connected to the remote.")
|
||||
|
||||
synchroniser.sync_files(["salut"])
|
||||
|
||||
synchroniser.close_ssh_master_connection()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user