Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / command_tree.py @ de73876b

History | View | Annotate | Download (7.6 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34

    
35
class Command(object):
36
    """Store a command and the next-level (2 levels)"""
37
    _name = None
38
    path = None
39
    cmd_class = None
40
    subcommands = {}
41
    help = ' '
42

    
43
    def __init__(self, path, help=' ', subcommands={}, cmd_class=None):
44
        self.path = path
45
        self.help = help
46
        self.subcommands = dict(subcommands)
47
        self.cmd_class = cmd_class
48

    
49
    @property
50
    def name(self):
51
        if self._name is None:
52
            self._name = self.path.split('_')[-1]
53
        return str(self._name)
54

    
55
    def add_subcmd(self, subcmd):
56
        if subcmd.path == '%s_%s' % (self.path, subcmd.name):
57
            self.subcommands[subcmd.name] = subcmd
58
            return True
59
        return False
60

    
61
    def get_subcmd(self, name):
62
        try:
63
            return self.subcommands[name]
64
        except KeyError:
65
            return None
66

    
67
    def contains(self, name):
68
        """Check if a name is a direct child of self"""
69
        return name in self.subcommands
70

    
71
    @property
72
    def is_command(self):
73
        return self.cmd_class is not None
74

    
75
    @property
76
    def has_description(self):
77
        return len(self.help.strip()) > 0
78

    
79
    @property
80
    def description(self):
81
        return self.help
82

    
83
    @property
84
    def parent_path(self):
85
        parentpath, sep, name = self.path.rpartition('_')
86
        return parentpath
87

    
88
    def set_class(self, cmd_class):
89
        self.cmd_class = cmd_class
90

    
91
    def get_class(self):
92
        return self.cmd_class
93

    
94
    def has_subname(self, subname):
95
        return subname in self.subcommands
96

    
97
    def get_subnames(self):
98
        return self.subcommands.keys()
99

    
100
    def get_subcommands(self):
101
        return self.subcommands.values()
102

    
103
    def sublen(self):
104
        return len(self.subcommands)
105

    
106
    def parse_out(self, args):
107
        cmd = self
108
        index = 0
109
        for term in args:
110
            try:
111
                cmd = cmd.subcommands[term]
112
            except KeyError:
113
                break
114
            index += 1
115
        return cmd, args[index:]
116

    
117
    def pretty_print(self, recursive=False):
118
        print('Path: %s (Name: %s) is_cmd: %s\n\thelp: %s' % (
119
            self.path,
120
            self.name,
121
            self.is_command,
122
            self.help))
123
        for cmd in self.get_subcommands():
124
            cmd.pretty_print(recursive)
125

    
126

    
127
class CommandTree(object):
128

    
129
    groups = {}
130
    _all_commands = {}
131
    name = None
132
    description = None
133

    
134
    def __init__(self, name, description=''):
135
        self.name = name
136
        self.description = description
137

    
138
    def add_command(self, command_path, description=None, cmd_class=None):
139
        terms = command_path.split('_')
140
        try:
141
            cmd = self.groups[terms[0]]
142
        except KeyError:
143
            cmd = Command(terms[0])
144
            self.groups[terms[0]] = cmd
145
            self._all_commands[terms[0]] = cmd
146
        path = terms[0]
147
        for term in terms[1:]:
148
            path += '_' + term
149
            try:
150
                cmd = cmd.subcommands[term]
151
            except KeyError:
152
                new_cmd = Command(path)
153
                self._all_commands[path] = new_cmd
154
                cmd.add_subcmd(new_cmd)
155
                cmd = new_cmd
156
        if cmd_class is not None:
157
            cmd.set_class(cmd_class)
158
        if description is not None:
159
            cmd.help = description
160

    
161
    def find_best_match(self, terms):
162
        """Find a command that best matches a given list of terms
163

164
        :param terms: (list of str) match them against paths in cmd_tree
165

166
        :returns: (Command, list) the matching command, the remaining terms
167
        """
168
        path = []
169
        for term in terms:
170
            check_path = path + [term]
171
            if '_'.join(check_path) not in self._all_commands:
172
                break
173
            path = check_path
174
        if path:
175
            return (self._all_commands['_'.join(path)], terms[len(path):])
176
        return (None, terms)
177

    
178
    def add_tree(self, new_tree):
179
        tname = new_tree.name
180
        tdesc = new_tree.description
181
        self.groups.update(new_tree.groups)
182
        self._all_commands.update(new_tree._all_commands)
183
        self.set_description(tname, tdesc)
184

    
185
    def has_command(self, path):
186
        return path in self._all_commands
187

    
188
    def get_command(self, path):
189
        return self._all_commands[path]
190

    
191
    def get_groups(self):
192
        return self.groups.values()
193

    
194
    def get_group_names(self):
195
        return self.groups.keys()
196

    
197
    def set_description(self, path, description):
198
        self._all_commands[path].help = description
199

    
200
    def get_description(self, path):
201
        return self._all_commands[path].help
202

    
203
    def set_class(self, path, cmd_class):
204
        self._all_commands[path].set_class(cmd_class)
205

    
206
    def get_class(self, path):
207
        return self._all_commands[path].get_class()
208

    
209
    def get_subnames(self, path=None):
210
        if path in (None, ''):
211
            return self.get_group_names()
212
        return self._all_commands[path].get_subnames()
213

    
214
    def get_subcommands(self, path=None):
215
        if path in (None, ''):
216
            return self.get_groups()
217
        return self._all_commands[path].get_subcommands()
218

    
219
    def get_parent(self, path):
220
        if '_' not in path:
221
            return None
222
        terms = path.split('_')
223
        parent_path = '_'.join(terms[:-1])
224
        return self._all_commands[parent_path]
225

    
226
    def get_closest_ancestor_command(self, path):
227
        path, sep, name = path.rpartition('_')
228
        while len(path) > 0:
229
            cmd = self._all_commands[path]
230
            if cmd.is_command:
231
                return cmd
232
            path, sep, name = path.rpartition('_')
233
        return None
234

    
235
        if '_' not in path:
236
            return None
237
        terms = path.split()[:-1]
238
        while len(terms) > 0:
239
            tmp_path = '_'.join(terms)
240
            cmd = self._all_commands[tmp_path]
241
            if cmd.is_command:
242
                return cmd
243
            terms = terms[:-1]
244
        raise KeyError('No ancestor commands')
245

    
246
    def pretty_print(self, group=None):
247
        if group is None:
248
            for group in self.groups:
249
                self.pretty_print(group)
250
        else:
251
            self.groups[group].pretty_print(recursive=True)