|
8 | 8 | import sys |
9 | 9 | import os |
10 | 10 | import configparser |
| 11 | +import optparse |
11 | 12 | from itertools import chain |
| 13 | +from functools import partial |
| 14 | + |
12 | 15 |
|
13 | 16 | from . import iri as pg_iri |
14 | 17 | from . import dsn as pg_dsn |
@@ -62,7 +65,7 @@ def getuser(): |
62 | 65 | # Extensions |
63 | 66 | 'ROLE' : 'role', # SET ROLE $PGROLE |
64 | 67 |
|
65 | | - # This keyword will never make it to a connect() function |
| 68 | + # This keyword *should* never make it to a connect() function |
66 | 69 | # as `resolve_password` should be called to fill in the |
67 | 70 | # parameter accordingly. |
68 | 71 | 'PASSFILE' : 'pgpassfile', |
@@ -150,6 +153,158 @@ def envvars(environ = os.environ, modifier : "environment variable key modifier" |
150 | 153 | if service in environ: |
151 | 154 | yield ('pg_service', environ[service]) |
152 | 155 |
|
| 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 | + |
153 | 308 | def resolve_password( |
154 | 309 | d : "a fully normalized set of client parameters(dict)", |
155 | 310 | getpass = getpass, |
@@ -367,8 +522,7 @@ def standard( |
367 | 522 |
|
368 | 523 | if __name__ == '__main__': |
369 | 524 | import pprint |
370 | | - from . import clientoptparse as cop |
371 | | - p = cop.DefaultParser( |
| 525 | + p = DefaultParser( |
372 | 526 | description = "print the clientparams dictionary for the environment" |
373 | 527 | ) |
374 | 528 | (co, ca) = p.parse_args() |
|
0 commit comments