Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
gridtk
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
7
Issues
7
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bob
gridtk
Commits
271eeb90
Commit
271eeb90
authored
Apr 16, 2019
by
Amir MOHAMMADI
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Automatic injection of bob.extension.rc to jinja variables
parent
43112b48
Pipeline
#29298
passed with stage
in 4 minutes and 2 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
8 deletions
+19
-8
doc/generate.rst
doc/generate.rst
+10
-0
gridtk/generator.py
gridtk/generator.py
+9
-8
No files found.
doc/generate.rst
View file @
271eeb90
...
...
@@ -189,6 +189,16 @@ configuration files and the aggregation would look like the following:
$ jgen variables.yaml template.py 'output-dir/{{ name }}-{{ version }}.py' run.sh 'output-dir/run.sh'
Automatic injection of variables
--------------------------------
Sometimes you want to use variables that are user specific in your jinja templates; For
example, a temp directory that can be different for other users. To allow this, jgen
automatically injects ``bob.extension.rc`` (see :ref:`bob.extension.rc`) into your
variables. Then, you can access ``bob.extension.rc`` using something like:
``rc.variable_name`` to access variables from it in your jinja templates.
.. Place your references here:
.. _yaml: https://en.wikipedia.org/wiki/YAML
.. _jinja2: http://jinja.pocoo.org/docs/
gridtk/generator.py
View file @
271eeb90
...
...
@@ -3,24 +3,24 @@
'''Utilities for generating configurations for running experiments in batch'''
from
bob.extension
import
rc
import
collections
import
itertools
import
yaml
import
jinja2
class
_OrderedD
ict
(
collections
.
OrderedDict
):
class
d
ict
(
collections
.
OrderedDict
):
"""An OrderedDict class that can be compared.
This is to avoid sort errors (in Python 3) that happen in jinja internally.
"""
def
__lt__
(
self
,
other
):
return
id
(
self
)
<
id
(
other
)
def
_ordered_load
(
stream
,
Loader
=
yaml
.
Loader
,
object_pairs_hook
=
_OrderedD
ict
):
object_pairs_hook
=
d
ict
):
'''Loads the contents of the YAML stream into :py:class:`collections.OrderedDict`'s
See: https://stackoverflow.com/questions/5121931/in-python-how-can-you-load-yaml-mappings-as-ordereddicts
...
...
@@ -131,8 +131,8 @@ def expand(data):
# separates "unique" objects from the ones we have to iterate
# pre-assemble return dictionary
iterables
=
_OrderedD
ict
()
unique
=
_OrderedD
ict
()
iterables
=
d
ict
()
unique
=
d
ict
()
for
key
,
value
in
data
.
items
():
if
isinstance
(
value
,
list
)
and
not
key
.
startswith
(
'_'
):
iterables
[
key
]
=
value
...
...
@@ -141,7 +141,7 @@ def expand(data):
# generates all possible combinations of iterables
for
values
in
itertools
.
product
(
*
iterables
.
values
()):
retval
=
_OrderedD
ict
(
unique
)
retval
=
d
ict
(
unique
)
keys
=
list
(
iterables
.
keys
())
retval
.
update
(
dict
(
zip
(
keys
,
values
)))
yield
retval
...
...
@@ -176,6 +176,7 @@ def generate(variables, template):
env
=
jinja2
.
Environment
(
undefined
=
jinja2
.
StrictUndefined
)
for
c
in
expand
(
variables
):
c
[
'rc'
]
=
rc
yield
env
.
from_string
(
template
).
render
(
c
)
...
...
@@ -206,5 +207,5 @@ def aggregate(variables, template):
'''
env
=
jinja2
.
Environment
(
undefined
=
jinja2
.
StrictUndefined
)
d
=
{
'cfgset'
:
list
(
expand
(
variables
))}
d
=
{
'cfgset'
:
list
(
expand
(
variables
))
,
'rc'
:
rc
}
return
env
.
from_string
(
template
).
render
(
d
)
Write
Preview
Markdown
is supported
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