1
1
#!python
2
2
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3
3
# vi: set ft=python sts=4 ts=4 sw=4 et:
4
-
5
4
import click
6
5
6
+ from .instance import list_interfaces
7
+ from .utils import (CONTEXT_SETTINGS ,
8
+ UNKNOWN_OPTIONS ,
9
+ ExistingDirPath ,
10
+ ExistingFilePath ,
11
+ RegularExpression ,
12
+ PythonModule )
13
+
7
14
8
- @click .group ()
15
+ # declare the CLI group
16
+ @click .group (context_settings = CONTEXT_SETTINGS )
9
17
def cli ():
10
18
pass
11
19
12
20
13
- @cli .command ()
14
- @click .argument ('logdir' , type = str )
15
- @click .option ('-r' , '--regex' , type = str , default = '*' ,
21
+ @cli .command (context_settings = CONTEXT_SETTINGS )
22
+ @click .argument ('logdir' , type = ExistingDirPath )
23
+ @click .option ('-r' , '--regex' , type = RegularExpression () , default = '*' ,
16
24
help = 'Regular expression to be searched in each traceback.' )
17
25
def search (logdir , regex ):
18
26
"""Search for tracebacks content.
19
27
20
28
Search for traceback inside a folder of nipype crash log files that match
21
29
a given regular expression.
22
30
23
- Examples:
24
- nipype search -d nipype/wd/log -r '.*subject123.*'
31
+ Examples:\n
32
+ nipype search nipype/wd/log -r '.*subject123.*'
25
33
"""
26
- import re
27
34
from .crash_files import iter_tracebacks
28
35
29
- rex = re .compile (regex , re .IGNORECASE )
30
36
for file , trace in iter_tracebacks (logdir ):
31
- if rex .search (trace ):
37
+ if regex .search (trace ):
32
38
click .echo ("-" * len (file ))
33
39
click .echo (file )
34
40
click .echo ("-" * len (file ))
35
41
click .echo (trace )
36
42
37
43
38
- @cli .command ()
39
- @click .argument ('crashfile' , type = str )
44
+ @cli .command (context_settings = CONTEXT_SETTINGS )
45
+ @click .argument ('crashfile' , type = ExistingFilePath , )
40
46
@click .option ('-r' , '--rerun' , is_flag = True , flag_value = True ,
41
47
help = 'Rerun crashed node.' )
42
48
@click .option ('-d' , '--debug' , is_flag = True , flag_value = True ,
43
49
help = 'Enable Python debugger when re-executing.' )
44
50
@click .option ('-i' , '--ipydebug' , is_flag = True , flag_value = True ,
45
51
help = 'Enable IPython debugger when re-executing.' )
46
- @click .option ('--dir' , type = str ,
52
+ @click .option ('--dir' , type = ExistingDirPath ,
47
53
help = 'Directory where to run the node in.' )
48
54
def crash (crashfile , rerun , debug , ipydebug , directory ):
49
55
"""Display Nipype crash files.
50
56
51
57
For certain crash files, one can rerun a failed node in a temp directory.
52
58
53
- Examples:
54
- nipype crash crashfile.pklz
55
- nipype crash crashfile.pklz -r -i
56
- nipype crash crashfile.pklz -r -i
59
+ Examples:\n
60
+ nipype crash crashfile.pklz\n
61
+ nipype crash crashfile.pklz -r -i\n
57
62
"""
58
63
from .crash_files import display_crash_file
59
64
@@ -67,16 +72,123 @@ def crash(crashfile, rerun, debug, ipydebug, directory):
67
72
display_crash_file (crashfile , rerun , debug , directory )
68
73
69
74
70
- @cli .command ()
71
- @click .argument ('pklz_file' , type = str )
75
+ @cli .command (context_settings = CONTEXT_SETTINGS )
76
+ @click .argument ('pklz_file' , type = ExistingFilePath )
72
77
def show (pklz_file ):
73
78
"""Print the content of Nipype node .pklz file.
74
79
75
- Examples:
80
+ Examples:\n
76
81
nipype show node.pklz
77
82
"""
78
83
from pprint import pprint
79
84
from ..utils .filemanip import loadpkl
80
85
81
86
pkl_data = loadpkl (pklz_file )
82
87
pprint (pkl_data )
88
+
89
+
90
+ @cli .command (context_settings = UNKNOWN_OPTIONS )
91
+ @click .argument ('module' , type = PythonModule (), required = False )
92
+ @click .argument ('interface' , type = str , required = False )
93
+ @click .option ('--list' , is_flag = True , flag_value = True ,
94
+ help = 'List the available Interfaces inside the given module.' )
95
+ @click .option ('-h' , '--help' , is_flag = True , flag_value = True ,
96
+ help = 'Show help message and exit.' )
97
+ @click .pass_context
98
+ def run (ctx , module , interface , list , help ):
99
+ """Run a Nipype Interface.
100
+
101
+ Examples:\n
102
+ nipype run nipype.interfaces.nipy --list\n
103
+ nipype run nipype.interfaces.nipy ComputeMask --help
104
+ """
105
+ import argparse
106
+ from .utils import add_args_options
107
+ from ..utils .nipype_cmd import run_instance
108
+
109
+ # print run command help if no arguments are given
110
+ module_given = bool (module )
111
+ if not module_given :
112
+ click .echo (ctx .command .get_help (ctx ))
113
+
114
+ # print the list available interfaces for the given module
115
+ elif (module_given and list ) or (module_given and not interface ):
116
+ iface_names = list_interfaces (module )
117
+ click .echo ('Available Interfaces:' )
118
+ for if_name in iface_names :
119
+ click .echo (' {}' .format (if_name ))
120
+
121
+ # check the interface
122
+ elif (module_given and interface ):
123
+ description = "Run {}" .format (interface )
124
+ prog = " " .join ([ctx .command_path ,
125
+ module .__name__ ,
126
+ interface ] +
127
+ ctx .args )
128
+ iface_parser = argparse .ArgumentParser (description = description ,
129
+ prog = prog )
130
+
131
+ # instantiate the interface
132
+ node = getattr (module , interface )()
133
+ iface_parser = add_args_options (iface_parser , node )
134
+
135
+ if not ctx .args :
136
+ # print the interface help
137
+ iface_parser .print_help ()
138
+ else :
139
+ # run the interface
140
+ args = iface_parser .parse_args (args = ctx .args )
141
+ run_instance (node , args )
142
+
143
+
144
+ #
145
+ # @cli.command(context_settings=CONTEXT_SETTINGS.update(dict(ignore_unknown_options=True,)))
146
+ # @click.argument('-f', '--format', type=click.Choice(['boutiques']))
147
+ # @click.argument('format_args', nargs=-1, type=click.UNPROCESSED)
148
+ # @click.pass_context
149
+ # def convert(ctx, format):
150
+ # """Export nipype interfaces to other formats."""
151
+ # if format == 'boutiques':
152
+ # ctx.forward(to_boutiques)
153
+ # import pdb; pdb.set_trace()
154
+ # ctx.invoke(to_boutiques, **format_args)
155
+ #
156
+ #
157
+ # @cli.command(context_settings=CONTEXT_SETTINGS)
158
+ # @click.option("-i", "--interface", type=str, required=True,
159
+ # help="Name of the Nipype interface to export.")
160
+ # @click.option("-m", "--module", type=PythonModule(), required=True,
161
+ # help="Module where the interface is defined.")
162
+ # @click.option("-o", "--output", type=str, required=True,
163
+ # help="JSON file name where the Boutiques descriptor will be written.")
164
+ # @click.option("-t", "--ignored-template-inputs", type=str, multiple=True,
165
+ # help="Interface inputs ignored in path template creations.")
166
+ # @click.option("-d", "--docker-image", type=str,
167
+ # help="Name of the Docker image where the Nipype interface is available.")
168
+ # @click.option("-r", "--docker-index", type=str,
169
+ # help="Docker index where the Docker image is stored (e.g. http://index.docker.io).")
170
+ # @click.option("-n", "--ignore-template-numbers", is_flag=True, flag_value=True,
171
+ # help="Ignore all numbers in path template creations.")
172
+ # @click.option("-v", "--verbose", is_flag=True, flag_value=True,
173
+ # help="Enable verbose output.")
174
+ # def to_boutiques(interface, module, output, ignored_template_inputs,
175
+ # docker_image, docker_index, ignore_template_numbers,
176
+ # verbose):
177
+ # """Nipype Boutiques exporter.
178
+ #
179
+ # See Boutiques specification at https://github.com/boutiques/schema.
180
+ # """
181
+ # from nipype.utils.nipype2boutiques import generate_boutiques_descriptor
182
+ #
183
+ # # Generates JSON string
184
+ # json_string = generate_boutiques_descriptor(module,
185
+ # interface,
186
+ # ignored_template_inputs,
187
+ # docker_image,
188
+ # docker_index,
189
+ # verbose,
190
+ # ignore_template_numbers)
191
+ #
192
+ # # Writes JSON string to file
193
+ # with open(output, 'w') as f:
194
+ # f.write(json_string)
0 commit comments