Skip to content

Commit

Permalink
Allow --options to be a file
Browse files Browse the repository at this point in the history
  • Loading branch information
jennydaman committed Feb 2, 2024
1 parent 7339261 commit 1bca402
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion print_fetal_brain_atlases_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@
}

if __name__ == "__main__":
json.dump(FILENAME_MAPPING, sys.stdout)
json.dump(FILENAME_MAPPING, sys.stdout, indent=2)
2 changes: 1 addition & 1 deletion pubchrisvisual/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
|_|
"""

__version__ = '0.0.1'
__version__ = '0.0.2'
29 changes: 27 additions & 2 deletions pubchrisvisual/one.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
parser.add_argument('--order', type=str,
help='Order of preference for file names as a comma-separated list')
parser.add_argument('--options', type=str, default='{}',
help='Mapping of file names to default Niivue options as stringified JSON')
help='Mapping of file names to default Niivue options. '
'Should either be a relative path or stringified JSON')
parser.add_argument('--readme', type=str,
help='README file content')

Expand All @@ -36,7 +37,7 @@
min_cpu_limit='200m',
)
def main(options: Namespace, inputdir: Path, outputdir: Path):
configs = deserialize_mapping(options.options)
configs = deserialize_mapping(path_or_fname(inputdir, options.options))
order = [name.strip() for name in ','.strip(options.order)]
print(DISPLAY_TITLE, flush=True)
shutil.copytree(inputdir, outputdir, dirs_exist_ok=True)
Expand All @@ -55,6 +56,8 @@ def main(options: Namespace, inputdir: Path, outputdir: Path):
if options.readme is not None:
(outputdir / 'README.txt').write_text(options.readme)

delete_file_and_empty_parents(outputdir, options.options)

(outputdir / '.chrisvisualdataset.root.json').write_text('{}')


Expand Down Expand Up @@ -92,5 +95,27 @@ def deserialize_mapping(x: str) -> dict[str, ChrisViewerFileOptions]:
sys.exit(1)


def path_or_fname(parent_dir: Path, value: str):
p = parent_dir / value
return p.read_text() if p.is_file() else value


def delete_file_and_empty_parents(root: Path, fname: str):
p = root / fname
if not p.is_file():
return
p.unlink()
delete_empty_dirs(p.resolve(), root.resolve())


def delete_empty_dirs(p: Path, root: Path):
if p.resolve() == root:
return
if next(p.glob('*'), None) is not None:
return
p.rmdir()
delete_empty_dirs(p.parent, root)


if __name__ == '__main__':
main()

0 comments on commit 1bca402

Please sign in to comment.