Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.6 kB)

1 54069d1b Stavros Sachtouris
# Copyright 2012 GRNET S.A. All rights reserved.
2 54069d1b Stavros Sachtouris
#
3 54069d1b Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 54069d1b Stavros Sachtouris
# without modification, are permitted provided that the following
5 54069d1b Stavros Sachtouris
# conditions are met:
6 54069d1b Stavros Sachtouris
#
7 54069d1b Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 54069d1b Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 54069d1b Stavros Sachtouris
#      disclaimer.
10 54069d1b Stavros Sachtouris
#
11 54069d1b Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 54069d1b Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 54069d1b Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 54069d1b Stavros Sachtouris
#      provided with the distribution.
15 54069d1b Stavros Sachtouris
#
16 54069d1b Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 54069d1b Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 54069d1b Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 54069d1b Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 54069d1b Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 54069d1b Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 54069d1b Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 54069d1b Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 54069d1b Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 54069d1b Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 54069d1b Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 54069d1b Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 54069d1b Stavros Sachtouris
#
29 54069d1b Stavros Sachtouris
# The views and conclusions contained in the software and
30 54069d1b Stavros Sachtouris
# documentation are those of the authors and should not be
31 54069d1b Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 54069d1b Stavros Sachtouris
# or implied, of GRNET S.A.
33 54069d1b Stavros Sachtouris
34 54069d1b Stavros Sachtouris
from kamaki.cli.commands import _command_init
35 54069d1b Stavros Sachtouris
from kamaki.cli import command
36 54069d1b Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
37 54069d1b Stavros Sachtouris
from kamaki.cli.errors import CLIError
38 54069d1b Stavros Sachtouris
from kamaki.clients.commissioning import CanonifyException
39 54069d1b Stavros Sachtouris
40 54069d1b Stavros Sachtouris
41 54069d1b Stavros Sachtouris
class commissioning_cli(object):
42 54069d1b Stavros Sachtouris
43 54069d1b Stavros Sachtouris
    api_spec = None
44 54069d1b Stavros Sachtouris
    appname = None
45 54069d1b Stavros Sachtouris
    client = None
46 54069d1b Stavros Sachtouris
    add_context = False
47 54069d1b Stavros Sachtouris
    ctree = None
48 54069d1b Stavros Sachtouris
    description = None
49 54069d1b Stavros Sachtouris
50 54069d1b Stavros Sachtouris
    def __init__(self):
51 54069d1b Stavros Sachtouris
        self.api_spec = self.client.api_spec
52 54069d1b Stavros Sachtouris
        self.appname = self.client.appname
53 54069d1b Stavros Sachtouris
        self.ctree = CommandTree(self.appname, self.description)
54 54069d1b Stavros Sachtouris
55 54069d1b Stavros Sachtouris
    def generate_all(self):
56 54069d1b Stavros Sachtouris
        for f in self.api_spec.call_names():
57 54069d1b Stavros Sachtouris
            c = self.mkClass(f)
58 54069d1b Stavros Sachtouris
            command(self.ctree)(c)
59 54069d1b Stavros Sachtouris
60 54069d1b Stavros Sachtouris
    def mkClass(self, method):
61 54069d1b Stavros Sachtouris
        class C(_command_init):
62 54069d1b Stavros Sachtouris
63 54069d1b Stavros Sachtouris
            __doc__ = self.api_spec.get_doc(method)
64 54069d1b Stavros Sachtouris
65 54069d1b Stavros Sachtouris
            def init(this):
66 54069d1b Stavros Sachtouris
                this.token = (this.config.get(self.appname, 'token') or
67 54069d1b Stavros Sachtouris
                              this.config.get('global', 'token'))
68 54069d1b Stavros Sachtouris
                this.base_url = (this.config.get(self.appname, 'url') or
69 54069d1b Stavros Sachtouris
                                 this.config.get('global', 'url'))
70 54069d1b Stavros Sachtouris
                this.client = self.client(this.base_url, this.token)
71 54069d1b Stavros Sachtouris
72 54069d1b Stavros Sachtouris
            def call(this, method, args):
73 54069d1b Stavros Sachtouris
                ctx = '=null ' if self.add_context else ''
74 54069d1b Stavros Sachtouris
                arglist = '[' + ctx + ' '.join(args) + ']'
75 54069d1b Stavros Sachtouris
                argdict = self.api_spec.parse(method, arglist)
76 54069d1b Stavros Sachtouris
                f = getattr(this.client, method)
77 54069d1b Stavros Sachtouris
                return f(**argdict)
78 54069d1b Stavros Sachtouris
79 54069d1b Stavros Sachtouris
            def main(this, *args):
80 54069d1b Stavros Sachtouris
                this.init()
81 54069d1b Stavros Sachtouris
                try:
82 54069d1b Stavros Sachtouris
                    r = this.call(method, args)
83 54069d1b Stavros Sachtouris
                    print r
84 54069d1b Stavros Sachtouris
                except CanonifyException, e:
85 54069d1b Stavros Sachtouris
                    params = self.api_spec.show_input_canonical(method)
86 54069d1b Stavros Sachtouris
                    meth = method.replace('_', ' ')
87 54069d1b Stavros Sachtouris
                    m = '%s\n  usage: %s %s' % (e, meth, params)
88 54069d1b Stavros Sachtouris
                    raise CLIError('%s: %s\n' % (self.appname, m))
89 54069d1b Stavros Sachtouris
                except Exception as e:
90 54069d1b Stavros Sachtouris
                    raise CLIError('%s: %s\n' % (self.appname, e))
91 54069d1b Stavros Sachtouris
92 54069d1b Stavros Sachtouris
        C.__name__ = self.appname + '_' + method
93 54069d1b Stavros Sachtouris
        return C