~zaber/openobject-client/main

« back to all changes in this revision

Viewing changes to scripts/client.py

  • Committer: Aki Mimoto
  • Date: 2012-02-16 01:22:13 UTC
  • Revision ID: aki+launchpad@zaber.com-20120216012213-dw18yqb67klxkczo
[IMP] Make command line migrations possible. Add support for commandline arguments

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import ConfigParser
3
3
import os
4
4
import sys
 
5
from optparse import OptionParser
 
6
import re
5
7
 
6
8
'''
7
9
Save a file like this in:
40
42
'''
41
43
 
42
44
class client:
43
 
    def __init__(self):
44
 
        """ Read configuration and open RPC connections """
45
 
        
46
 
        defaults = {'prompt': ''}
47
 
        config = ConfigParser.ConfigParser(defaults)
48
 
        config.read(os.path.expanduser('~/.zerp_config'))
49
 
        server_conn_key = "server " + config.get('server','connection')
50
 
        username = config.get(server_conn_key,'username') #the user
51
 
        self.pwd = config.get(server_conn_key,'password') #the password of the user
52
 
        self.dbname = config.get(server_conn_key,'database') #the database
53
 
        rpc_url = config.get(server_conn_key,'rpc_url')
 
45
    def __init__(self,
 
46
                connection=None,
 
47
                config=None,
 
48
                no_getopts=False,
 
49
                getopts=None,
 
50
                getopt_additional_options=None
 
51
 
 
52
            ):
 
53
        """ Read configuration and open RPC connections 
 
54
 
 
55
        Alternative configuration can be loaded with the "connection" argument
 
56
        eg. client(connection="live")
 
57
 
 
58
        Using a completely different config altogether can also be achieved by using 
 
59
        the "config" argument.
 
60
        eg. client(keyword={
 
61
                      username: user
 
62
                      password: pass
 
63
                      database: my-database-foo2
 
64
                      rpc_url: http://localhost:8069/xmlrpc
 
65
                    })
 
66
 
 
67
        Any scripts that are currently using this client can take advantage of the
 
68
        commandline options to choose between server profiles. 
 
69
 
 
70
        eg. script.py -h
 
71
 
 
72
        eg. script.py -c localhost
 
73
 
 
74
        Note that any parameter based calls will override any commandline options
 
75
 
 
76
        To add functionality to getopt such as information on what the script does, 
 
77
        instantiate the client class with parameters such as "getopts" and/or 
 
78
        "getopt_additional_options".
 
79
 
 
80
        eg. c = client(getopts={
 
81
                          'usage':"usage: %prog [options] DATABASE1 DATABASE2 ..."
 
82
                      })
 
83
 
 
84
        The above example will produce a message like the following if the script 
 
85
        is called with -h:
 
86
 
 
87
            Usage: migrate.py [options] DATABASE1 DATABASE2 ...
 
88
 
 
89
            Options:
 
90
              -h, --help            show this help message and exit
 
91
              -l, --list            List available connections
 
92
              -c CONNECTION, --connection=CONNECTION
 
93
                                    Which connection to use
 
94
 
 
95
        Using "getopt_additional_options" will rack additional options as described
 
96
        here: http://docs.python.org/library/optparse.html#tutorial
 
97
 
 
98
        Set "no_getopts" to true to avoid any invocation of the getopt subroutines.
 
99
 
 
100
        """
 
101
 
 
102
        if not no_getopts:
 
103
            self.parse_options(getopts,getopt_additional_options)
 
104
 
 
105
        if config == None:
 
106
            if connection == None and self.options.connection:
 
107
                connection = self.options.connection
 
108
            config = self.load_config(connection)
 
109
 
 
110
        username = config.get('username') #the user
 
111
        self.pwd = config.get('password') #the password of the user
 
112
        self.dbname = config.get('database') #the database
 
113
        rpc_url = config.get('rpc_url')
54
114
        self.rpc_url = rpc_url
55
115
 
56
116
        # Get the user_id
58
118
        self.user_id = sock_common.login(self.dbname, username, self.pwd)
59
119
        self.sock = xmlrpclib.ServerProxy(rpc_url+'/object')
60
120
 
61
 
        prompt = config.get(server_conn_key, 'prompt')
 
121
        prompt = config.get('prompt')
62
122
        if prompt:
63
123
            print prompt
64
124
            print 'Type Enter to continue or Ctrl-C to abort.'
65
125
            sys.stdin.readline()
66
126
 
 
127
    def parse_options(self,getopts=None,getopt_additional_options=None):
 
128
        """ Subroutine to parse the commandline for options such as
 
129
            the selection of which server profile to use.
 
130
        """
 
131
        if getopts == None: getopts = {}
 
132
        parser = OptionParser(**getopts)
 
133
 
 
134
        parser.add_option(
 
135
            "-l", "--list", action="store_true", dest="list_connections",
 
136
            help="List available connections")
 
137
 
 
138
        parser.add_option(
 
139
            "-c", "--connection", dest="connection",
 
140
            help="Which connection to use")
 
141
 
 
142
        if getopt_additional_options:
 
143
            for options in getopt_additional_options:
 
144
                parser.add_option(*options)
 
145
 
 
146
        (self.options, self.args) = parser.parse_args()
 
147
 
 
148
        if self.options.list_connections:
 
149
            config = ConfigParser.ConfigParser()
 
150
            config.read(os.path.expanduser('~/.zerp_config'))
 
151
            default = config.get('server','connection')
 
152
            print "Groups in current configuration:\n"
 
153
            for section_name in sorted(config.sections()):
 
154
                match = re.search( "^server (.+)", section_name )
 
155
                if not match: continue
 
156
                profile_name = match.group(1)
 
157
                print "  ", "*" if default == profile_name else "-",
 
158
                print match.group(1)
 
159
            print "\nprofile marked with '*' is the current default"
 
160
            print
 
161
            sys.exit()
 
162
 
 
163
    def load_config(self,connection=None):
 
164
        defaults = {'prompt': ''}
 
165
        config = ConfigParser.ConfigParser(defaults)
 
166
        config.read(os.path.expanduser('~/.zerp_config'))
 
167
        server_conn_key = "server " + ( 
 
168
                              connection or config.get('server','connection')
 
169
                          )
 
170
        config = dict(config.items(server_conn_key))
 
171
        return config
 
172
 
67
173
 
68
174
    def search(self, model, constraints, span_start=0, span_limit=100000, not_sure=0):
69
175
        """ Get a list of ids for records that match the constraints.
213
319
            model,
214
320
            signal,
215
321
            res_id)
 
322
 
 
323
 
 
324