From 48d491312ac8bf29190a356bdb291ca0149ae31b Mon Sep 17 00:00:00 2001 From: zlaxy Date: Wed, 22 Sep 2021 16:31:11 +0300 Subject: [PATCH] Add group tree (nested groups) --- sshch/sshch | 73 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/sshch/sshch b/sshch/sshch index 1992a49..3d446e9 100755 --- a/sshch/sshch +++ b/sshch/sshch @@ -20,7 +20,7 @@ import curses from curses import textpad, panel # https://gitlab.com/zlax/sshch -version = "1.05" +version = "1.06" # expand groups by default expand_default = True # 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' +class GroupTree(object): + """Group object with relatives information""" + def __init__(self, group): + self.group = group + self.aliases = [] + self.children = [] + self.parent = [] + + def AddNewAlias(alias): if not conf.has_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())) +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): aliases = [] groups = [] + treelist = {} + rootgroups = [] resultalias = [] resultstring = [] for a in conf.sections(): @@ -229,7 +263,33 @@ def GetTreeList(strings=True, expandlist=True): 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) + 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() @@ -247,16 +307,17 @@ def GetTreeList(strings=True, expandlist=True): aliases.remove(ga) except ValueError: pass - for a in aliases: + """ + for a in rootaliases: resultalias.append(a) result = "".join([str(a), " (", (conf.get(a, "exec_string") if conf.has_option(a, "exec_string") else ""), ")", (" [password]" if conf.has_option(a, "password") else "")]) resultstring.append(result) if strings: - return resultstring; + return resultstring else: - return resultalias; + return resultalias def CMDFullList(option, opt, value, parser): @@ -452,12 +513,12 @@ def CursesMain(): for a in conf.sections(): if conf.has_option(a, "group"): groups.append(a) - expanded = groups + expanded = list(groups) strings = GetTreeList(False, expanded) stringsfull = GetTreeList(True, expanded) elif expand_default == False: groups = [] - expanded = groups + expanded = [] strings = GetTreeList(False, expanded) stringsfull = GetTreeList(True, expanded) row_num = len(strings)