Skip to content

Commit

Permalink
reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sorend committed Jun 23, 2024
1 parent 91337fe commit de7a47a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions sshconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@

class ConfigLine:
"""Holds configuration for a line in ssh config."""

def __init__(self, line, host=None, key=None, value=None):
self.line = line
self.host = host
Expand All @@ -119,6 +120,7 @@ def __init__(self, line, host=None, key=None, value=None):
def __repr__(self):
return "'%s' host=%s key=%s value=%s" % (self.line, self.host, self.key, self.value)


def read_ssh_config_file(path):
"""
Read ssh config file and return parsed SshConfigFile
Expand All @@ -127,16 +129,19 @@ def read_ssh_config_file(path):
lines = fh_.read().splitlines()
return SshConfigFile(lines)


def empty_ssh_config_file():
"""
Creates a new empty ssh configuration.
"""
return SshConfigFile([])


def _key_value(line):
upto_comment = line.split("#")[0]
return [x.strip() for x in re.split(r"\s+", upto_comment.strip(), 1)]


def _remap_key(key):
""" Change key into correct casing if we know the parameter """
if key in KNOWN_PARAMS:
Expand All @@ -145,13 +150,16 @@ def _remap_key(key):
return KNOWN_PARAMS[known_params.index(key.lower())]
return key


def _indent(s):
return s[0: len(s) - len(s.lstrip())]


class SshConfigFile(object):
"""
Class for manipulating SSH configuration.
"""

def __init__(self, lines):
self.lines_ = []
self.hosts_ = set()
Expand All @@ -178,7 +186,6 @@ def parse(self, lines):
popular = list(reversed(sorted(counter.items(), key=lambda e: e[1])))
self.indent = popular[0][0] if len(popular) > 0 else ' '


def hosts(self):
"""
Return the hosts found in the configuration.
Expand Down Expand Up @@ -208,7 +215,9 @@ def host(self, host):
for k, value in [(x.key.lower(), x.value) for x in self.lines_
if x.host == host and x.key.lower() != "host"]:
vals[k].append(value)
flatten = lambda x: x[0] if len(x) == 1 else x

def flatten(x):
return x[0] if len(x) == 1 else x
return {k: flatten(v) for k, v in vals.items()}
return {}

Expand Down Expand Up @@ -329,7 +338,7 @@ def remove(self, host):
raise ValueError("Host %s: not found." % host)
self.hosts_.remove(host)
# remove lines, including comments inside the host lines
host_lines = [ idx for idx, x in enumerate(self.lines_) if x.host == host ]
host_lines = [idx for idx, x in enumerate(self.lines_) if x.host == host]
remove_range = reversed(range(min(host_lines), max(host_lines) + 1))
for idx in remove_range:
del self.lines_[idx]
Expand Down Expand Up @@ -374,7 +383,7 @@ def read_ssh_config(master_path):
queue = [(master_path, master_config)]
while len(queue) > 0:
cur_path, cur_config = queue.pop()
cur_includes = [ x.value for x in cur_config.lines_ if x.key is not None and x.key.lower() == "include" ]
cur_includes = [x.value for x in cur_config.lines_ if x.key is not None and x.key.lower() == "include"]
configs.append((cur_path, cur_config))
for cur_include in cur_includes:
for new_path in _resolve_includes(base_path, cur_include):
Expand All @@ -383,8 +392,10 @@ def read_ssh_config(master_path):

return SshConfig(configs)


class SshConfig(object):
"""Class for manipulating set of ssh config files"""

def __init__(self, configs):
self.configs_ = configs

Expand Down Expand Up @@ -491,12 +502,11 @@ def remove(self, host):
return
raise ValueError("Host %s: not found" % host)


def config(self):
"""
Return the configuration as a string (without includes).
"""
return "\n".join([ c.config(True) for p, c in self.configs_ ])
return "\n".join([c.config(True) for p, c in self.configs_])

def write(self, path):
"""
Expand Down

0 comments on commit de7a47a

Please sign in to comment.