Revision d252a7a8 kamaki/cli/command_tree/__init__.py

b/kamaki/cli/command_tree/__init__.py
101 101
        return cmd, args[index:]
102 102

  
103 103
    def pretty_print(self, recursive=False):
104
        print('Path: %s (Name: %s) is_cmd: %s\n\thelp: %s' % (
105
            self.path,
106
            self.name,
107
            self.is_command,
108
            self.help))
104
        print('%s\t\t(Name: %s is_cmd: %s help: %s)' % (
105
            self.path, self.name, self.is_command, self.help))
109 106
        for cmd in self.subcommands.values():
110 107
            cmd.pretty_print(recursive)
111 108

  
112 109

  
113 110
class CommandTree(object):
114 111

  
115
    groups = {}
116
    _all_commands = {}
117
    name = None
118
    description = None
119

  
120 112
    def __init__(self, name, description=''):
121 113
        self.name = name
122 114
        self.description = description
115
        self.groups = dict()
116
        self._all_commands = dict()
123 117

  
124 118
    def exclude(self, groups_to_exclude=[]):
125 119
        for group in groups_to_exclude:
......
143 137
                self._all_commands[path] = new_cmd
144 138
                cmd.add_subcmd(new_cmd)
145 139
                cmd = new_cmd
146
        if cmd_class:
147
            cmd.cmd_class = cmd_class
148
        if description is not None:
149
            cmd.help = description
140
        cmd.cmd_class = cmd_class or None
141
        cmd.help = description or None
150 142

  
151 143
    def find_best_match(self, terms):
152 144
        """Find a command that best matches a given list of terms
153 145

  
154
        :param terms: (list of str) match them against paths in cmd_tree
146
        :param terms: (list of str) match against paths in cmd_tree, e.g.
147
            ['aa', 'bb', 'cc'] matches aa_bb_cc
155 148

  
156
        :returns: (Command, list) the matching command, the remaining terms
149
        :returns: (Command, list) the matching command, the remaining terms or
150
            None
157 151
        """
158 152
        path = []
159 153
        for term in terms:
......
170 164
        tdesc = new_tree.description
171 165
        self.groups.update(new_tree.groups)
172 166
        self._all_commands.update(new_tree._all_commands)
173
        self.set_description(tname, tdesc)
167
        try:
168
            self._all_commands[tname].help = tdesc
169
        except KeyError:
170
            self.add_command(tname, tdesc)
174 171

  
175 172
    def has_command(self, path):
176 173
        return path in self._all_commands
......
178 175
    def get_command(self, path):
179 176
        return self._all_commands[path]
180 177

  
181
    def get_groups(self):
182
        return self.groups.values()
183

  
184
    def get_group_names(self):
185
        return self.groups.keys()
186

  
187
    def set_description(self, path, description):
188
        self._all_commands[path].help = description
189

  
190
    def get_description(self, path):
191
        return self._all_commands[path].help
192

  
193
    def get_subnames(self, path=None):
178
    def subnames(self, path=None):
194 179
        if path in (None, ''):
195
            return self.get_group_names()
180
            return self.groups.keys()
196 181
        return self._all_commands[path].subcommands.keys()
197 182

  
198 183
    def get_subcommands(self, path=None):
199 184
        return self._all_commands[path].subcommands.values() if (
200
            path) else self.get_groups()
201

  
202
    def get_parent(self, path):
203
        if '_' not in path:
204
            return None
205
        terms = path.split('_')
206
        parent_path = '_'.join(terms[:-1])
207
        return self._all_commands[parent_path]
208

  
209
    def get_closest_ancestor_command(self, path):
210
        path, sep, name = path.rpartition('_')
211
        while len(path) > 0:
212
            cmd = self._all_commands[path]
213
            if cmd.is_command:
214
                return cmd
215
            path, sep, name = path.rpartition('_')
216
        return None
217

  
218
        if '_' not in path:
219
            return None
220
        terms = path.split()[:-1]
221
        while len(terms) > 0:
222
            tmp_path = '_'.join(terms)
223
            cmd = self._all_commands[tmp_path]
224
            if cmd.is_command:
225
                return cmd
226
            terms = terms[:-1]
227
        raise KeyError('No ancestor commands')
185
            path) else self.groups.values()
228 186

  
229 187
    def pretty_print(self, group=None):
230 188
        if group is None:

Also available in: Unified diff