diff --git a/visualdataset/__init__.py b/visualdataset/__init__.py index 8423dad..2dd9179 100644 --- a/visualdataset/__init__.py +++ b/visualdataset/__init__.py @@ -9,4 +9,4 @@ |_| """ -__version__ = '0.2.3' +__version__ = '0.3.0' diff --git a/visualdataset/__main__.py b/visualdataset/__main__.py index e4dcca0..a77bad4 100644 --- a/visualdataset/__main__.py +++ b/visualdataset/__main__.py @@ -14,14 +14,14 @@ parser = ArgumentParser(description='Prepares a dataset for use with the ChRIS_ui ' '"Visual Datasets" feature.', formatter_class=ArgumentDefaultsHelpFormatter) -parser.add_argument('--mode', type=str, default='file', choices=['file', 'string', 'freesurfer-7.3.3', 'malpem-1.3'], - help='File matching and option selection mode. file=accept JSON files from ' - '--matchers and --options. string=accept JSON strings from --matchers and --options. ' - 'freesurfer=built-in support for the FreeSurfer output files.') +parser.add_argument('--mode', type=str, default='none', choices=['freesurfer-7.3.3', 'malpem-1.3', 'none'], + help='Use default values for --matchers and --options for a well known program\'s output. ') parser.add_argument('--matchers', type=str, - help='Regular expressions used to assign tags to files') + help='Regular expressions used to assign tags to files, ' + 'as stringified JSON or a path to a file relative to inputdir') parser.add_argument('--options', type=str, - help='Metadata to go with tag sets') + help='Metadata to go with tag sets, ' + 'as stringified JSON or a path to a file relative to inputdir') parser.add_argument('--first-run-files', type=str, default='[]', help='List of files to show on first run, ' 'as a stringified JSON list of paths relative to inputdir') diff --git a/visualdataset/json_arg_parser.py b/visualdataset/json_arg_parser.py index c462df7..2b5e7e6 100644 --- a/visualdataset/json_arg_parser.py +++ b/visualdataset/json_arg_parser.py @@ -10,30 +10,50 @@ from visualdataset.wellknown import FREESURFER_MATCHERS, FREESURFER_OPTIONS, MALPEM_MATCHERS, MALPEM_OPTIONS +EVERYTHING_MATCHER: Sequence[Matcher] = [Matcher(key='type', value='file', regex=r'\.(nii(\.gz)?)|(mgz)$')] +""" +a Matcher matching all supported file formats. +""" + + def parse_args(input_dir: Path, mode: str, matchers: str | None, options: str | None ) -> tuple[Sequence[Matcher], Sequence[OptionsLink]]: mode = mode.lower() - if mode.startswith('freesurfer'): - return FREESURFER_MATCHERS, FREESURFER_OPTIONS - if mode.startswith('malpem'): - return MALPEM_MATCHERS, MALPEM_OPTIONS - if mode == 'file': - matchers_str = '[]' if matchers is None else (input_dir / matchers).read_text() - options_str = '[]' if options is None else (input_dir / options).read_text() - elif mode == 'string': - matchers_str = '[]' if matchers is None else matchers - options_str = '[]' if options is None else options - else: - print(f'Unsupported option --mode={mode}') - sys.exit(1) - matchers_list = deserialize_list(matchers_str, Matcher, '--matchers') - options_list = deserialize_list(options_str, OptionsLink, '--options') + matchers_list = None + options_list = [] + + if 'freesurfer' in mode: + matchers_list = FREESURFER_MATCHERS + options_list = FREESURFER_OPTIONS + if 'malpem' in mode: + matchers_list = MALPEM_MATCHERS + options_list = MALPEM_OPTIONS + + if matchers is not None: + matchers_list = deserialize_list(file_or_string(input_dir, matchers), Matcher, '--matchers') + if options is not None: + options_list = deserialize_list(file_or_string(input_dir, options), OptionsLink, '--options') + + if matchers_list is None: + matchers_list = EVERYTHING_MATCHER + return matchers_list, options_list _M = TypeVar('_M', bound=BaseModel) +def file_or_string(dir: Path, arg: str) -> str: + path = dir / arg + try: + is_file = path.is_file() + except OSError: # in case file name is too long (it's probably the value!) + is_file = False + if is_file: + return path.read_text() + return arg + + def deserialize_list(s: str, t: Type[_M], flag: str) -> Sequence[_M]: try: data = json.loads(s)