Add group tree (nested groups)
This commit is contained in:
parent
0bbf06340a
commit
48d491312a
73
sshch/sshch
73
sshch/sshch
|
@ -20,7 +20,7 @@ import curses
|
||||||
from curses import textpad, panel
|
from curses import textpad, panel
|
||||||
|
|
||||||
# https://gitlab.com/zlax/sshch
|
# https://gitlab.com/zlax/sshch
|
||||||
version = "1.05"
|
version = "1.06"
|
||||||
# expand groups by default
|
# expand groups by default
|
||||||
expand_default = True
|
expand_default = True
|
||||||
# path to conf dir and file, default: ~/.config/sshch.conf
|
# path to conf dir and file, default: ~/.config/sshch.conf
|
||||||
|
@ -28,6 +28,15 @@ conf_dir = path.expanduser("~") + '/.config'
|
||||||
conf_file = conf_dir + '/' + 'sshch.conf'
|
conf_file = conf_dir + '/' + 'sshch.conf'
|
||||||
|
|
||||||
|
|
||||||
|
class GroupTree(object):
|
||||||
|
"""Group object with relatives information"""
|
||||||
|
def __init__(self, group):
|
||||||
|
self.group = group
|
||||||
|
self.aliases = []
|
||||||
|
self.children = []
|
||||||
|
self.parent = []
|
||||||
|
|
||||||
|
|
||||||
def AddNewAlias(alias):
|
def AddNewAlias(alias):
|
||||||
if not conf.has_section(alias):
|
if not conf.has_section(alias):
|
||||||
conf.add_section(alias)
|
conf.add_section(alias)
|
||||||
|
@ -219,9 +228,34 @@ def CMDList(option, opt, value, parser):
|
||||||
print(' '.join(str(p) for p in conf.sections()))
|
print(' '.join(str(p) for p in conf.sections()))
|
||||||
|
|
||||||
|
|
||||||
|
def GroupTreeRecursion(level, group, treelist, resultalias, resultstring, expandlist, previousgroups):
|
||||||
|
if group in previousgroups:
|
||||||
|
return resultalias, resultstring
|
||||||
|
previousgroups.append(group)
|
||||||
|
resultalias.append(group)
|
||||||
|
if group in expandlist:
|
||||||
|
resultstring.append(' '*(level-1)+">> "+group)
|
||||||
|
else:
|
||||||
|
resultstring.append(' '*(level-1)+"<> "+group)
|
||||||
|
if expandlist == True or group in expandlist:
|
||||||
|
for g in treelist[group].children:
|
||||||
|
resultalias, resultstring = GroupTreeRecursion(level+1, g, treelist, resultalias, resultstring, expandlist, previousgroups)
|
||||||
|
if expandlist == True or group in expandlist:
|
||||||
|
for ga in treelist[group].aliases:
|
||||||
|
if conf.has_option(ga, "exec_string"):
|
||||||
|
resultalias.append(ga)
|
||||||
|
result = "".join([' '*(level-1)+" ~ ", 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)
|
||||||
|
return resultalias, resultstring
|
||||||
|
|
||||||
|
|
||||||
def GetTreeList(strings=True, expandlist=True):
|
def GetTreeList(strings=True, expandlist=True):
|
||||||
aliases = []
|
aliases = []
|
||||||
groups = []
|
groups = []
|
||||||
|
treelist = {}
|
||||||
|
rootgroups = []
|
||||||
resultalias = []
|
resultalias = []
|
||||||
resultstring = []
|
resultstring = []
|
||||||
for a in conf.sections():
|
for a in conf.sections():
|
||||||
|
@ -229,7 +263,33 @@ def GetTreeList(strings=True, expandlist=True):
|
||||||
groups.append(a)
|
groups.append(a)
|
||||||
elif conf.has_option(a, "exec_string"):
|
elif conf.has_option(a, "exec_string"):
|
||||||
aliases.append(a)
|
aliases.append(a)
|
||||||
|
rootaliases = list(aliases)
|
||||||
for g in groups:
|
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)
|
||||||
|
for g in rootgroups:
|
||||||
|
resultalias, resultstring = GroupTreeRecursion(1, g, treelist, resultalias, resultstring, expandlist, [])
|
||||||
|
"""
|
||||||
|
|
||||||
|
# if not treelist[g].parent:
|
||||||
|
# resultalias.append(g)
|
||||||
|
|
||||||
resultalias.append(g)
|
resultalias.append(g)
|
||||||
resultstring.append(">> "+g)
|
resultstring.append(">> "+g)
|
||||||
group_aliases = conf.get(g, "group").split()
|
group_aliases = conf.get(g, "group").split()
|
||||||
|
@ -247,16 +307,17 @@ def GetTreeList(strings=True, expandlist=True):
|
||||||
aliases.remove(ga)
|
aliases.remove(ga)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
for a in aliases:
|
"""
|
||||||
|
for a in rootaliases:
|
||||||
resultalias.append(a)
|
resultalias.append(a)
|
||||||
result = "".join([str(a), " (", (conf.get(a, "exec_string") if
|
result = "".join([str(a), " (", (conf.get(a, "exec_string") if
|
||||||
conf.has_option(a, "exec_string") else ""), ")",
|
conf.has_option(a, "exec_string") else ""), ")",
|
||||||
(" [password]" if conf.has_option(a, "password") else "")])
|
(" [password]" if conf.has_option(a, "password") else "")])
|
||||||
resultstring.append(result)
|
resultstring.append(result)
|
||||||
if strings:
|
if strings:
|
||||||
return resultstring;
|
return resultstring
|
||||||
else:
|
else:
|
||||||
return resultalias;
|
return resultalias
|
||||||
|
|
||||||
|
|
||||||
def CMDFullList(option, opt, value, parser):
|
def CMDFullList(option, opt, value, parser):
|
||||||
|
@ -452,12 +513,12 @@ def CursesMain():
|
||||||
for a in conf.sections():
|
for a in conf.sections():
|
||||||
if conf.has_option(a, "group"):
|
if conf.has_option(a, "group"):
|
||||||
groups.append(a)
|
groups.append(a)
|
||||||
expanded = groups
|
expanded = list(groups)
|
||||||
strings = GetTreeList(False, expanded)
|
strings = GetTreeList(False, expanded)
|
||||||
stringsfull = GetTreeList(True, expanded)
|
stringsfull = GetTreeList(True, expanded)
|
||||||
elif expand_default == False:
|
elif expand_default == False:
|
||||||
groups = []
|
groups = []
|
||||||
expanded = groups
|
expanded = []
|
||||||
strings = GetTreeList(False, expanded)
|
strings = GetTreeList(False, expanded)
|
||||||
stringsfull = GetTreeList(True, expanded)
|
stringsfull = GetTreeList(True, expanded)
|
||||||
row_num = len(strings)
|
row_num = len(strings)
|
||||||
|
|
Loading…
Reference in New Issue