Revision fd5db045 kamaki/cli/command_tree.py
b/kamaki/cli/command_tree.py | ||
---|---|---|
5 | 5 |
# conditions are met: |
6 | 6 |
# |
7 | 7 |
# 1. Redistributions of source code must retain the above |
8 |
# copyright notice, this list of conditions and the following
|
|
9 |
# disclaimer.
|
|
8 |
# copyright notice, this list of conditions and the following
|
|
9 |
# disclaimer.
|
|
10 | 10 |
# |
11 | 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.
|
|
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 | 15 |
# |
16 | 16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
17 | 17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
... | ... | |
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
|
|
34 | 35 |
class Command(object): |
35 |
"""Store a command and the next-level commands as well - no deep tree here""" |
|
36 |
_name = None |
|
37 |
path = None |
|
38 |
cmd_class = None |
|
39 |
subcommands = {} |
|
40 |
help = ' ' |
|
41 |
|
|
42 |
def __init__(self, path, help = ' ', subcommands={}, cmd_class=None): |
|
43 |
self.path = path |
|
44 |
self.help = help |
|
45 |
self.subcommands =dict(subcommands) |
|
46 |
self.cmd_class = cmd_class |
|
47 |
|
|
48 |
@property |
|
49 |
def name(self): |
|
50 |
if self._name is None: |
|
51 |
self._name = self.path.split('_')[-1] |
|
52 |
return str(self._name) |
|
53 |
|
|
54 |
def add_subcmd(self, subcmd): |
|
55 |
if subcmd.path == self.path+'_'+subcmd.name: |
|
56 |
self.subcommands[subcmd.name] = subcmd |
|
57 |
return True |
|
58 |
return False |
|
59 |
def get_subcmd(self, name): |
|
60 |
try: |
|
61 |
return self.subcommands[name] |
|
62 |
except KeyError: |
|
63 |
return None |
|
64 |
|
|
65 |
def contains(self, name): |
|
66 |
"""Check if a name is a direct child of self""" |
|
67 |
return self.subcommands.has_key(name) |
|
68 |
|
|
69 |
@property |
|
70 |
def is_command(self): |
|
71 |
return self.cmd_class is not None |
|
72 |
@property |
|
73 |
def has_description(self): |
|
74 |
return len(self.help.strip()) > 0 |
|
75 |
@property |
|
76 |
def description(self): |
|
77 |
return self.help |
|
78 |
@property |
|
79 |
def parent_path(self): |
|
80 |
parentpath, sep, name = self.path.rpartition('_') |
|
81 |
return parentpath |
|
82 |
|
|
83 |
def set_class(self, cmd_class): |
|
84 |
self.cmd_class = cmd_class |
|
85 |
def get_class(self): |
|
86 |
return self.cmd_class |
|
87 |
|
|
88 |
def has_subname(self, subname): |
|
89 |
return self.subcommands.has_key(subname) |
|
90 |
def get_subnames(self): |
|
91 |
return self.subcommands.keys() |
|
92 |
def get_subcommands(self): |
|
93 |
return self.subcommands.values() |
|
94 |
def sublen(self): |
|
95 |
return len(self.subcommands) |
|
96 |
|
|
97 |
def parse_out(self, args): |
|
98 |
cmd = self |
|
99 |
index = 0 |
|
100 |
for term in args: |
|
101 |
try: |
|
102 |
cmd = cmd.subcommands[term] |
|
103 |
except KeyError: |
|
104 |
break |
|
105 |
index += 1 |
|
106 |
return cmd, args[index:] |
|
107 |
|
|
108 |
def pretty_print(self, recursive=False): |
|
109 |
print('Path: %s (Name: %s) is_cmd: %s\n\thelp: %s'%(self.path, self.name, |
|
110 |
self.is_command, self.help)) |
|
111 |
for cmd in self.get_subcommands(): |
|
112 |
cmd.pretty_print(recursive) |
|
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, self.name, self.is_command, self.help)) |
|
120 |
for cmd in self.get_subcommands(): |
|
121 |
cmd.pretty_print(recursive) |
|
122 |
|
|
113 | 123 |
|
114 | 124 |
class CommandTree(object): |
115 | 125 |
|
116 |
groups = {} |
|
117 |
_all_commands = {} |
|
118 |
name = None |
|
119 |
description = None |
|
120 |
|
|
121 |
def __init__(self, name, description=''): |
|
122 |
self.name = name |
|
123 |
self.description = description |
|
124 |
|
|
125 |
def add_command(self, command_path, description=None, cmd_class=None): |
|
126 |
terms = command_path.split('_') |
|
127 |
try: |
|
128 |
cmd = self.groups[terms[0]] |
|
129 |
except KeyError: |
|
130 |
cmd = Command(terms[0]) |
|
131 |
self.groups[terms[0]] = cmd |
|
132 |
self._all_commands[terms[0]] = cmd |
|
133 |
path = terms[0] |
|
134 |
for term in terms[1:]: |
|
135 |
path += '_'+term |
|
136 |
try: |
|
137 |
cmd = cmd.subcommands[term] |
|
138 |
except KeyError: |
|
139 |
new_cmd = Command(path) |
|
140 |
self._all_commands[path] = new_cmd |
|
141 |
cmd.add_subcmd(new_cmd) |
|
142 |
cmd = new_cmd |
|
143 |
if cmd_class is not None: |
|
144 |
cmd.set_class(cmd_class) |
|
145 |
if description is not None: |
|
146 |
cmd.help = description |
|
147 |
def get_command(self, path): |
|
148 |
return self._all_commands[path] |
|
149 |
def get_groups(self): |
|
150 |
return self.groups.values() |
|
151 |
def get_group_names(self): |
|
152 |
return self.groups.keys() |
|
153 |
|
|
154 |
def set_description(self, path, description): |
|
155 |
self._all_commands[path].help = description |
|
156 |
def get_descitpion(self, path): |
|
157 |
return self._all_commands[path].help |
|
158 |
def set_class(self, path, cmd_class): |
|
159 |
self._all_commands[path].set_class(cmd_class) |
|
160 |
def get_class(self, path): |
|
161 |
return self._all_commands[path].get_class() |
|
162 |
|
|
163 |
def get_subnames(self, path=None): |
|
164 |
return self.get_group_names() if path in (None, '') \ |
|
165 |
else self._all_commands[path].get_subnames() |
|
166 |
def get_subcommands(self, path=None): |
|
167 |
return self.get_groups() if path in (None, '') \ |
|
168 |
else self._all_commands[path].get_subcommands() |
|
169 |
def get_parent(self, path): |
|
170 |
if '_' not in path: |
|
171 |
return None |
|
172 |
terms = path.split('_') |
|
173 |
parent_path = '_'.join(terms[:-1]) |
|
174 |
return self._all_commands[parent_path] |
|
175 |
def get_closest_ancestor_command(self, path): |
|
176 |
path, sep, name = path.rpartition('_') |
|
177 |
while len(path) > 0: |
|
178 |
cmd = self._all_commands[path] |
|
179 |
if cmd.is_command: |
|
180 |
return cmd |
|
181 |
path, sep, name = path.rpartition('_') |
|
182 |
return None |
|
183 |
|
|
184 |
if '_' not in path: |
|
185 |
return None |
|
186 |
terms = terms[:-1] |
|
187 |
while len(terms) > 0: |
|
188 |
tmp_path = '_'.join(terms) |
|
189 |
cmd = self._all_commands[tmp_path] |
|
190 |
if cmd.is_command: |
|
191 |
return cmd |
|
192 |
terms = terms[:-1] |
|
193 |
raise KeyError('No ancestor commands') |
|
194 |
|
|
195 |
def pretty_print(self, group=None): |
|
196 |
if group is None: |
|
197 |
for group in self.groups: |
|
198 |
self.pretty_print(group) |
|
199 |
else: |
|
200 |
self.groups[group].pretty_print(recursive=True) |
|
126 |
groups = {} |
|
127 |
_all_commands = {} |
|
128 |
name = None |
|
129 |
description = None |
|
130 |
|
|
131 |
def __init__(self, name, description=''): |
|
132 |
self.name = name |
|
133 |
self.description = description |
|
134 |
|
|
135 |
def add_command(self, command_path, description=None, cmd_class=None): |
|
136 |
terms = command_path.split('_') |
|
137 |
try: |
|
138 |
cmd = self.groups[terms[0]] |
|
139 |
except KeyError: |
|
140 |
cmd = Command(terms[0]) |
|
141 |
self.groups[terms[0]] = cmd |
|
142 |
self._all_commands[terms[0]] = cmd |
|
143 |
path = terms[0] |
|
144 |
for term in terms[1:]: |
|
145 |
path += '_' + term |
|
146 |
try: |
|
147 |
cmd = cmd.subcommands[term] |
|
148 |
except KeyError: |
|
149 |
new_cmd = Command(path) |
|
150 |
self._all_commands[path] = new_cmd |
|
151 |
cmd.add_subcmd(new_cmd) |
|
152 |
cmd = new_cmd |
|
153 |
if cmd_class is not None: |
|
154 |
cmd.set_class(cmd_class) |
|
155 |
if description is not None: |
|
156 |
cmd.help = description |
|
157 |
|
|
158 |
def get_command(self, path): |
|
159 |
return self._all_commands[path] |
|
160 |
|
|
161 |
def get_groups(self): |
|
162 |
return self.groups.values() |
|
163 |
|
|
164 |
def get_group_names(self): |
|
165 |
return self.groups.keys() |
|
166 |
|
|
167 |
def set_description(self, path, description): |
|
168 |
self._all_commands[path].help = description |
|
169 |
|
|
170 |
def get_descitpion(self, path): |
|
171 |
return self._all_commands[path].help |
|
172 |
|
|
173 |
def set_class(self, path, cmd_class): |
|
174 |
self._all_commands[path].set_class(cmd_class) |
|
175 |
|
|
176 |
def get_class(self, path): |
|
177 |
return self._all_commands[path].get_class() |
|
178 |
|
|
179 |
def get_subnames(self, path=None): |
|
180 |
return self.get_group_names() if path in (None, '') \ |
|
181 |
else self._all_commands[path].get_subnames() |
|
182 |
|
|
183 |
def get_subcommands(self, path=None): |
|
184 |
return self.get_groups() if path in (None, '') \ |
|
185 |
else self._all_commands[path].get_subcommands() |
|
186 |
|
|
187 |
def get_parent(self, path): |
|
188 |
if '_' not in path: |
|
189 |
return None |
|
190 |
terms = path.split('_') |
|
191 |
parent_path = '_'.join(terms[:-1]) |
|
192 |
return self._all_commands[parent_path] |
|
193 |
|
|
194 |
def get_closest_ancestor_command(self, path): |
|
195 |
path, sep, name = path.rpartition('_') |
|
196 |
while len(path) > 0: |
|
197 |
cmd = self._all_commands[path] |
|
198 |
if cmd.is_command: |
|
199 |
return cmd |
|
200 |
path, sep, name = path.rpartition('_') |
|
201 |
return None |
|
202 |
|
|
203 |
if '_' not in path: |
|
204 |
return None |
|
205 |
terms = terms[:-1] |
|
206 |
while len(terms) > 0: |
|
207 |
tmp_path = '_'.join(terms) |
|
208 |
cmd = self._all_commands[tmp_path] |
|
209 |
if cmd.is_command: |
|
210 |
return cmd |
|
211 |
terms = terms[:-1] |
|
212 |
raise KeyError('No ancestor commands') |
|
213 |
|
|
214 |
def pretty_print(self, group=None): |
|
215 |
if group is None: |
|
216 |
for group in self.groups: |
|
217 |
self.pretty_print(group) |
|
218 |
else: |
|
219 |
self.groups[group].pretty_print(recursive=True) |
Also available in: Unified diff