[MERGE] from trunk
[odoo/odoo.git] / openerpcommand / call.py
1 """
2 Call an arbitrary model's method.
3 """
4 import ast
5 import os
6 import pprint
7 import sys
8 import time
9 import xmlrpclib
10
11 import client
12
13 class Call(client.Client):
14     """\
15     Call an arbitrary model's method.
16
17     Example:
18       > oe call res.users.read '[1, 3]' '[]' -u 1 -p admin
19     """
20     # TODO The above docstring is completely borked in the
21     # --help message.
22
23     command_name = 'call'
24
25     def __init__(self, subparsers=None):
26         super(Call, self).__init__(subparsers)
27         self.parser.add_argument('call', metavar='MODEL.METHOD',
28             help='the model and the method to call, using the '
29             '<model>.<method> format.')
30         self.parser.add_argument('args', metavar='ARGUMENT',
31             nargs='+',
32             help='the argument for the method call, must be '
33             '`ast.literal_eval` compatible. Can be repeated.')
34
35     def work(self):
36         try:
37             model, method = self.args.call.rsplit('.', 1)
38         except:
39             print "Invalid syntax `%s` must have the form <model>.<method>."
40             sys.exit(1)
41         args = tuple(map(ast.literal_eval, self.args.args)) if self.args.args else ()
42         x = self.execute(model, method, *args)
43         pprint.pprint(x, indent=4)
44