Skip to content

Commit e812f9e

Browse files
committed
[ENH] Allow accessing metadata in template_description.json
Adds one doctest. Close #3.
1 parent 4b480e4 commit e812f9e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

templateflow/api.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
TemplateFlow's Python Client
33
"""
4+
from json import loads
45
from datalad import api
56
from datalad.support.exceptions import IncompleteResultsError
67
from .conf import TF_HOME
@@ -51,3 +52,28 @@ def templates():
5152
templates = [str(p.name)[4:] for p in TF_HOME.glob('tpl-*')]
5253

5354
return sorted(templates)
55+
56+
57+
def get_metadata(template_id):
58+
"""
59+
Fetch one file from one template
60+
61+
>>> get_metadata('MNI152Lin')['Name']
62+
'Linear ICBM Average Brain (ICBM152) Stereotaxic Registration Model'
63+
64+
"""
65+
66+
filepath = TF_HOME / ('tpl-%s' % template_id) / 'template_description.json'
67+
68+
# Ensure that template is installed and file is available
69+
try:
70+
api.get(str(filepath))
71+
except IncompleteResultsError as exc:
72+
if exc.failed[0]['message'] == 'path not associated with any dataset':
73+
from .conf import TF_GITHUB_SOURCE
74+
api.install(path=str(TF_HOME), source=TF_GITHUB_SOURCE, recursive=True)
75+
api.get(str(filepath))
76+
else:
77+
raise
78+
79+
return loads(filepath.read_text())

0 commit comments

Comments
 (0)