Skip to content

Commit c6095da

Browse files
committed
NF: Interactive prompts for project initialization and subject updates
1 parent 61f760b commit c6095da

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

datalad_xnat/init.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,35 @@ def __call__(url,
148148
return
149149

150150
if project is None:
151-
from datalad.ui import ui
152151
projects = platform.get_project_ids()
153-
ui.message(
154-
'No project name specified. The following projects are '
155-
'available on {} for user {}:'.format(
152+
from datalad.ui import ui
153+
if ui.is_interactive:
154+
import inquirer
155+
from inquirer.themes import GreenPassion
156+
message = 'Select a project via arrow keys, ' \
157+
'confirm with "Enter"'.format(
156158
url,
157-
'anonymous' if platform.credential_name == 'anonymous'
158-
else platform.authenticated_user))
159-
for p in sorted(projects):
160-
# list and prep for C&P
161-
# TODO multi-column formatting?
162-
ui.message(" {}".format(quote_cmdlinearg(p)))
163-
return
159+
'anonymous' if platform.credential_name == 'anonymous' \
160+
else platform.authenticated_user)
161+
checkbox = [inquirer.List('project',
162+
message=message,
163+
choices=sorted(projects))]
164+
resp = inquirer.prompt(checkbox, theme=GreenPassion())
165+
lgr.info("Received the following project selection from the "
166+
"interactive prompt: %s", str(resp['project']))
167+
project = resp['project']
168+
else:
169+
ui.message(
170+
'No project name specified. The following projects are '
171+
'available on {} for user {}:'.format(
172+
url,
173+
'anonymous' if platform.credential_name == 'anonymous'
174+
else platform.authenticated_user))
175+
for p in sorted(projects):
176+
# list and prep for C&P
177+
# TODO multi-column formatting?
178+
ui.message(" {}".format(quote_cmdlinearg(p)))
179+
return
164180

165181
# query the specified project to make sure it exists and is accessible
166182
try:

datalad_xnat/update.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,43 @@ def __call__(subjects='list', credential=None, dataset=None, ifexists=None, reck
124124

125125
# provide subject list
126126
if 'list' in subjects:
127-
from datalad.ui import ui
128127
subs = platform.get_subject_ids(xnat_project)
129-
ui.message(
130-
'The following subjects are available for XNAT '
131-
'project {}:'.format(xnat_project))
132-
for s in sorted(subs):
133-
ui.message(" {}".format(quote_cmdlinearg(s)))
134-
ui.message(
135-
'Specify a specific subject(s) or "all" to download associated '
136-
'files for.')
137-
return
128+
from datalad.ui import ui
129+
if ui.is_interactive:
130+
import inquirer
131+
from inquirer.themes import GreenPassion
132+
hit = False
133+
subs.insert(0, 'all')
134+
subs.insert(1, 'cancel')
135+
while not hit:
136+
message = 'Select subject(s) to download from ' \
137+
'Project "{}" via arrow keys. ' \
138+
'Confirm with "Enter"'.format(xnat_project)
139+
checkbox = [inquirer.Checkbox('subject',
140+
message=message,
141+
choices=subs)]
142+
resp = inquirer.prompt(checkbox, theme=GreenPassion())
143+
if resp['subject']:
144+
hit = True
145+
else:
146+
ui.message("Please select one or more subjects using "
147+
"the right arrow keys. Unselect with the "
148+
"left arrow key.")
149+
if 'cancel' in resp['subject']:
150+
return
151+
lgr.info("Received the following subject list from an "
152+
"interactive prompt: %s", str(resp['subject']))
153+
subjects = resp['subject']
154+
else:
155+
ui.message(
156+
'The following subjects are available for XNAT '
157+
'project {}:'.format(xnat_project))
158+
for s in sorted(subs):
159+
ui.message(" {}".format(quote_cmdlinearg(s)))
160+
ui.message(
161+
'Specify a specific subject(s) or "all" to download associated '
162+
'files for.')
163+
return
138164

139165
# query the specified subject(s) to make sure it exists and is accessible
140166
# TODO we culd just take the input subject list at face-value

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ python_requires = >= 3.6
1616
install_requires =
1717
datalad >= 0.15.0
1818
requests
19+
inquirer
1920
packages = find:
2021
include_package_data = True
2122

0 commit comments

Comments
 (0)