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

34

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

    def __init__(self, path, help=' ', subcommands={}, cmd_class=None):
44
        assert path, 'Cannot initialize a command without a command path'
45
        self.path = path
46
47
48
        self.help = help or ''
        self.subcommands = dict(subcommands) if subcommands else {}
        self.cmd_class = cmd_class or None
49
50
51

    @property
    def name(self):
52
        if not self._name:
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
            self._name = self.path.split('_')[-1]
        return str(self._name)

    def add_subcmd(self, subcmd):
        if subcmd.path == '%s_%s' % (self.path, subcmd.name):
            self.subcommands[subcmd.name] = subcmd
            return True
        return False

    def get_subcmd(self, name):
        try:
            return self.subcommands[name]
        except KeyError:
            return None

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

    @property
    def is_command(self):
74
        return len(self.subcommands) == 0 if self.cmd_class else False
75
76
77

    @property
    def parent_path(self):
78
        try:
79
            return self.path[:self.path.rindex('_')]
80
81
        except ValueError:
            return ''
82
83

    def parse_out(self, args):
84
85
86
87
88
89
90
91
92
        """Find the deepest subcommand matching a series of terms
        but stop the first time a term doesn't match

        :param args: (list) terms to match commands against

        :returns: (parsed out command, the rest of the arguments)

        :raises TypeError: if args is not inalterable
        """
93
94
95
96
97
98
99
100
101
102
103
        cmd = self
        index = 0
        for term in args:
            try:
                cmd = cmd.subcommands[term]
            except KeyError:
                break
            index += 1
        return cmd, args[index:]

    def pretty_print(self, recursive=False):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
104
105
        print('%s\t\t(Name: %s is_cmd: %s help: %s)' % (
            self.path, self.name, self.is_command, self.help))
106
        for cmd in self.subcommands.values():
107
108
            cmd.pretty_print(recursive)

109

110
111
class CommandTree(object):

112
113
114
    def __init__(self, name, description=''):
        self.name = name
        self.description = description
Stavros Sachtouris's avatar
Stavros Sachtouris committed
115
116
        self.groups = dict()
        self._all_commands = dict()
117

118
119
120
121
    def exclude(self, groups_to_exclude=[]):
        for group in groups_to_exclude:
            self.groups.pop(group, None)

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    def add_command(self, command_path, description=None, cmd_class=None):
        terms = command_path.split('_')
        try:
            cmd = self.groups[terms[0]]
        except KeyError:
            cmd = Command(terms[0])
            self.groups[terms[0]] = cmd
            self._all_commands[terms[0]] = cmd
        path = terms[0]
        for term in terms[1:]:
            path += '_' + term
            try:
                cmd = cmd.subcommands[term]
            except KeyError:
                new_cmd = Command(path)
                self._all_commands[path] = new_cmd
                cmd.add_subcmd(new_cmd)
                cmd = new_cmd
Stavros Sachtouris's avatar
Stavros Sachtouris committed
140
141
        cmd.cmd_class = cmd_class or None
        cmd.help = description or None
142

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

Stavros Sachtouris's avatar
Stavros Sachtouris committed
146
147
        :param terms: (list of str) match against paths in cmd_tree, e.g.
            ['aa', 'bb', 'cc'] matches aa_bb_cc
148

Stavros Sachtouris's avatar
Stavros Sachtouris committed
149
150
        :returns: (Command, list) the matching command, the remaining terms or
            None
151
152
153
154
155
156
157
158
159
160
161
        """
        path = []
        for term in terms:
            check_path = path + [term]
            if '_'.join(check_path) not in self._all_commands:
                break
            path = check_path
        if path:
            return (self._all_commands['_'.join(path)], terms[len(path):])
        return (None, terms)

162
163
164
165
166
    def add_tree(self, new_tree):
        tname = new_tree.name
        tdesc = new_tree.description
        self.groups.update(new_tree.groups)
        self._all_commands.update(new_tree._all_commands)
Stavros Sachtouris's avatar
Stavros Sachtouris committed
167
168
169
170
        try:
            self._all_commands[tname].help = tdesc
        except KeyError:
            self.add_command(tname, tdesc)
171

172
173
174
    def has_command(self, path):
        return path in self._all_commands

175
176
177
    def get_command(self, path):
        return self._all_commands[path]

Stavros Sachtouris's avatar
Stavros Sachtouris committed
178
    def subnames(self, path=None):
179
        if path in (None, ''):
Stavros Sachtouris's avatar
Stavros Sachtouris committed
180
            return self.groups.keys()
181
        return self._all_commands[path].subcommands.keys()
182
183

    def get_subcommands(self, path=None):
184
        return self._all_commands[path].subcommands.values() if (
Stavros Sachtouris's avatar
Stavros Sachtouris committed
185
            path) else self.groups.values()
186
187
188
189
190
191
192

    def pretty_print(self, group=None):
        if group is None:
            for group in self.groups:
                self.pretty_print(group)
        else:
            self.groups[group].pretty_print(recursive=True)