mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-26 22:56: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.do_help.aliases.append('h')
|
||||
|
||||
conf.get_config()
|
||||
|
||||
|
||||
def get_optparser(self):
|
||||
"""this is the parser for "global" options (not specific to subcommand)"""
|
||||
|
||||
optparser = cmdln.CmdlnOptionParser(self, version=get_osc_version())
|
||||
optparser.add_option('-H', '--http-debug', action='store_true',
|
||||
default=conf.config['http_debug'],
|
||||
help='debug HTTP traffic')
|
||||
optparser.add_option('-A', '--apisrv', dest='apisrv',
|
||||
metavar='URL',
|
||||
help='specify URL to access API server at')
|
||||
optparser.add_option('-c', '--config', dest='conffile',
|
||||
metavar='FILE',
|
||||
help='specify alternate configuration file')
|
||||
return optparser
|
||||
|
||||
|
||||
def postoptparse(self):
|
||||
"""merge commandline options into the config"""
|
||||
|
||||
conf.config['http_debug'] = self.options.http_debug
|
||||
if self.options.apisrv:
|
||||
conf.config['scheme'], conf.config['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)
|
||||
conf.get_config(override_conffile = self.options.conffile,
|
||||
override_http_debug = self.options.http_debug,
|
||||
override_apisrv = self.options.apisrv)
|
||||
|
||||
|
||||
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'])
|
||||
|
||||
|
||||
def get_config():
|
||||
def get_config(override_conffile = None,
|
||||
override_http_debug = None,
|
||||
override_apisrv = None):
|
||||
"""do the actual work (see module documentation)"""
|
||||
import os
|
||||
import sys
|
||||
global config
|
||||
|
||||
conffile = os.environ.get('OSC_CONFIG', '~/.oscrc')
|
||||
conffile = override_conffile or os.environ.get('OSC_CONFIG', '~/.oscrc')
|
||||
conffile = os.path.expanduser(conffile)
|
||||
|
||||
if not os.path.exists(conffile):
|
||||
@ -249,3 +251,20 @@ def get_config():
|
||||
# add the auth data we collected to the config 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