comparison env/lib/python3.9/site-packages/planemo/config.py @ 0:4f3585e2f14b draft default tip

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