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
Evan Edmond
fslpy
Commits
4e87cbf9
Commit
4e87cbf9
authored
Jan 18, 2019
by
Paul McCarthy
🚵
Browse files
ENH: Minor tweaks to make FileTreeQuery functional
parent
6e3589c0
Changes
2
Show whitespace changes
Inline
Side-by-side
fsl/utils/filetree/__init__.py
View file @
4e87cbf9
...
...
@@ -278,3 +278,4 @@ __author__ = 'Michiel Cottaar <Michiel.Cottaar@ndcn.ox.ac.uk>'
from
.filetree
import
FileTree
,
register_tree
,
MissingVariable
from
.parse
import
tree_directories
,
list_all_trees
from
.query
import
FileTreeQuery
fsl/utils/filetree/query.py
View file @
4e87cbf9
#!/usr/bin/env python
#
# query.py -
# query.py -
The FileTreeQuery class
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
# Author: Michiel Cottaar <michiel.cottaar@.ndcn.ox.ac.uk>
#
"""
"""
import
logging
import
collections
import
os.path
as
op
from
typing
import
Dict
,
Set
,
List
...
...
@@ -21,8 +25,8 @@ class FileTreeQuery(object):
"""
"""
self
.
__tree
=
tree
self
.
__matches
=
self
.
__tree
.
scan
()
self
.
__variables
=
Match
.
allVariables
(
self
.
__
tree
,
self
.
__matches
)
self
.
__matches
=
Match
.
scan
(
tree
)
self
.
__variables
=
Match
.
allVariables
(
tree
,
self
.
__matches
)
def
variables
(
self
)
->
Dict
[
str
,
Set
]:
...
...
@@ -34,7 +38,8 @@ class FileTreeQuery(object):
def
query
(
self
,
**
variables
)
->
List
[
str
]:
"""
"""Return all ``Match`` objects which match the given set of
``variable=value`` arguments.
"""
hits
=
[]
...
...
@@ -52,14 +57,16 @@ class Match(object):
"""
@
staticmethod
def
allVariables
(
tree
,
matches
)
->
Dict
(
str
,
Set
):
"""
def
allVariables
(
tree
,
matches
)
->
Dict
[
str
,
Set
]:
"""Returns a dict of ``{ variable : [values] }`` mappings
containing all variables and their possible values present
in the given list of ``Match`` objects.
"""
allvars
=
collections
.
defaultdict
(
set
)
for
m
in
matches
:
for
var
,
val
in
m
.
variables
.
items
():
allvars
[
var
].
update
(
val
)
allvars
[
var
].
add
(
val
)
return
allvars
...
...
@@ -74,11 +81,20 @@ class Match(object):
matches
=
[]
for
template
in
tree
.
templates
:
for
filename
in
tree
.
get_all
(
template
,
glob_vars
=
'all'
):
if
not
op
.
isfile
(
filename
):
continue
variables
=
tree
.
extract_variables
(
template
,
filename
)
match
=
Match
(
filename
,
template
,
variables
)
matches
.
append
(
match
)
variables
=
{
var
:
val
for
var
,
val
in
variables
.
items
()
if
val
is
not
None
}
matches
.
append
(
Match
(
filename
,
template
,
variables
))
for
tree_name
,
sub_tree
in
tree
.
sub_trees
:
matches
.
extend
(
Match
.
scan
(
sub_tree
))
return
matches
...
...
@@ -93,3 +109,11 @@ class Match(object):
self
.
filename
=
filename
self
.
short_name
=
short_name
self
.
variables
=
dict
(
variables
)
def
__repr__
(
self
):
return
self
.
filename
def
__str__
(
self
):
return
repr
(
self
)
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