Commit 05b35efa authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Merge branch 'enh_check_invalid_chars' into 'master'

Check invalid characters in trees

See merge request fsl/fslpy!121
parents 8f9ffdf8 d52b58ed
Pipeline #3681 passed with stages
in 13 minutes and 41 seconds
......@@ -38,6 +38,21 @@ def list_all_trees() -> List[str]:
return trees
def check_forbidden_characters(text, characters, text_type):
"""
Checks the text for forbidden characters
raises ValueError if one is found
:param text: string with the text
:param characters: sequence of forbidden characters
:param text_type: type of the text to raise in error message
"""
bad = [character for character in characters if character in text]
if len(bad) > 0:
raise ValueError('Invalid character(s) "{}" in {}: {}'.format("".join(bad), text_type, text))
def read_line(line: str) -> Tuple[int, PurePath, str]:
"""
Parses line from the tree file
......@@ -54,11 +69,14 @@ def read_line(line: str) -> Tuple[int, PurePath, str]:
match = re.match(r'^(\s*)(\S*)\s*\((\S*)\)\s*$', line)
if match is not None:
gr = match.groups()
check_forbidden_characters(gr[1], r'<>"/\|?*', 'file or directory name')
check_forbidden_characters(gr[2], r'(){}/', 'short name')
return len(gr[0]), PurePath(gr[1]), gr[2]
match = re.match(r'^(\s*)(\S*)\s*$', line)
if match is not None:
gr = match.groups()
short_name = gr[1].split('.')[0]
check_forbidden_characters(gr[1], r'<>"/\|?*', 'file or directory name')
return len(gr[0]), PurePath(gr[1]), short_name
raise ValueError('Unrecognized line %s' % line)
......@@ -79,6 +97,8 @@ def read_subtree_line(line: str, directory: str) -> Tuple[int, "filetree.FileTre
if match is None:
raise ValueError("Sub-tree line could not be parsed: {}".format(line.strip()))
spaces, type_name, variables_str, short_name = match.groups()
check_forbidden_characters(type_name, r'<>:"/\|?*', 'sub-tree name')
check_forbidden_characters(short_name, r'(){}/', 'sub-tree name')
variables = {}
if len(variables_str.strip()) != 0:
......
......@@ -27,6 +27,6 @@
{subject}.sulc_MSMAll.{space}.dscalar.nii (sulc_msm_cifti)
{subject}.thickness_MSMAll.{space}.dscalar.nii (thick_msm_cifti)
{subject}.curvature_MSMAll.{space}.dscalar.nii (curv_msm_cifti)
{subject}.BA.32k_fs_LR.dlabel.nii (BA_cifti))
{subject}.BA.32k_fs_LR.dlabel.nii (BA_cifti)
{subject}.aparc.32k_fs_LR.dlabel.nii (aparc_cifti)
{subject}.aparc.a2009s.32k_fs_LR.dlabel.nii (aparc2009_cifti)
......@@ -67,10 +67,10 @@
aparc.a2009s+aseg.mgz (aparc_a2009s_aseg)
aseg.mgz
label/
label
{hemi}h.aparc.annot (aparc.annot)
stats/
stats
aseg.stats (aseg_stats)
wmparc.stats (wmparc_stats)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment