Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FSL
fslpy
Commits
fa643260
Commit
fa643260
authored
Aug 15, 2019
by
Michiel Cottaar
Browse files
ENH: look for sub-trees in parent tree directory
parent
f9bde009
Changes
4
Hide whitespace changes
Inline
Side-by-side
fsl/utils/filetree/filetree.py
View file @
fa643260
...
...
@@ -360,6 +360,7 @@ class FileTree(object):
filename
=
parse
.
search_tree
(
tree_name
)
tree_name
=
op
.
splitext
(
op
.
basename
(
filename
))[
0
]
filename
=
Path
(
filename
)
dirname
=
str
(
filename
.
parent
)
templates
=
{}
nspaces_level
=
[]
...
...
@@ -392,7 +393,8 @@ class FileTree(object):
else
:
sub_dir
=
current_filename
.
parents
[
0
]
_
,
sub_tree
,
short_name
=
parse
.
read_subtree_line
(
line
,
sub_dir
)
with
parse
.
extra_tree_dirs
([
dirname
]):
_
,
sub_tree
,
short_name
=
parse
.
read_subtree_line
(
line
,
sub_dir
)
if
short_name
in
sub_trees
:
raise
ValueError
(
"Name of sub_tree {short_name} used multiple times in {tree_name}.tree"
.
format
(
**
locals
()))
...
...
fsl/utils/filetree/parse.py
View file @
fa643260
import
glob
import
os.path
as
op
from
.
import
filetree
from
contextlib
import
contextmanager
from
pathlib
import
PurePath
from
typing
import
Tuple
,
List
import
re
...
...
@@ -9,6 +10,25 @@ import re
tree_directories
=
[
'.'
,
op
.
join
(
op
.
split
(
__file__
)[
0
],
'trees'
)]
@
contextmanager
def
extra_tree_dirs
(
extra_dirs
):
"""Temporarily insert ``extra_dirs`` to the beginning of :attr:`tree_directories`.
:arg extra_dirs: Sequence of additional tree file directories to search.
"""
global
tree_directories
old_tree_directories
=
list
(
tree_directories
)
tree_directories
=
list
(
extra_dirs
)
+
list
(
tree_directories
)
try
:
yield
finally
:
tree_directories
=
old_tree_directories
def
search_tree
(
name
:
str
)
->
str
:
"""
Searches for the file defining the specific tree
...
...
tests/test_filetree/local_parent.tree
0 → 100644
View file @
fa643260
->format (format)
\ No newline at end of file
tests/test_filetree/test_read.py
View file @
fa643260
...
...
@@ -4,6 +4,7 @@ from pathlib import PurePath
import
os.path
as
op
import
pytest
from
glob
import
glob
import
os
def
same_path
(
p1
,
p2
):
...
...
@@ -161,3 +162,14 @@ def test_extract_vars_but():
assert
{
'p1'
:
'opt_file'
,
'p2'
:
'test'
}
==
tree
.
update
(
p1
=
'opt_file'
).
extract_variables
(
'fn'
,
fn
)
assert
{
'p1'
:
'opt'
,
'p2'
:
'file_test'
}
==
tree
.
update
(
p1
=
'opt'
).
extract_variables
(
'fn'
,
fn
)
assert
{
'p1'
:
'opt_{p3}'
,
'p2'
:
'test'
,
'p3'
:
'file'
}
==
tree
.
update
(
p1
=
'opt_{p3}'
).
extract_variables
(
'fn'
,
fn
)
def
test_read_local_sub_children
():
"""
Look for trees defined in the same folder as the test directory
"""
directory
=
op
.
split
(
__file__
)[
0
]
if
op
.
abspath
(
op
.
curdir
)
==
op
.
abspath
(
directory
):
# ensure current directory is not the test directory, which would cause the test to be too easy
os
.
chdir
(
'..'
)
filetree
.
FileTree
.
read
(
op
.
join
(
directory
,
'local_parent.tree'
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment