-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
105 lines (83 loc) · 3.23 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""Dataclasses to hold and parse tool configuration"""
from dataclasses import dataclass, field, fields, MISSING
from typing import List, Optional, TypeVar
import yaml
T = TypeVar("T", bound="MergeableDataclass")
@dataclass
class MergeableDataclass:
def merge(self: T, other: T) -> T:
if type(self) is not type(other):
raise TypeError(f"Cannot merge dataclasses of different types: {type(self)} and {type(other)}")
for f in fields(other):
other_value = getattr(other, f.name)
if f.default is not MISSING and other_value != f.default:
setattr(self, f.name, other_value)
elif f.default_factory is not MISSING and callable(f.default_factory):
if other_value != f.default_factory():
setattr(self, f.name, other_value)
elif other_value is not None:
setattr(self, f.name, other_value)
return self
@dataclass
class NexusServer(MergeableDataclass):
host: Optional[str] = None
user: Optional[str] = None
password: Optional[str] = None
docker_host: Optional[str] = None
def fix_paths(self):
# Ensure `host` and `docker_host` never ends with a '/'
if self.host and self.host.endswith("/"):
self.host = self.host[:-1]
if self.docker_host and self.docker_host.endswith("/"):
self.docker_host = self.docker_host[:-1]
def __post_init__(self):
self.fix_paths()
@dataclass
class Action:
repo: str
active: bool = True
source: NexusServer = field(default_factory=NexusServer)
destination: NexusServer = field(default_factory=NexusServer)
target_repo: Optional[str] = None
repo_type: Optional[str] = None
description: Optional[str] = None
action: Optional[str] = None
path: Optional[str] = None
def fix_paths(self):
# Ensure `path` never ends with a '/'
if self.path and self.path.endswith("/"):
self.path = self.path[:-1]
def __post_init__(self):
self.fix_paths()
if not self.target_repo:
self.target_repo = self.repo
@dataclass
class NexusCopyConfig:
actions: List[Action]
source: NexusServer = field(default_factory=NexusServer)
destination: NexusServer = field(default_factory=NexusServer)
default_action: Optional[str] = "both"
local_path: str = "."
one: bool = None
force: bool = None
def fix_paths(self):
# Ensure `local_path` never ends with a '/'
if self.local_path and self.local_path.endswith("/"):
self.local_path = self.local_path[:-1]
def __post_init__(self):
self.fix_paths()
@classmethod
def from_yaml(cls, yaml_path: str) -> 'NexusCopyConfig':
with open(yaml_path, 'r') as f:
data = yaml.safe_load(f)
actions = [Action(**action) for action in data.get('actions', [])]
config = cls(
default_action=data.get('default_action'),
local_path=data.get('local_path'),
actions=actions,
)
if 'source' in data:
config.source = NexusServer(**data['source'])
if 'destination' in data:
config.destination = NexusServer(**data['destination'])
return config