Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / command_tree.py @ 5eae854d

History | View | Annotate | Download (7.8 kB)

1
# Copyright 2011 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
import cmd
35
#from .errors import CLIUnknownCommand, CLICmdIncompleteError, CLICmdSpecError, CLIError
36

    
37
class Command(object):
38
        """Store a command and the next-level commands as well - no deep tree here"""
39
        _name = None
40
        path = None
41
        cmd_class = None
42
        subcommands = {}
43
        help = ' '
44

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

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

    
57
        def add_subcmd(self, subcmd):
58
                if subcmd.path == self.path+'_'+subcmd.name:
59
                        self.subcommands[subcmd.name] = subcmd
60
                        return True
61
                return False
62
        def get_subcmd(self, name):
63
                try:
64
                        return self.subcommands[name]
65
                except KeyError:
66
                        return None
67

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

    
72
        @property 
73
        def is_command(self):
74
                return self.cmd_class is not None
75
        @property 
76
        def has_description(self):
77
                return len(self.help.strip()) > 0
78
        @property 
79
        def description(self):
80
                return self.help
81

    
82
        def set_class(self, cmd_class):
83
                self.cmd_class = cmd_class
84
        def get_class(self):
85
                return self.cmd_class
86

    
87
        def has_subname(self, subname):
88
                return self.subcommands.has_key(subname)        
89
        def get_subnames(self):
90
                return self.subcommands.keys()
91
        def get_subcommands(self):
92
                return self.subcommands.values()
93
        def sublen(self):
94
                return len(self.subcommands)
95

    
96
        def parse_out(self, args):
97
                cmd = self
98
                index = 0
99
                for term in args:
100
                        try:
101
                                cmd = cmd.subcommands[term]
102
                        except KeyError:
103
                                break
104
                        index += 1
105
                return cmd, args[index:]
106

    
107
        def pretty_print(self, recursive=False):
108
                print('Path: %s (Name: %s) is_cmd: %s\n\thelp: %s'%(self.path, self.name,
109
                        self.is_command, self.help))
110
                for cmd in self.get_subcommands():
111
                        cmd.pretty_print(recursive)
112

    
113
def test_Command():
114
        cmd = Command('store', 'A store thingy')
115
        cmd.add_subcmd(Command('store_list'))
116
        tmp = cmd.get_subcmd('list')
117
        tmp.add_subcmd(Command('store_list_all', 'List everything'))
118
        tmp.add_subcmd(Command('store_list_one', 'List just one stuff'))
119
        cmd.pretty_print(True)
120

    
121
class CommandTree(object):
122

    
123
        groups = {}
124
        _all_commands = {}
125
        name = None
126
        description = None
127

    
128
        def __init__(self, name, description=''):
129
                self.name = name
130
                self.description = description
131

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

    
161
        def set_description(self, path, description):
162
                self._all_commands[path].help = description
163
        def get_descitpion(self, path):
164
                return self._all_commands[path].help
165
        def set_class(self, path, cmd_class):
166
                self._all_commands[path].set_class(cmd_class)
167
        def get_class(self, path):
168
                return self._all_commands[path].get_class()
169

    
170
        def get_subnames(self, path=None):
171
                return self.get_group_names() if path in (None, '') \
172
                else self._all_commands[path].get_subnames()
173
        def get_subcommands(self, path=None):
174
                return self.get_groups() if path in (None, '') \
175
                else self._all_commands[path].get_subcommands()
176
        def get_parent(self, path):
177
                if '_' not in path:
178
                        return None
179
                terms = path.split('_')
180
                parent_path = '_'.join(terms[:-1])
181
                return self._all_commands[parent_path]
182
        def get_closest_ancestor_command(self, path):
183
                path, sep, name = path.rpartition('_')
184
                while len(path) > 0:
185
                        cmd = self._all_commands[path]
186
                        if cmd.is_command:
187
                                return cmd
188
                        path, sep, name = path.rpartition('_')
189
                return None
190

    
191
                if '_' not in path:
192
                        return None
193
                terms = terms[:-1]
194
                while len(terms) > 0:
195
                        tmp_path = '_'.join(terms)
196
                        cmd = self._all_commands[tmp_path]
197
                        if cmd.is_command:
198
                                return cmd
199
                        terms = terms[:-1]
200
                raise KeyError('No ancestor commands')
201

    
202
        def pretty_print(self, group=None):
203
                if group is None:
204
                        for group in self.groups:
205
                                self.pretty_print(group)
206
                else:
207
                        self.groups[group].pretty_print(recursive=True)
208

    
209
def test_CommandTree():
210
        tree = CommandTree('kamaki', 'the kamaki tools')
211
        tree.add_command('store', 'A storage thingy')
212
        tree.add_command('server_list_lala', description='A testing server list', cmd_class=Shell)
213
        tree.add_command('store_list_all', 'List all things', cmd_class=Command)
214
        tree.add_command('store_list', 'List smthing pls', cmd_class=Shell)
215
        tree.add_command('server_list', description='A server list subgrp')
216
        tree.add_command('server', description='A server is a SERVER', cmd_class=CommandTree)
217
        tree.set_class('server', None)
218
        tree.set_description('server_list', '')
219
        if tree.get_class('server_list_lala') is Shell:
220
                print('server_list_lala is Shell')
221
        else:
222
                print('server_list_lala is not Shell')
223
        tree.pretty_print()
224
        print('store_list_all closest parent command is %s'%tree.get_closest_ancestor_command('store_list_all').path)
225
        tree.set_class('store', tree.get_command('store_list').get_class())
226
        tree.set_class('store_list', None)
227
        print('store_list_all closest parent command is %s'%tree.get_closest_ancestor_command('store_list_all').path)
228
        try:
229
                print('nonexisting_list_command closest parent is %s'%tree.get_closest_ancestor_command('nonexisting_list_command').path)
230
        except KeyError:
231
                print('Aparrently nonexisting_list_command is nonexisting ')
232

    
233
class Shell(cmd.Cmd):
234
        """Simple command processor example."""
235

    
236
        def do_greet(self, line):
237
                """Hello [cmd]
238
                        @line some line"""
239
                print "hello"
240

    
241
        def do_lala(self, lala):
242
                print('This is what I got: %s'%lala)
243
        def help_lala(self):
244
                print('This is SPAAARTAAAAAAA')
245

    
246
        def do_lalum(self, args):
247
                print('lalum')
248
        def complete_lalum(self, text, line, begidx, endidx):
249
                completions = ['lala']
250
                return completions
251

    
252
        def do_EOF(self, line):
253
                return True
254

    
255
if __name__ == '__main__':
256
        sh = Shell()
257
        sh.prompt = 'lala_$ '
258
        sh.cmdloop()
259

    
260
        #import sys
261
        #sh.onecmd(' '.join(sys.argv[1:]))
262
        #test_CommandTree()