mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-27 15:06:15 +01:00
- change conf.get_config() to take three optional override values:
get_config(override_conffile = None, override_http_debug = None, override_apisrv = None) Thus, it should be possible to use the osc module with one simple conf.get_config() call. It is no longer required to set up the api url in the config dict, and call conf.init_basicauth().
This commit is contained in:
parent
106645a87b
commit
a3a2667742
@ -32,37 +32,28 @@ class Osc(cmdln.Cmdln):
|
|||||||
cmdln.Cmdln.__init__(self, *args, **kwargs)
|
cmdln.Cmdln.__init__(self, *args, **kwargs)
|
||||||
cmdln.Cmdln.do_help.aliases.append('h')
|
cmdln.Cmdln.do_help.aliases.append('h')
|
||||||
|
|
||||||
conf.get_config()
|
|
||||||
|
|
||||||
|
|
||||||
def get_optparser(self):
|
def get_optparser(self):
|
||||||
"""this is the parser for "global" options (not specific to subcommand)"""
|
"""this is the parser for "global" options (not specific to subcommand)"""
|
||||||
|
|
||||||
optparser = cmdln.CmdlnOptionParser(self, version=get_osc_version())
|
optparser = cmdln.CmdlnOptionParser(self, version=get_osc_version())
|
||||||
optparser.add_option('-H', '--http-debug', action='store_true',
|
optparser.add_option('-H', '--http-debug', action='store_true',
|
||||||
default=conf.config['http_debug'],
|
|
||||||
help='debug HTTP traffic')
|
help='debug HTTP traffic')
|
||||||
optparser.add_option('-A', '--apisrv', dest='apisrv',
|
optparser.add_option('-A', '--apisrv', dest='apisrv',
|
||||||
metavar='URL',
|
metavar='URL',
|
||||||
help='specify URL to access API server at')
|
help='specify URL to access API server at')
|
||||||
|
optparser.add_option('-c', '--config', dest='conffile',
|
||||||
|
metavar='FILE',
|
||||||
|
help='specify alternate configuration file')
|
||||||
return optparser
|
return optparser
|
||||||
|
|
||||||
|
|
||||||
def postoptparse(self):
|
def postoptparse(self):
|
||||||
"""merge commandline options into the config"""
|
"""merge commandline options into the config"""
|
||||||
|
|
||||||
conf.config['http_debug'] = self.options.http_debug
|
conf.get_config(override_conffile = self.options.conffile,
|
||||||
if self.options.apisrv:
|
override_http_debug = self.options.http_debug,
|
||||||
conf.config['scheme'], conf.config['apisrv'] = \
|
override_apisrv = self.options.apisrv)
|
||||||
conf.parse_apisrv_url(conf.config['scheme'], self.options.apisrv)
|
|
||||||
conf.config['apiurl'] = conf.config['scheme'] + '://' + conf.config['apisrv']
|
|
||||||
|
|
||||||
# XXX unless config['user'] goes away (and is replaced with a handy function, or
|
|
||||||
# config becomes an object, even better), set the global 'user' here as well:
|
|
||||||
conf.config['user'] = conf.config['auth_dict'][conf.config['apisrv']]['user']
|
|
||||||
|
|
||||||
# finally, initialize urllib2 for to use the credentials for Basic Authentication
|
|
||||||
conf.init_basicauth(conf.config)
|
|
||||||
|
|
||||||
|
|
||||||
def do_init(self, subcmd, opts, project, package):
|
def do_init(self, subcmd, opts, project, package):
|
||||||
|
23
osc/conf.py
23
osc/conf.py
@ -160,13 +160,15 @@ def init_basicauth(config):
|
|||||||
authhandler.add_password(None, host, auth['user'], auth['pass'])
|
authhandler.add_password(None, host, auth['user'], auth['pass'])
|
||||||
|
|
||||||
|
|
||||||
def get_config():
|
def get_config(override_conffile = None,
|
||||||
|
override_http_debug = None,
|
||||||
|
override_apisrv = None):
|
||||||
"""do the actual work (see module documentation)"""
|
"""do the actual work (see module documentation)"""
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
global config
|
global config
|
||||||
|
|
||||||
conffile = os.environ.get('OSC_CONFIG', '~/.oscrc')
|
conffile = override_conffile or os.environ.get('OSC_CONFIG', '~/.oscrc')
|
||||||
conffile = os.path.expanduser(conffile)
|
conffile = os.path.expanduser(conffile)
|
||||||
|
|
||||||
if not os.path.exists(conffile):
|
if not os.path.exists(conffile):
|
||||||
@ -249,3 +251,20 @@ def get_config():
|
|||||||
# add the auth data we collected to the config dict
|
# add the auth data we collected to the config dict
|
||||||
config['auth_dict'] = auth_dict
|
config['auth_dict'] = auth_dict
|
||||||
|
|
||||||
|
# override values which we were called with
|
||||||
|
if override_http_debug:
|
||||||
|
config['http_debug'] = override_http_debug
|
||||||
|
if override_apisrv:
|
||||||
|
config['scheme'], config['apisrv'] = \
|
||||||
|
parse_apisrv_url(config['scheme'], override_apisrv)
|
||||||
|
|
||||||
|
# to make the mess complete, set up the more convenient api url which we'll rather use
|
||||||
|
config['apiurl'] = config['scheme'] + '://' + config['apisrv']
|
||||||
|
|
||||||
|
# XXX unless config['user'] goes away (and is replaced with a handy function, or
|
||||||
|
# config becomes an object, even better), set the global 'user' here as well:
|
||||||
|
config['user'] = config['auth_dict'][config['apisrv']]['user']
|
||||||
|
|
||||||
|
# finally, initialize urllib2 for to use the credentials for Basic Authentication
|
||||||
|
init_basicauth(config)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user