Skip to content

Commit

Permalink
Description markdown export option
Browse files Browse the repository at this point in the history
  • Loading branch information
bclenet committed Sep 26, 2023
1 parent 4b30504 commit 722b999
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
33 changes: 33 additions & 0 deletions narps_open/data/description/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from os.path import join
from csv import DictReader
from json import dumps
from importlib_resources import files

class TeamDescription(dict):
Expand All @@ -25,6 +26,9 @@ def __init__(self, team_id):
self.team_id = team_id
self._load()

def __str__(self):
return dumps(self, indent = 4)

@property
def general(self) -> dict:
""" Getter for the sub dictionary general """
Expand Down Expand Up @@ -55,6 +59,35 @@ def derived(self) -> dict:
""" Getter for the sub dictionary containing derived team description """
return self._get_sub_dict('derived')

def markdown(self):
""" Return the team description as a string formatted in markdown """
return_string = f'# NARPS team description : {self.team_id}\n'

dictionaries = [
self.general,
self.exclusions,
self.preprocessing,
self.analysis,
self.categorized_for_analysis,
self.derived
]

names = [
'General',
'Exclusions',
'Preprocessing',
'Analysis',
'Categorized for analysis',
'Derived'
]

for dictionary, name in zip(dictionaries, names):
return_string += f'## {name}\n'
for key in dictionary:
return_string += f'* `{key}` : {dictionary[key]}\n'

return return_string

def _get_sub_dict(self, key_first_part:str) -> dict:
""" Return a sub-dictionary of self, with keys that contain key_first_part.
The first part of the keys are removed, e.g.:
Expand Down
34 changes: 21 additions & 13 deletions narps_open/data/description/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,30 @@
'derived'
],
help='the sub dictionary of team description')
formats = parser.add_mutually_exclusive_group(required = False)
formats.add_argument('--json', action='store_true', help='output team description as JSON')
formats.add_argument('--md', action='store_true', help='output team description as Markdown')
arguments = parser.parse_args()

# Initialize a TeamDescription
information = TeamDescription(team_id = arguments.team)

if arguments.dictionary == 'general':
print(dumps(information.general, indent = 4))
elif arguments.dictionary == 'exclusions':
print(dumps(information.exclusions, indent = 4))
elif arguments.dictionary == 'preprocessing':
print(dumps(information.preprocessing, indent = 4))
elif arguments.dictionary == 'analysis':
print(dumps(information.analysis, indent = 4))
elif arguments.dictionary == 'categorized_for_analysis':
print(dumps(information.categorized_for_analysis, indent = 4))
elif arguments.dictionary == 'derived':
print(dumps(information.derived, indent = 4))
# Output description
if arguments.md and arguments.dictionary is not None:
print('Sub dictionaries cannot be exported as Markdown yet.')
print('Print the whole description instead.')
elif arguments.md:
print(information.markdown())
else:
print(dumps(information, indent = 4))
if arguments.dictionary == 'general':
print(dumps(information.general, indent = 4))
elif arguments.dictionary == 'exclusions':
print(dumps(information.exclusions, indent = 4))
elif arguments.dictionary == 'preprocessing':
print(dumps(information.preprocessing, indent = 4))
elif arguments.dictionary == 'analysis':
print(dumps(information.analysis, indent = 4))
elif arguments.dictionary == 'categorized_for_analysis':
print(dumps(information.categorized_for_analysis, indent = 4))
elif arguments.dictionary == 'derived':
print(dumps(information.derived, indent = 4))

0 comments on commit 722b999

Please sign in to comment.