root / kamaki / cli / commands / __init__.py @ 2005b18e
History | View | Annotate | Download (3.7 kB)
1 | 5eae854d | Stavros Sachtouris | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 5eae854d | Stavros Sachtouris | #
|
3 | 5eae854d | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 5eae854d | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 5eae854d | Stavros Sachtouris | # conditions are met:
|
6 | 5eae854d | Stavros Sachtouris | #
|
7 | 5eae854d | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 5eae854d | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 5eae854d | Stavros Sachtouris | # disclaimer.
|
10 | 5eae854d | Stavros Sachtouris | #
|
11 | 5eae854d | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 5eae854d | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 5eae854d | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 5eae854d | Stavros Sachtouris | # provided with the distribution.
|
15 | 5eae854d | Stavros Sachtouris | #
|
16 | 5eae854d | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 5eae854d | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 5eae854d | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 5eae854d | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 5eae854d | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 5eae854d | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 5eae854d | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 5eae854d | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 5eae854d | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 5eae854d | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 5eae854d | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 5eae854d | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 5eae854d | Stavros Sachtouris | #
|
29 | 5eae854d | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 5eae854d | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 5eae854d | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 5eae854d | Stavros Sachtouris | # or implied, of GRNET S.A.command
|
33 | 5eae854d | Stavros Sachtouris | |
34 | 6069b53b | Stavros Sachtouris | import logging |
35 | 6069b53b | Stavros Sachtouris | |
36 | 6069b53b | Stavros Sachtouris | sendlog = logging.getLogger('clients.send')
|
37 | 6069b53b | Stavros Sachtouris | recvlog = logging.getLogger('clients.recv')
|
38 | 6069b53b | Stavros Sachtouris | |
39 | 234954d1 | Stavros Sachtouris | |
40 | 5eae854d | Stavros Sachtouris | class _command_init(object): |
41 | 36526b3c | Stavros Sachtouris | |
42 | 5eae854d | Stavros Sachtouris | def __init__(self, arguments={}): |
43 | e15d78e2 | Stavros Sachtouris | if hasattr(self, 'arguments'): |
44 | e15d78e2 | Stavros Sachtouris | arguments.update(self.arguments)
|
45 | e15d78e2 | Stavros Sachtouris | self.arguments = dict(arguments) |
46 | 5eae854d | Stavros Sachtouris | try:
|
47 | e15d78e2 | Stavros Sachtouris | self.config = self['config'] |
48 | 5a37a189 | Stavros Sachtouris | #self.config = self.get_argument('config')
|
49 | 5eae854d | Stavros Sachtouris | except KeyError: |
50 | 5eae854d | Stavros Sachtouris | pass
|
51 | 5eae854d | Stavros Sachtouris | |
52 | 5a673575 | Stavros Sachtouris | def _safe_progress_bar(self, msg, arg='progress_bar'): |
53 | 5a673575 | Stavros Sachtouris | """Try to get a progress bar, but do not raise errors"""
|
54 | 5a673575 | Stavros Sachtouris | try:
|
55 | 5a673575 | Stavros Sachtouris | progress_bar = self.arguments[arg]
|
56 | 5a673575 | Stavros Sachtouris | gen = progress_bar.get_generator(msg) |
57 | 5a673575 | Stavros Sachtouris | except Exception: |
58 | 5a673575 | Stavros Sachtouris | return (None, None) |
59 | 5a673575 | Stavros Sachtouris | return (progress_bar, gen)
|
60 | 5a673575 | Stavros Sachtouris | |
61 | 5a673575 | Stavros Sachtouris | def _safe_progress_bar_finish(self, progress_bar): |
62 | 5a673575 | Stavros Sachtouris | try:
|
63 | 5a673575 | Stavros Sachtouris | progress_bar.finish() |
64 | 5a673575 | Stavros Sachtouris | except Exception: |
65 | 5a673575 | Stavros Sachtouris | pass
|
66 | 5a673575 | Stavros Sachtouris | |
67 | 5a37a189 | Stavros Sachtouris | def __getitem__(self, argterm): |
68 | 5a37a189 | Stavros Sachtouris | """
|
69 | 5a37a189 | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
70 | 5a37a189 | Stavros Sachtouris |
|
71 | b113e74b | Stavros Sachtouris | :returns: the value of the corresponding Argument (not the argument
|
72 | b113e74b | Stavros Sachtouris | object)
|
73 | 5a37a189 | Stavros Sachtouris |
|
74 | 5a37a189 | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
75 | 5a37a189 | Stavros Sachtouris | """
|
76 | b113e74b | Stavros Sachtouris | return self.arguments[argterm].value |
77 | 5a37a189 | Stavros Sachtouris | |
78 | 5a37a189 | Stavros Sachtouris | def __setitem__(self, argterm, arg): |
79 | 5a37a189 | Stavros Sachtouris | """Install an argument as argterm
|
80 | 5a37a189 | Stavros Sachtouris | If argterm points to another argument, the other argument is lost
|
81 | 5a37a189 | Stavros Sachtouris |
|
82 | 5a37a189 | Stavros Sachtouris | :param argterm: (str)
|
83 | 5a37a189 | Stavros Sachtouris |
|
84 | 5a37a189 | Stavros Sachtouris | :param arg: (Argument)
|
85 | 5a37a189 | Stavros Sachtouris | """
|
86 | 5a37a189 | Stavros Sachtouris | if not hasattr(self, 'arguments'): |
87 | 5a37a189 | Stavros Sachtouris | self.arguments = {}
|
88 | 5a37a189 | Stavros Sachtouris | self.arguments[argterm] = arg
|
89 | 5a37a189 | Stavros Sachtouris | |
90 | b113e74b | Stavros Sachtouris | def get_argument_object(self, argterm): |
91 | b113e74b | Stavros Sachtouris | """
|
92 | b113e74b | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
93 | b113e74b | Stavros Sachtouris |
|
94 | b113e74b | Stavros Sachtouris | :returns: the arument object
|
95 | b113e74b | Stavros Sachtouris |
|
96 | b113e74b | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
97 | b113e74b | Stavros Sachtouris | """
|
98 | b113e74b | Stavros Sachtouris | return self.arguments[argterm] |
99 | b113e74b | Stavros Sachtouris | |
100 | 5eae854d | Stavros Sachtouris | def get_argument(self, argterm): |
101 | 5a37a189 | Stavros Sachtouris | """
|
102 | 5a37a189 | Stavros Sachtouris | :param argterm: (str) the name/label of an argument in self.arguments
|
103 | 5a37a189 | Stavros Sachtouris |
|
104 | 5a37a189 | Stavros Sachtouris | :returns: the value of the arument object
|
105 | 5a37a189 | Stavros Sachtouris |
|
106 | 5a37a189 | Stavros Sachtouris | :raises KeyError: if argterm not in self.arguments of this object
|
107 | 5a37a189 | Stavros Sachtouris | """
|
108 | e15d78e2 | Stavros Sachtouris | return self[argterm] |