Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Evan Edmond
fslpy
Commits
346656d1
Commit
346656d1
authored
5 years ago
by
Michiel Cottaar
Browse files
Options
Downloads
Patches
Plain Diff
ENH: replaced ambiguous exists method with on_disk and defines
parent
7114ce1a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
fsl/utils/filetree/filetree.py
+50
-23
50 additions, 23 deletions
fsl/utils/filetree/filetree.py
tests/test_filetree/test_read.py
+15
-15
15 additions, 15 deletions
tests/test_filetree/test_read.py
with
65 additions
and
38 deletions
fsl/utils/filetree/filetree.py
+
50
−
23
View file @
346656d1
from
pathlib
import
Path
,
PurePath
from
typing
import
Tuple
,
Optional
,
List
,
Dict
,
Any
,
Set
from
typing
import
Tuple
,
Optional
,
Dict
,
Any
,
Set
from
copy
import
deepcopy
from
.
import
parse
import
pickle
import
os.path
as
op
from
.
import
utils
from
fsl.utils.deprecated
import
deprecated
class
MissingVariable
(
KeyError
):
...
...
@@ -257,12 +258,38 @@ class FileTree(object):
raise
IOError
(
"
Pickle file did not contain %s object
"
%
cls
)
return
res
def
exists
(
self
,
short_names
,
on_disk
=
False
,
error
=
False
,
glob_vars
=
()):
def
defines
(
self
,
short_names
,
error
=
False
):
"""
Checks whether templates are defined for all the `short_names`
:param short_names: sequence of expected short names to exist in the tree
:param error: if True raises ValueError if any `short_names` are undefined
:return: True if all are defined, False otherwise
:raise: ValueError if `error` is set to True and any template is missing
"""
if
isinstance
(
short_names
,
str
):
short_names
=
(
short_names
,
)
def
single_test
(
short_name
):
try
:
self
.
_get_template_tree
(
short_name
)
except
KeyError
:
return
True
return
False
missing
=
tuple
(
name
for
name
in
short_names
if
single_test
(
name
))
if
len
(
missing
)
>
0
:
if
error
:
raise
ValueError
(
"
Provided Filetree is missing template definitions for {}
"
.
format
(
missing
))
return
False
return
True
def
on_disk
(
self
,
short_names
,
error
=
False
,
glob_vars
=
()):
"""
Checks whether
the short_names are defined in the tree (and optional exist on the disk)
Checks whether
at least one file exists for every file in `short_names`
:param short_names: list of expected short names to exist in the tree
:param on_disk: if True checks whether the files exist on disk
:param error: if True raises a helpful error when the check fails
:param glob_vars: sequence of undefined variables that can take any possible values when looking for matches on the disk
If `glob_vars` contains any defined variables, it will be ignored.
...
...
@@ -271,34 +298,34 @@ class FileTree(object):
- ValueError if error is set and the tree is incomplete
- IOError if error is set and any files are missing from the disk
"""
self
.
defines
(
short_names
,
error
=
error
)
if
isinstance
(
short_names
,
str
):
short_names
=
(
short_names
,
)
def
single_test
(
short_name
)
:
try
:
self
.
_get_template_tree
(
short_name
)
except
KeyE
rror
:
r
eturn
Tru
e
try
:
missing
=
tuple
(
name
for
name
in
short_names
if
len
(
self
.
get_all
(
name
,
glob_vars
=
glob_vars
))
==
0
)
except
KeyError
:
if
e
rror
:
r
ais
e
return
False
missing
=
tuple
(
name
for
name
in
short_names
if
single_test
(
name
))
if
len
(
missing
)
>
0
:
if
error
:
raise
Value
Error
(
"
Provided Filetree is missing file definitions
for {}
"
.
format
(
missing
))
raise
IO
Error
(
"
Failed to find any files on disk
for {}
"
.
format
(
missing
))
return
False
if
on_disk
:
try
:
missing
=
tuple
(
name
for
name
in
short_names
if
len
(
self
.
get_all
(
name
,
glob_vars
=
glob_vars
))
==
0
)
except
KeyError
:
if
error
:
raise
return
False
if
len
(
missing
)
>
0
:
if
error
:
raise
IOError
(
"
Failed to find any files existing for {}
"
.
format
(
missing
))
return
False
return
True
@deprecated
(
rin
=
'
2.4
'
,
msg
=
'
Use FileTree.defines or FileTree.on_disk instead
'
)
def
exists
(
self
,
short_names
,
on_disk
=
False
,
error
=
False
,
glob_vars
=
()):
"""
Deprecated in favor of :meth:`on_disk` and :meth:`defines`.
"""
if
on_disk
:
return
self
.
on_disk
(
short_names
,
error
=
error
,
glob_vars
=
glob_vars
)
else
:
return
self
.
defines
(
short_names
,
error
=
error
,
glob_vars
=
glob_vars
)
@classmethod
def
read
(
cls
,
tree_name
:
str
,
directory
=
'
.
'
,
**
variables
)
->
"
FileTree
"
:
"""
...
...
This diff is collapsed.
Click to expand it.
tests/test_filetree/test_read.py
+
15
−
15
View file @
346656d1
...
...
@@ -34,17 +34,17 @@ def test_complicated_tree():
same_path
(
tree
.
update
(
hemi
=
'
L
'
).
get
(
'
T1w_32k/white
'
),
L_white
)
same_path
(
tree
.
sub_trees
[
'
T1w_32k
'
].
update
(
hemi
=
'
L
'
).
get
(
'
white
'
),
L_white
)
assert
tree
.
exist
s
((
'
T1w_32k/white
'
,
))
assert
tree
.
exist
s
(
'
T1w_32k/white
'
)
assert
not
tree
.
exist
s
((
'
T1w_32k/white_misspelled
'
,
))
assert
not
tree
.
exist
s
((
'
T1w_32k/white
'
,
'
T1w_32k/white_misspelled
'
,
))
assert
not
tree
.
exist
s
((
'
T1w_32k_err/white
'
,
))
assert
not
tree
.
exist
s
((
'
../test
'
))
assert
tree
.
define
s
((
'
T1w_32k/white
'
,
))
assert
tree
.
define
s
(
'
T1w_32k/white
'
)
assert
not
tree
.
define
s
((
'
T1w_32k/white_misspelled
'
,
))
assert
not
tree
.
define
s
((
'
T1w_32k/white
'
,
'
T1w_32k/white_misspelled
'
,
))
assert
not
tree
.
define
s
((
'
T1w_32k_err/white
'
,
))
assert
not
tree
.
define
s
((
'
../test
'
))
with
pytest
.
raises
(
ValueError
):
assert
not
tree
.
exist
s
((
'
../test
'
),
error
=
True
)
assert
not
tree
.
define
s
((
'
../test
'
),
error
=
True
)
with
pytest
.
raises
(
ValueError
):
tree
.
exist
s
((
'
T1w_32k_err/white
'
,
),
error
=
True
)
assert
tree
.
exist
s
((
'
T1w_32k/white
'
,
),
error
=
True
)
tree
.
define
s
((
'
T1w_32k_err/white
'
,
),
error
=
True
)
assert
tree
.
define
s
((
'
T1w_32k/white
'
,
),
error
=
True
)
def
test_parent_tree
():
...
...
@@ -110,14 +110,14 @@ def test_custom_tree():
assert
vars
==
tree
.
extract_variables
(
'
sub_file
'
,
filename
)
assert
{
'
opt
'
:
None
}
==
tree
.
extract_variables
(
'
sub_file
'
,
tree
.
get
(
'
sub_file
'
))
assert
tree
.
exists
((
'
sub_file
'
,
'
opt_file
'
),
error
=
True
,
on_disk
=
True
,
glob_vars
=
[
'
opt
'
])
assert
tree
.
exists
((
'
sub_file
'
,
'
opt_file
'
),
on_disk
=
True
,
glob_vars
=
[
'
opt
'
])
assert
not
tree
.
exists
((
'
sub_file
'
,
'
opt_file
'
),
error
=
False
,
on_disk
=
True
)
assert
tree
.
on_disk
((
'
sub_file
'
,
'
opt_file
'
),
error
=
True
,
glob_vars
=
[
'
opt
'
])
assert
tree
.
on_disk
((
'
sub_file
'
,
'
opt_file
'
),
glob_vars
=
[
'
opt
'
])
assert
not
tree
.
on_disk
((
'
sub_file
'
,
'
opt_file
'
),
error
=
False
)
with
pytest
.
raises
(
KeyError
):
assert
tree
.
exists
((
'
sub_file
'
,
'
opt_file
'
),
error
=
True
,
on_disk
=
True
)
assert
not
tree
.
update
(
opt
=
'
test2
'
).
exists
((
'
sub_file
'
,
'
opt_file
'
)
,
on_disk
=
True
)
assert
tree
.
on_disk
((
'
sub_file
'
,
'
opt_file
'
),
error
=
True
)
assert
not
tree
.
update
(
opt
=
'
test2
'
).
on_disk
((
'
sub_file
'
,
'
opt_file
'
))
with
pytest
.
raises
(
IOError
):
tree
.
update
(
opt
=
'
test2
'
).
exists
((
'
sub_file
'
,
'
opt_file
'
),
on_disk
=
True
,
error
=
True
)
tree
.
update
(
opt
=
'
test2
'
).
on_disk
((
'
sub_file
'
,
'
opt_file
'
),
error
=
True
)
assert
tree
.
template_variables
()
==
{
'
opt
'
}
assert
tree
.
template_variables
(
optional
=
False
)
==
{
'
opt
'
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment