Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/planemo/config.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
| author | bcclaywell |
|---|---|
| date | Mon, 12 Oct 2015 17:43:33 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d67268158946 |
|---|---|
| 1 import os | |
| 2 import yaml | |
| 3 | |
| 4 PLANEMO_CONFIG_ENV_PROP = "PLANEMO_GLOBAL_CONFIG_PATH" | |
| 5 DEFAULT_CONFIG = { | |
| 6 } | |
| 7 | |
| 8 | |
| 9 def get_default_callback(default, name=None): | |
| 10 | |
| 11 def callback(ctx, param, value): | |
| 12 planemo_ctx = ctx.obj | |
| 13 config_name = name | |
| 14 if config_name is None: | |
| 15 config_name = param.name | |
| 16 | |
| 17 return _default_option(planemo_ctx, config_name, value, default) | |
| 18 | |
| 19 return callback | |
| 20 | |
| 21 | |
| 22 # TODO: delete the following stuff. | |
| 23 def populate_kwds(ctx, options_hash, kwds): | |
| 24 """ Iterate over default option hash and use value from | |
| 25 ~/.planemo.yml configuration if set or else the specified | |
| 26 default. Populate kwds with this configured default value. | |
| 27 """ | |
| 28 for name, default in options_hash.items(): | |
| 29 _populate_default_output(ctx, name, kwds, default) | |
| 30 | |
| 31 | |
| 32 def _populate_default_output(ctx, kwd_key, kwds, default): | |
| 33 kwd_value = kwds.get(kwd_key, None) | |
| 34 if kwd_value is None: | |
| 35 global_config = ctx.global_config | |
| 36 global_config_key = "default_%s" % kwd_key | |
| 37 if global_config_key in global_config: | |
| 38 default_value = global_config[global_config_key] | |
| 39 else: | |
| 40 default_value = default | |
| 41 | |
| 42 if default_value: | |
| 43 default_value = os.path.abspath(default_value) | |
| 44 kwds[kwd_key] = default_value | |
| 45 | |
| 46 | |
| 47 def _default_option(ctx, kwd_key, kwd_value, default): | |
| 48 value = kwd_value | |
| 49 | |
| 50 if value is None: | |
| 51 global_config = ctx.global_config | |
| 52 global_config_key = "default_%s" % kwd_key | |
| 53 if global_config_key in global_config: | |
| 54 default_value = global_config[global_config_key] | |
| 55 else: | |
| 56 default_value = default | |
| 57 | |
| 58 value = default_value | |
| 59 | |
| 60 return value | |
| 61 | |
| 62 | |
| 63 def global_config_path(config_path=None): | |
| 64 if not config_path: | |
| 65 config_path = os.environ.get( | |
| 66 PLANEMO_CONFIG_ENV_PROP, | |
| 67 "~/.planemo.yml" | |
| 68 ) | |
| 69 config_path = os.path.expanduser(config_path) | |
| 70 return config_path | |
| 71 | |
| 72 | |
| 73 def read_global_config(config_path): | |
| 74 config_path = global_config_path(config_path) | |
| 75 if not os.path.exists(config_path): | |
| 76 return DEFAULT_CONFIG | |
| 77 | |
| 78 with open(config_path) as f: | |
| 79 return yaml.load(f) |
