Browse Source

Finished GroupedConfigFile but need to work host line with

extra names
neo
Raphael Roberts 6 years ago
parent
commit
4fd7251aba
  1. 32
      ssh_config_utils/config_file.py
  2. 11
      ssh_config_utils/host.py

32
ssh_config_utils/config_file.py

@ -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)
)

11
ssh_config_utils/host.py

@ -3,7 +3,8 @@ from ssh_config_utils.parser import CamelCase_to_snake, KEYWORDS_LOWER_TRANSLATE
class Host:
def __init__(self, name, options):
def __init__(self, name: str, options):
self.parent = None
self.options = options
self.name = name
@ -25,8 +26,14 @@ class Host:
def __str__(self):
return serialize_host(self.name, self.options, dict(indent=" " * 2))
def fullname(self):
if self.parent is None:
return self.name
else:
return self.parent.fullpath()
def format(self, **format_data):
return serialize_host(self.name, self.options, format_data)
return serialize_host(self.fullname(), self.options, format_data)
class GlobalHost(Host):

Loading…
Cancel
Save