comparison env/lib/python3.7/site-packages/planemo/config.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 """Module defines abstractions for configuring Planemo."""
2
3 import os
4
5 import aenum
6 import click
7 import yaml
8
9 PLANEMO_CONFIG_ENV_PROP = "PLANEMO_GLOBAL_CONFIG_PATH"
10 DEFAULT_CONFIG = {
11 }
12
13 VALUE_UNSET = object()
14
15
16 OptionSource = aenum.Enum(
17 "OptionSource", 'cli profile global_config default'
18 )
19
20
21 def _default_callback(
22 default, use_global_config=False, resolve_path=False, extra_global_config_vars=[],
23 ):
24
25 def callback(ctx, param, value):
26 planemo_ctx = ctx.obj
27 param_name = param.name
28 if value is not None:
29 result = value
30 option_source = OptionSource.cli
31 else:
32 result, option_source = _find_default(
33 planemo_ctx,
34 param,
35 use_global_config=use_global_config,
36 extra_global_config_vars=extra_global_config_vars,
37 )
38
39 if result is VALUE_UNSET:
40 result = default
41 option_source = OptionSource.default
42
43 assert option_source is not None
44 assert result is not VALUE_UNSET
45
46 if resolve_path and result is not None:
47 result = os.path.abspath(result)
48
49 planemo_ctx.set_option_source(param_name, option_source)
50 return result
51
52 return callback
53
54
55 def _find_default(ctx, param, use_global_config, extra_global_config_vars):
56 if use_global_config:
57 global_config = ctx.global_config
58 global_config_keys = ["default_%s" % param.name] + extra_global_config_vars
59 for global_config_key in global_config_keys:
60 if global_config_key in global_config:
61 default_value = global_config[global_config_key]
62 return default_value, OptionSource.global_config
63
64 return VALUE_UNSET, None
65
66
67 def planemo_option(*args, **kwargs):
68 """Extend ``click.option`` with planemo-config aware configuration.
69
70 This extends click.option to use a callback when assigning default
71 values, add ``use_global_config`` keyword argument to allow reading
72 defaults from ~/.planemo.yml, and tracks how parameters are specified
73 using the Planemo Context object.
74 """
75 option_type = kwargs.get("type", None)
76 use_global_config = kwargs.pop("use_global_config", False)
77 use_env_var = kwargs.pop("use_env_var", False)
78 extra_global_config_vars = kwargs.pop("extra_global_config_vars", [])
79
80 default_specified = "default" in kwargs
81 default = None
82 if default_specified:
83 default = kwargs.pop("default")
84
85 if default_specified or use_global_config or use_env_var:
86 outer_callback = kwargs.pop("callback", None)
87
88 def callback(ctx, param, value):
89 resolve_path = option_type and getattr(option_type, "resolve_path", False)
90 result = _default_callback(
91 default,
92 use_global_config=use_global_config,
93 extra_global_config_vars=extra_global_config_vars,
94 resolve_path=resolve_path,
95 )(ctx, param, value)
96
97 if outer_callback is not None:
98 result = outer_callback(ctx, param, result)
99
100 return result
101
102 kwargs["callback"] = callback
103
104 if default_specified:
105 kwargs["default"] = None
106
107 if use_env_var:
108 name = None
109 for arg in args:
110 if arg.startswith("--"):
111 name = arg[len("--"):]
112 assert name
113 kwargs["envvar"] = "PLANEMO_%s" % name.upper()
114
115 option = click.option(*args, **kwargs)
116 return option
117
118
119 def global_config_path(config_path=None):
120 if not config_path:
121 config_path = os.environ.get(
122 PLANEMO_CONFIG_ENV_PROP,
123 "~/.planemo.yml"
124 )
125 config_path = os.path.expanduser(config_path)
126 return config_path
127
128
129 def read_global_config(config_path):
130 config_path = global_config_path(config_path)
131 if not os.path.exists(config_path):
132 return DEFAULT_CONFIG
133
134 with open(config_path) as f:
135 return yaml.safe_load(f)
136
137
138 __all__ = (
139 "global_config_path",
140 "read_global_config",
141 "planemo_option",
142 )