44
""" Read configuration and open RPC connections """
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')
50
getopt_additional_options=None
53
""" Read configuration and open RPC connections
55
Alternative configuration can be loaded with the "connection" argument
56
eg. client(connection="live")
58
Using a completely different config altogether can also be achieved by using
59
the "config" argument.
63
database: my-database-foo2
64
rpc_url: http://localhost:8069/xmlrpc
67
Any scripts that are currently using this client can take advantage of the
68
commandline options to choose between server profiles.
72
eg. script.py -c localhost
74
Note that any parameter based calls will override any commandline options
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".
80
eg. c = client(getopts={
81
'usage':"usage: %prog [options] DATABASE1 DATABASE2 ..."
84
The above example will produce a message like the following if the script
87
Usage: migrate.py [options] DATABASE1 DATABASE2 ...
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
95
Using "getopt_additional_options" will rack additional options as described
96
here: http://docs.python.org/library/optparse.html#tutorial
98
Set "no_getopts" to true to avoid any invocation of the getopt subroutines.
103
self.parse_options(getopts,getopt_additional_options)
106
if connection == None and self.options.connection:
107
connection = self.options.connection
108
config = self.load_config(connection)
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
58
118
self.user_id = sock_common.login(self.dbname, username, self.pwd)
59
119
self.sock = xmlrpclib.ServerProxy(rpc_url+'/object')
61
prompt = config.get(server_conn_key, 'prompt')
121
prompt = config.get('prompt')
64
124
print 'Type Enter to continue or Ctrl-C to abort.'
65
125
sys.stdin.readline()
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.
131
if getopts == None: getopts = {}
132
parser = OptionParser(**getopts)
135
"-l", "--list", action="store_true", dest="list_connections",
136
help="List available connections")
139
"-c", "--connection", dest="connection",
140
help="Which connection to use")
142
if getopt_additional_options:
143
for options in getopt_additional_options:
144
parser.add_option(*options)
146
(self.options, self.args) = parser.parse_args()
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 "-",
159
print "\nprofile marked with '*' is the current default"
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')
170
config = dict(config.items(server_conn_key))
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.