Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.recipe.template
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Xavier Thompson
slapos.recipe.template
Commits
776b21d6
Commit
776b21d6
authored
Jan 17, 2017
by
Vincent Pelletier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
recipe.template.jinja2: Add render-once support.
To be used for zope's inituser file, for example.
parent
dbb27169
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
0 deletions
+68
-0
slapos/recipe/template/README.jinja2.txt
slapos/recipe/template/README.jinja2.txt
+62
-0
slapos/recipe/template/jinja2_template.py
slapos/recipe/template/jinja2_template.py
+6
-0
No files found.
slapos/recipe/template/README.jinja2.txt
View file @
776b21d6
...
@@ -116,6 +116,9 @@ Optional:
...
@@ -116,6 +116,9 @@ Optional:
Mode, in octal representation (no need for 0-prefix) to set output file to.
Mode, in octal representation (no need for 0-prefix) to set output file to.
This is applied before storing anything in output file.
This is applied before storing anything in output file.
``once``
Path of a marker file to prevents rendering altogether.
``extensions``
``extensions``
Jinja2 extensions to enable when rendering the template,
Jinja2 extensions to enable when rendering the template,
whitespace-separated. By default, none is loaded.
whitespace-separated. By default, none is loaded.
...
@@ -525,3 +528,62 @@ Let's just use ``buildout.cfg`` using this egg::
...
@@ -525,3 +528,62 @@ Let's just use ``buildout.cfg`` using this egg::
>>> cat('foo')
>>> cat('foo')
foobar
foobar
Avoiding file re-creation
~~~~~~~~~~~~~~~~~~~~~~~~~
Normally, each time the section is installed/updated the file gets
re-generated. This may be undesirable in some cases.
``once`` allows specifying a marker file, which when present prevents template
rendering.
>>> import os
>>> write('buildout.cfg',
... '''
... [buildout]
... parts = template
...
... [template]
... recipe = slapos.recipe.template:jinja2
... template = inline:dummy
... rendered = foo_once
... once = foo_flag
... ''')
>>> print system(join('bin', 'buildout')),
Uninstalling template.
Installing template.
Template was rendered::
>>> cat('foo_once')
dummy
And canary exists::
>>> os.path.exists('foo_flag')
True
Remove rendered file and re-render::
>>> import os
>>> os.unlink('foo_once')
>>> open('buildout.cfg', 'a').writelines(['extra = useless'])
>>> print system(join('bin', 'buildout')),
Uninstalling template.
Installing template.
Template was not rendered::
>>> os.path.exists('foo_once')
False
Removing the canary allows template to be re-rendered::
>>> os.unlink('foo_flag')
>>> open('buildout.cfg', 'a').writelines(['moreextra = still useless'])
>>> print system(join('bin', 'buildout')),
Uninstalling template.
Installing template.
>>> cat('foo_once')
dummy
slapos/recipe/template/jinja2_template.py
View file @
776b21d6
...
@@ -209,8 +209,11 @@ class Recipe(object):
...
@@ -209,8 +209,11 @@ class Recipe(object):
self
.
template
=
env
.
template_class
.
from_code
(
env
,
self
.
template
=
env
.
template_class
.
from_code
(
env
,
env
.
compile
(
source
,
filename
=
template
),
env
.
compile
(
source
,
filename
=
template
),
env
.
make_globals
(
None
),
None
)
env
.
make_globals
(
None
),
None
)
self
.
once
=
options
.
get
(
'once'
)
def
install
(
self
):
def
install
(
self
):
if
self
.
once
and
os
.
path
.
exists
(
self
.
once
):
return
# Unlink any existing file, so umask is always applied.
# Unlink any existing file, so umask is always applied.
try
:
try
:
os
.
unlink
(
self
.
rendered
)
os
.
unlink
(
self
.
rendered
)
...
@@ -228,6 +231,9 @@ class Recipe(object):
...
@@ -228,6 +231,9 @@ class Recipe(object):
self
.
mode
),
'w'
)
as
out
:
self
.
mode
),
'w'
)
as
out
:
out
.
write
(
self
.
template
.
render
(
**
self
.
context
)
out
.
write
(
self
.
template
.
render
(
**
self
.
context
)
.
encode
(
self
.
encoding
))
.
encode
(
self
.
encoding
))
if
self
.
once
:
open
(
self
.
once
,
'w'
).
close
()
return
return
self
.
rendered
return
self
.
rendered
update
=
install
update
=
install
...
...
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