Skip to content

Commit

Permalink
Allow string and boolean keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
groutr committed Jul 21, 2015
1 parent 5df83e9 commit d093ca5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
48 changes: 20 additions & 28 deletions conda/cli/main_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,11 @@ def __init__(self, reason):
yaml parser (this will remove any structure or comments from the existing
.condarc file). Reason: %s""" % reason]

class BoolKey(common.Completer):
def __contains__(self, other):
# Other is either one of the keys or the boolean
try:
import yaml
except ImportError:
yaml = False

ret = other in config.rc_bool_keys
if yaml:
ret = ret or isinstance(yaml.load(other), bool)

return ret

class SingleValueKey(common.Completer):
def _get_items(self):
return config.rc_bool_keys + ['yes', 'no', 'on', 'off', 'true', 'false']
return config.rc_bool_keys + \
config.rc_string_keys + \
['yes', 'no', 'on', 'off', 'true', 'false']

class ListKey(common.Completer):
def _get_items(self):
Expand Down Expand Up @@ -182,11 +171,10 @@ def configure_parser(sub_parsers):
"--set",
nargs=2,
action="append",
help="""Set a boolean key. BOOL_VALUE should be a valid YAML boolean,
such as 'yes' or 'no'.""",
help="""Set a key.""",
default=[],
choices=BoolKey(),
metavar=('KEY', 'BOOL_VALUE'),
choices=SingleValueKey(),
metavar=('KEY', 'VALUE'),
)
action.add_argument(
"--remove",
Expand Down Expand Up @@ -322,13 +310,21 @@ def execute_config(args, parser):
new_rc_config.setdefault(key, []).insert(0, item)

# Set
set_bools, set_strings = set(config.rc_bool_keys), set(config.rc_string_keys)
for key, item in args.set:
# Check key and value
yamlitem = yaml.load(item)
if not isinstance(yamlitem, bool):
common.error_and_exit("%r is not a boolean" % item, json=args.json,
error_type="TypeError")

new_rc_config[key] = yamlitem
if key in set_bools:
if not isinstance(yamlitem, bool):
common.error_and_exit("Key: %s; Value is not a boolean" % (key, item), json=args.json,
error_type="TypeError")
new_rc_config[key] = yamlitem
elif key in set_strings:
new_rc_config[key] = yamlitem
else:
common.error_and_exit("Error key must be one of %s, not %s" %
(set_bools | set_strings, key), json=args.json,
error_type="ValueError")

# Remove
for key, item in args.remove:
Expand Down Expand Up @@ -405,10 +401,6 @@ def execute_config(args, parser):
new_rc_config['channels'].append('defaults')

for key, item in args.set:
if key not in config.rc_bool_keys:
common.error_and_exit("Error key must be one of %s, not %s" %
(config.rc_bool_keys, key), json=args.json,
error_type="ValueError")
added = False
for pos, line in enumerate(new_rc_text[:]):
matched = setkeyregexes[key].match(line)
Expand Down
14 changes: 7 additions & 7 deletions conda/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@
'allow_other_channels',
]

rc_string_keys = [
'ssl_verify',
'channel_alias',
'root_dir',
]

# Not supported by conda config yet
rc_other = [
'proxy_servers',
'root_dir',
'channel_alias',
'ssl_verify'
]

user_rc_path = abspath(expanduser('~/.condarc'))
Expand Down Expand Up @@ -333,12 +338,7 @@ def get_proxy_servers():
create_default_packages = list(rc.get('create_default_packages', []))

# ssl_verify can be a boolean value or a filename string
ssl_verify = rc.get('ssl_verify', 'True')
_v = ssl_verify.lower()
if _v in {'on', 'true', 'yes'}:
ssl_verify = True
elif _v in {'off', 'false', 'no'}:
ssl_verify = False
ssl_verify = rc.get('ssl_verify', True)

try:
track_features = set(rc['track_features'].split())
Expand Down

0 comments on commit d093ca5

Please sign in to comment.