2 call func method invoker
4 Copyright 2007, Red Hat, Inc
7 This software may be freely redistributed under the terms of the GNU
8 general public license.
10 You should have received a copy of the GNU General Public License
11 along with this program; if not, write to the Free Software
12 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 from func
.overlord
import command
21 from func
.overlord
import client
26 class Call(client
.command
.Command
):
28 usage
= "call module method name arg1 arg2..."
30 self
.parser
.add_option("-v", "--verbose", dest
="verbose",
32 self
.parser
.add_option("-x", "--xmlrpc", dest
="xmlrpc",
33 help="output return data in XMLRPC format",
35 self
.parser
.add_option("", "--raw", dest
="rawprint",
36 help="output return data using Python print",
38 self
.parser
.add_option("-j", "--json", dest
="json",
39 help="output return data using JSON",
41 self
.parser
.add_option("-p", "--port", dest
="port",
43 self
.parser
.add_option("-f", "--forks", dest
="forks",
44 help="how many parallel processes? (default 1)",
45 default
=DEFAULT_FORKS
)
47 def handleOptions(self
, options
):
48 self
.options
= options
50 self
.verbose
= options
.verbose
51 self
.port
= options
.port
53 # I'm not really a fan of the "module methodname" approach
54 # but we'll keep it for now -akl
56 def parse(self
, argv
):
59 return command
.Command
.parse(self
, argv
)
62 def format_return(self
, data
):
64 The call module supports multiple output return types, the default is pprint.
67 if self
.options
.xmlrpc
:
68 return xmlrpclib
.dumps((data
,""))
73 return simplejson
.dumps(data
)
75 print "WARNING: json support not found, install python-simplejson"
78 if self
.options
.rawprint
:
81 return pprint
.pformat(data
)
85 # I'm not really a fan of the "module methodname" approach
86 # but we'll keep it for now -akl
88 # I kind of feel like we shouldn't be parsing args here, but I'm
89 # not sure what the write place is -al;
96 self
.method_args
= args
[2:]
100 # this could get weird, sub sub classes might be calling this
101 # this with multiple.parentCommand.parentCommands...
102 # maybe command.py needs a way to set attrs on subCommands?
103 # or some sort of shared datastruct?
104 self
.server_spec
= self
.parentCommand
.server_spec
106 client_obj
= client
.Client(self
.server_spec
,port
=self
.port
,interactive
=True,
107 verbose
=self
.verbose
, config
=self
.config
, nforks
=self
.options
.forks
)
108 results
= client_obj
.run(self
.module
, self
.method
, self
.method_args
)
110 # TO DO: add multiplexer support
111 # probably as a higher level module.
113 # dump the return code stuff atm till we figure out the right place for it
114 return self
.format_return(results
)