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