|
|
|
@ -16,12 +16,23 @@ class HostGroup: |
|
|
|
self.children[name] = child |
|
|
|
return child |
|
|
|
|
|
|
|
def add_host(self, host): |
|
|
|
self.children[host.name] = host |
|
|
|
host.parent = self |
|
|
|
|
|
|
|
@property |
|
|
|
def fullpath(self): |
|
|
|
if self.parent is None: |
|
|
|
return "" |
|
|
|
return ".".join(self.parent.fullpath(), self.name) |
|
|
|
|
|
|
|
def hosts(self): |
|
|
|
for child in self.children.values(): |
|
|
|
if isinstance(child, HostGroup): |
|
|
|
yield from child.hosts() |
|
|
|
elif isinstance(child, Host): |
|
|
|
yield Host |
|
|
|
|
|
|
|
|
|
|
|
class ConfigFile: |
|
|
|
def __init__(self, global_host, hosts): |
|
|
|
@ -91,3 +102,24 @@ class GroupedConfigFile(ConfigFile): |
|
|
|
def __init__(self, global_host, hosts): |
|
|
|
self.global_host = global_host |
|
|
|
self.root_group = HostGroup("") |
|
|
|
host: Host |
|
|
|
for host in hosts: |
|
|
|
current = self.root_group |
|
|
|
parts = host.name.split(".") |
|
|
|
host.name = parts[-1] |
|
|
|
for path in parts[:-1]: |
|
|
|
current = current.add_child_group(path) |
|
|
|
current.add_host(host) |
|
|
|
|
|
|
|
def format(self, **format_data): |
|
|
|
hosts = sorted( |
|
|
|
self.root_group.hosts(), |
|
|
|
key=lambda host: host.name |
|
|
|
if isinstance(host.name, str) |
|
|
|
else " ".join(host.name), |
|
|
|
) |
|
|
|
if self.global_host is not None: |
|
|
|
hosts.insert(0, self.global_host) |
|
|
|
return format_data["host_seperator"].join( |
|
|
|
map(lambda host: host.format(**format_data), hosts) |
|
|
|
) |