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
Model registry
Operate
Environments
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
FSL
fslpy
Commits
4e87cbf9
Commit
4e87cbf9
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: Minor tweaks to make FileTreeQuery functional
parent
6e3589c0
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
fsl/utils/filetree/__init__.py
+1
-0
1 addition, 0 deletions
fsl/utils/filetree/__init__.py
fsl/utils/filetree/query.py
+34
-10
34 additions, 10 deletions
fsl/utils/filetree/query.py
with
35 additions
and
10 deletions
fsl/utils/filetree/__init__.py
+
1
−
0
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
This diff is collapsed.
Click to expand it.
fsl/utils/filetree/query.py
+
34
−
10
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
from
typing
import
Dict
,
Set
,
List
import
os.path
as
op
from
typing
import
Dict
,
Set
,
List
log
=
logging
.
getLogger
(
__name__
)
...
...
@@ -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
)
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