Skip to content

Commit e130428

Browse files
author
James William Pye
committed
Integrate clientoptparse with clientparameters.
While clientparameters doesn't need clientoptparse, clientoptparse surely has a lot of clientparameter specific aspects as it outputs the parameters in a "denormalized" fashion. Other than organization, I don't seen any point for this being a separate module.
1 parent f5281f8 commit e130428

4 files changed

Lines changed: 167 additions & 192 deletions

File tree

postgresql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
=============
88
"""
99
__author__ = "James William Pye <x@jwp.name>"
10-
__version__ = "0.8dev"
1110
__date__ = "2009-01-19 21:15:00-07"
1211

12+
__version__ = "0.8dev"
1313
version_info = (0, 8, 0, 'dev', 0)
1414

1515
#'PGGSSLIB' : 'gsslib',

postgresql/clientoptparse.py

Lines changed: 0 additions & 173 deletions
This file was deleted.

postgresql/clientparameters.py

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import sys
99
import os
1010
import configparser
11+
import optparse
1112
from itertools import chain
13+
from functools import partial
14+
1215

1316
from . import iri as pg_iri
1417
from . import dsn as pg_dsn
@@ -62,7 +65,7 @@ def getuser():
6265
# Extensions
6366
'ROLE' : 'role', # SET ROLE $PGROLE
6467

65-
# This keyword will never make it to a connect() function
68+
# This keyword *should* never make it to a connect() function
6669
# as `resolve_password` should be called to fill in the
6770
# parameter accordingly.
6871
'PASSFILE' : 'pgpassfile',
@@ -150,6 +153,158 @@ def envvars(environ = os.environ, modifier : "environment variable key modifier"
150153
if service in environ:
151154
yield ('pg_service', environ[service])
152155

156+
##
157+
# optparse options
158+
##
159+
160+
option_datadir = optparse.make_option('-D', '--datadir',
161+
help = 'location of the database storage area',
162+
default = None,
163+
dest = 'datadir',
164+
)
165+
166+
option_in_xact = optparse.make_option('-1', '--with-transaction',
167+
dest = 'in_xact',
168+
action = 'store_true',
169+
help = 'run operation with a transaction block',
170+
)
171+
172+
def append_db_client_parameters(option, opt_str, value, parser):
173+
# for options without arguments, None is passed in.
174+
value = True if value is None else value
175+
parser.values.db_client_parameters.append(
176+
((option.dest,), value)
177+
)
178+
179+
make_option = partial(optparse.make_option,
180+
action = 'callback',
181+
callback = append_db_client_parameters
182+
)
183+
184+
option_user = make_option('-U', '--username',
185+
dest = 'user',
186+
type = 'str',
187+
help = 'user name to connect as',
188+
)
189+
option_database = make_option('-d', '--database',
190+
type = 'str',
191+
help = "database's name",
192+
dest = 'database',
193+
)
194+
option_password = make_option('-W', '--password',
195+
dest = 'prompt_password',
196+
help = 'prompt for password',
197+
)
198+
option_host = make_option('-h', '--host',
199+
help = 'database server host',
200+
type = 'str',
201+
dest = 'host',
202+
)
203+
option_port = make_option('-p', '--port',
204+
help = 'database server port',
205+
type = 'str',
206+
dest = 'port',
207+
)
208+
option_unix = make_option('--unix',
209+
help = 'path to filesystem socket',
210+
type = 'str',
211+
dest = 'unix',
212+
)
213+
214+
def append_settings(option, opt_str, value, parser):
215+
'split the string into a (key,value) pair tuple'
216+
kv = value.split('=', 1)
217+
if len(kv) != 2:
218+
raise OptionValueError("invalid setting argument, %r" %(value,))
219+
parser.values.db_client_parameters.append(
220+
((option.dest, kv[0]), kv[1])
221+
)
222+
223+
option_settings = make_option('-s', '--setting',
224+
dest = 'settings',
225+
help = 'run-time parameters to set upon connecting',
226+
callback = append_settings,
227+
type = 'str',
228+
)
229+
230+
option_sslmode = make_option('--ssl-mode',
231+
dest = 'sslmode',
232+
help = 'SSL rules for connectivity',
233+
choices = ('require','prefer','allow','disable'),
234+
type = 'choice',
235+
)
236+
237+
option_role = make_option('--role',
238+
dest = 'role',
239+
help = 'run operation as the role',
240+
type = 'str',
241+
)
242+
243+
def append_db_client_x_parameters(option, opt_str, value, parser):
244+
parser.values.db_client_parameters.append((option.dest, value))
245+
make_x_option = partial(make_option, callback = append_db_client_x_parameters)
246+
247+
option_iri = make_x_option('-I', '--iri',
248+
help = 'complete resource identifier, pq-IRI',
249+
type = 'str',
250+
dest = 'pq_iri',
251+
)
252+
option_dsn = make_x_option('--dsn',
253+
help = 'DSN for connection',
254+
type = 'str',
255+
dest = 'pq_dsn',
256+
)
257+
258+
# PostgreSQL Standard Options
259+
standard_optparse_options = (
260+
option_host, option_port,
261+
option_user, option_password,
262+
option_database,
263+
)
264+
265+
class StandardParser(optparse.OptionParser):
266+
"""
267+
Option parser limited to the basic -U, -h, -p, -W, and -D options.
268+
This parser subclass is necessary for two reasons:
269+
270+
1. _add_help_option override to not conflict with -h
271+
2. Initialize the db_client_parameters on the parser's values.
272+
273+
See the DefaultParser for more fun.
274+
"""
275+
standard_option_list = standard_optparse_options
276+
277+
def get_default_values(self, *args, **kw):
278+
v = super().get_default_values(*args, **kw)
279+
v.db_client_parameters = []
280+
return v
281+
282+
def _add_help_option(self):
283+
# Only allow long --help so that it will not conflict with -h(host)
284+
self.add_option("--help",
285+
action = "help",
286+
help = "show this help message and exit",
287+
)
288+
289+
# Extended Options
290+
default_optparse_options = [
291+
option_unix,
292+
option_sslmode,
293+
option_role,
294+
option_settings,
295+
# Complex Options
296+
option_iri,
297+
option_dsn,
298+
]
299+
default_optparse_options.extend(standard_optparse_options)
300+
301+
class DefaultParser(StandardParser):
302+
"""
303+
Parser that includes a variety of connectivity options.
304+
(IRI, DSN, sslmode, role(set role), settings)
305+
"""
306+
standard_option_list = default_optparse_options
307+
153308
def resolve_password(
154309
d : "a fully normalized set of client parameters(dict)",
155310
getpass = getpass,
@@ -367,8 +522,7 @@ def standard(
367522

368523
if __name__ == '__main__':
369524
import pprint
370-
from . import clientoptparse as cop
371-
p = cop.DefaultParser(
525+
p = DefaultParser(
372526
description = "print the clientparams dictionary for the environment"
373527
)
374528
(co, ca) = p.parse_args()

0 commit comments

Comments
 (0)