Add GroupChildAliases function

This commit is contained in:
ivan 2021-09-22 17:39:54 +03:00
parent 48d491312a
commit 10ca6ad15b
1 changed files with 63 additions and 63 deletions

View File

@ -20,7 +20,7 @@ import curses
from curses import textpad, panel
# https://gitlab.com/zlax/sshch
version = "1.06"
version = "1.07"
# expand groups by default
expand_default = True
# path to conf dir and file, default: ~/.config/sshch.conf
@ -208,13 +208,12 @@ def CMDConnect(aliases, command=False):
groups.append(a)
for alias in aliases:
if alias in groups:
group_aliases = conf.get(alias, "group").split()
group_aliases = GroupChildAliases(alias)
for ga in group_aliases:
if not ga in connectaliases:
connectaliases.append(ga)
else:
if not alias in connectaliases:
connectaliases.append(alias)
connectaliases = setSeq(connectaliases)
for alias in connectaliases:
if conf.has_section(alias):
print("Connecting to " + alias + "...")
@ -228,6 +227,59 @@ def CMDList(option, opt, value, parser):
print(' '.join(str(p) for p in conf.sections()))
def setSeq(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
def GroupChildRecursion(group, childaliases, treelist):
if treelist[group].children:
for a in treelist[group].children:
GroupChildRecursion(a, childaliases, treelist)
if treelist[group].aliases:
for a in treelist[group].aliases:
childaliases.append(a)
return childaliases
def GroupChildAliases(group):
aliases = []
groups = []
rootgroups = []
treelist = {}
for a in conf.sections():
if conf.has_option(a, "group"):
groups.append(a)
elif conf.has_option(a, "exec_string"):
aliases.append(a)
rootaliases = list(aliases)
for g in groups:
treelist[g] = GroupTree(g)
group_aliases = conf.get(g, "group").split()
for ga in group_aliases:
if ga in groups:
treelist[g].children.append(ga)
elif ga in aliases:
treelist[g].aliases.append(ga)
try:
rootaliases.remove(ga)
except ValueError:
pass
for g in groups:
if treelist[g].children:
for child in treelist[g].children:
treelist[child].parent.append(g)
for g in groups:
if not treelist[g].parent:
rootgroups.append(g)
if group == False:
return aliases, groups, rootaliases, rootgroups, treelist
else:
childaliases = GroupChildRecursion(group, [], treelist)
return setSeq(childaliases)
def GroupTreeRecursion(level, group, treelist, resultalias, resultstring, expandlist, previousgroups):
if group in previousgroups:
return resultalias, resultstring
@ -252,62 +304,11 @@ def GroupTreeRecursion(level, group, treelist, resultalias, resultstring, expand
def GetTreeList(strings=True, expandlist=True):
aliases = []
groups = []
treelist = {}
rootgroups = []
resultalias = []
resultstring = []
for a in conf.sections():
if conf.has_option(a, "group"):
groups.append(a)
elif conf.has_option(a, "exec_string"):
aliases.append(a)
rootaliases = list(aliases)
for g in groups:
treelist[g] = GroupTree(g)
group_aliases = conf.get(g, "group").split()
for ga in group_aliases:
if ga in groups:
treelist[g].children.append(ga)
elif ga in aliases:
treelist[g].aliases.append(ga)
try:
rootaliases.remove(ga)
except ValueError:
pass
for g in groups:
if treelist[g].children:
for child in treelist[g].children:
treelist[child].parent.append(g)
for g in groups:
if not treelist[g].parent:
rootgroups.append(g)
aliases, groups, rootaliases, rootgroups, treelist = GroupChildAliases(False)
for g in rootgroups:
resultalias, resultstring = GroupTreeRecursion(1, g, treelist, resultalias, resultstring, expandlist, [])
"""
# if not treelist[g].parent:
# resultalias.append(g)
resultalias.append(g)
resultstring.append(">> "+g)
group_aliases = conf.get(g, "group").split()
for ga in group_aliases:
if expandlist == False:
pass
elif expandlist == True or g in expandlist:
if conf.has_option(ga, "exec_string"):
resultalias.append(ga+" "+g)
result = "".join([" ", str(ga), " (", (conf.get(ga, "exec_string") if
conf.has_option(ga, "exec_string") else ""), ")",
(" [password]" if conf.has_option(ga, "password") else "")])
resultstring.append(result)
try:
aliases.remove(ga)
except ValueError:
pass
"""
for a in rootaliases:
resultalias.append(a)
result = "".join([str(a), " (", (conf.get(a, "exec_string") if
@ -334,13 +335,12 @@ def CursesConnect(screen, aliases, command=False):
groups.append(a)
for alias in aliases:
if alias in groups:
group_aliases = conf.get(alias, "group").split()
group_aliases = GroupChildAliases(alias)
for ga in group_aliases:
if not ga in connectaliases:
connectaliases.append(ga)
else:
if not alias in connectaliases:
connectaliases.append(alias)
connectaliases = setSeq(connectaliases)
for alias in connectaliases:
print("Connecting to " + alias + "...")
ConnectAlias(alias, command)