Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commissioning.py @ 761e0cbf

History | View | Annotate | Download (3.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
from kamaki.cli.commands import _command_init
35
from kamaki.cli import command
36
from kamaki.cli.command_tree import CommandTree
37
from kamaki.cli.errors import CLIError
38
from kamaki.clients.commissioning import CanonifyException
39

    
40

    
41
class commissioning_cli(object):
42

    
43
    api_spec = None
44
    appname = None
45
    client = None
46
    add_context = False
47
    ctree = None
48
    description = None
49

    
50
    def __init__(self):
51
        self.api_spec = self.client.api_spec
52
        self.appname = self.client.appname
53
        self.ctree = CommandTree(self.appname, self.description)
54

    
55
    def generate_all(self):
56
        for f in self.api_spec.call_names():
57
            c = self.mkClass(f)
58
            command(self.ctree)(c)
59

    
60
    def mkClass(self, method):
61
        class C(_command_init):
62

    
63
            __doc__ = self.api_spec.get_doc(method)
64

    
65
            def init(this):
66
                this.token = (this.config.get(self.appname, 'token') or
67
                              this.config.get('global', 'token'))
68
                this.base_url = (this.config.get(self.appname, 'url') or
69
                                 this.config.get('global', 'url'))
70
                this.client = self.client(this.base_url, this.token)
71

    
72
            def call(this, method, args):
73
                ctx = '=null ' if self.add_context else ''
74
                arglist = '[' + ctx + ' '.join(args) + ']'
75
                argdict = self.api_spec.parse(method, arglist)
76
                f = getattr(this.client, method)
77
                return f(**argdict)
78

    
79
            def main(this, *args):
80
                this.init()
81
                try:
82
                    r = this.call(method, args)
83
                    print r
84
                except CanonifyException, e:
85
                    params = self.api_spec.show_input_canonical(method)
86
                    meth = method.replace('_', ' ')
87
                    m = '%s\n  usage: %s %s' % (e, meth, params)
88
                    raise CLIError('%s: %s\n' % (self.appname, m))
89
                except Exception as e:
90
                    raise CLIError('%s: %s\n' % (self.appname, e))
91

    
92
        C.__name__ = self.appname + '_' + method
93
        return C