6
6
7
7
Usage::
8
8
9
- haddock3-cfg -m MODULE
9
+ haddock3-cfg # prints only the general parametes
10
+ haddock3-cfg -g # same as above
11
+ haddock3-cfg -m MODULE # prints only the module parameters
10
12
haddock3-cfg -m MODULE -l LEVEL
13
+ haddock3-cfg -m topoaa -l all
14
+ haddock3-cfg -m rigidbody -l all -g # prints the module and the globals
11
15
"""
12
16
import argparse
13
17
import importlib
18
+ import os
14
19
import sys
15
20
16
21
from haddock import config_expert_levels
17
- from haddock .gear .yaml2cfg import yaml2cfg_text
18
- from haddock .libs .libio import read_from_yaml
19
22
from haddock .modules import modules_category
20
23
21
24
29
32
"-m" ,
30
33
dest = "module" ,
31
34
help = "The module for which you want to retrieve the default configuration." ,
32
- required = True ,
33
35
choices = sorted (modules_category .keys ()),
36
+ default = None ,
34
37
)
35
38
36
39
ap .add_argument (
42
45
choices = config_expert_levels + ("all" ,),
43
46
)
44
47
48
+ ap .add_argument (
49
+ '-g' ,
50
+ '--globals' ,
51
+ dest = 'global_params' ,
52
+ help = "Add also the optional module's general parameters." ,
53
+ action = "store_true" ,
54
+ )
55
+
45
56
46
57
def _ap ():
47
58
return ap
@@ -57,6 +68,20 @@ def load_args(ap):
57
68
def cli (ap , main ):
58
69
"""Command-line interface entry point."""
59
70
cmd = load_args (ap )
71
+
72
+ # I didn't want to to have the `--globals` param as a negative parameter
73
+ # ('store_false'). However, `module` and `globals` can't both be false.
74
+ # In order for the commands below performing the same:
75
+ #
76
+ # haddock3-cfg
77
+ # haddock3-cfg -g
78
+ #
79
+ # we need this quick if statement. Which basically adjust the argparse to
80
+ # the default values in the main function.
81
+ # @joaomcteixeira
82
+ if cmd .global_params is False and cmd .module is None :
83
+ cmd .global_params = True
84
+
60
85
main (** vars (cmd ))
61
86
62
87
@@ -65,22 +90,69 @@ def maincli():
65
90
cli (ap , main )
66
91
67
92
68
- def main (module , explevel ):
69
- """Extract the default configuration file for a given module."""
70
- module_name = "." .join ((
71
- 'haddock' ,
72
- 'modules' ,
73
- modules_category [module ],
74
- module ,
75
- ))
76
-
77
- module_lib = importlib .import_module (module_name )
78
- cfg = module_lib .DEFAULT_CONFIG
79
-
80
- ycfg = read_from_yaml (cfg )
81
-
82
- new_config = yaml2cfg_text (ycfg , module , explevel )
83
- print (new_config , file = sys .stdout , flush = True ) # noqa: T201
93
+ def main (module = None , explevel = "all" , global_params = True ):
94
+ """
95
+ Extract the defaults in the form of a run configuration file.
96
+
97
+ Parameters
98
+ ----------
99
+ module : str or None
100
+ The module name to extract the defaults from.
101
+ If ``None`` given, we expect ``global_params`` to be ``True``,
102
+ otherwise nothing will be printed.
103
+
104
+ explevel : str
105
+ Filter the parameters according to the expert level. Output all
106
+ parameters that belong to the referred level or below. Choices
107
+ are: all, easy, expert, and guru.
108
+
109
+ global_params : bool
110
+ Whether to add the module's general global parameter. If
111
+ ``True`` and ``module`` is ``None``, outputs only the general
112
+ parameters.
113
+ """
114
+ from haddock import modules_defaults_path
115
+ from haddock .gear .yaml2cfg import yaml2cfg_text
116
+ from haddock .libs .libio import read_from_yaml
117
+
118
+ new_config = ''
119
+
120
+ if global_params :
121
+ general_cfg = read_from_yaml (modules_defaults_path )
122
+ general_params_str = yaml2cfg_text (
123
+ general_cfg ,
124
+ module = None ,
125
+ explevel = "all" ,
126
+ )
127
+ comment = os .linesep .join ((
128
+ "# The parameters below are optional parameters. " ,
129
+ "# They can either be used as global parameters or as part " ,
130
+ "# of the module's parameters" ,
131
+ ))
132
+
133
+ new_config = os .linesep .join ((
134
+ comment ,
135
+ general_params_str ,
136
+ ))
137
+
138
+ if module :
139
+
140
+ module_name = "." .join ((
141
+ 'haddock' ,
142
+ 'modules' ,
143
+ modules_category [module ],
144
+ module ,
145
+ ))
146
+
147
+ module_lib = importlib .import_module (module_name )
148
+ cfg = module_lib .DEFAULT_CONFIG
149
+
150
+ ycfg = read_from_yaml (cfg )
151
+ module_config = yaml2cfg_text (ycfg , module , explevel )
152
+
153
+ new_config = os .linesep .join ((new_config , module_config ))
154
+
155
+ sys .stdout .write (new_config )
84
156
85
157
return 0
86
158
0 commit comments