1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """
19 common functionality for flumotion-admin-command
20 """
21
22 import sys
23
24
25
26 from flumotion.common import componentui, common, errors
27 from flumotion.admin import admin
28
29
30 from flumotion.monitor.nagios import util
31
32 __version__ = "$Rev: 6562 $"
33
34
35 ARGUMENTS_DESCRIPTION = """
36 Arguments to the method are passed using an argument list string, and the
37 arguments (matching the argument list string).
38
39 Example: "method ss one two" would invoke remote_method("one", "two")
40 """
41
42
43
44
47
48
49
50
61
62 def _doParseTypedArgs(spec, args):
63 accum = []
64 while spec:
65 argtype = spec.pop(0)
66 parsers = {'i': int, 's': str, 'b': common.strToBool,
67 'F': _readFile, 'N': (lambda _: None)}
68 if argtype == ')':
69 return tuple(accum)
70 elif argtype == '(':
71 accum.append(_doParseTypedArgs(spec, args))
72 elif argtype == '}':
73 return dict(accum)
74 elif argtype == '{':
75 accum.append(_doParseTypedArgs(spec, args))
76 elif argtype == ']':
77 return accum
78 elif argtype == '[':
79 accum.append(_doParseTypedArgs(spec, args))
80 elif argtype not in parsers:
81 raise ParseException('Unknown argument type: %r'
82 % argtype)
83 else:
84 parser = parsers[argtype]
85 try:
86 arg = args.pop(0)
87 except IndexError:
88 raise ParseException('Missing argument of type %r'
89 % parser)
90 try:
91 accum.append(parser(arg))
92 except Exception, e:
93 raise ParseException('Failed to parse %s as %r: %s'
94 % (arg, parser, e))
95
96 spec = list(spec) + [')']
97 args = list(args)
98
99 try:
100 res = _doParseTypedArgs(spec, args)
101 except ParseException, e:
102 print e.args[0]
103 return None
104
105 if args:
106 print 'Left over arguments:', args
107 return None
108 else:
109 return res
110
111
112
113
115
116 - def do(self, args):
119
121 self.debug('invoking doCallback with args %r', args)
122 return self.doCallback(args)
123
125 """
126 Subclasses should implement this as an alternative to the normal do
127 method. It will be called after a connection to the manager is made.
128
129 Don't forget to return a deferred you create to properly chain
130 execution.
131 """
132 raise NotImplementedError(
133 "subclass %r should implement doCallback" % self.__class__)
134
171
173 self.debug('Connected to manager.')
174 return adminMedium
175
182
183
185 """
186 Raised when the code wants the program to exit with a return value and
187 a message.
188 """
189
191 self.args = (code, msg)
192 self.code = code
193 self.msg = msg
194
195
197 raise Exited(1, "ERROR: " + msg)
198
199
201 sys.stderr.write("ERROR: " + msg + '\n')
202 return 1
203