copy_asset_file() does not expand if context not given

This commit is contained in:
Takeshi KOMIYA 2016-06-27 12:13:16 +09:00
parent 07ddff9933
commit db23797c57
2 changed files with 26 additions and 13 deletions

View File

@ -15,21 +15,22 @@ from docutils.utils import relative_path
from sphinx.util.osutil import copyfile, ensuredir, walk
def copy_asset_file(source, destination, context={}, renderer=None):
def copy_asset_file(source, destination, context=None, renderer=None):
"""Copy an asset file to destination.
On copying, it expands the template variables if the asset is a template file.
On copying, it expands the template variables if context argument is given and
the asset is a template file.
:param source: The path to source file
:param destination: The path to destination file or directory
:param context: The template variables
:param context: The template variables. If not given, template files are simply copied
:param renderer: The template engine
"""
if os.path.exists(destination) and os.path.isdir(destination):
# Use source filename if destination points a directory
destination = os.path.join(destination, os.path.basename(source))
if source.lower().endswith('_t'):
if source.lower().endswith('_t') and context:
if renderer is None:
msg = 'Template engine is not initialized. Failed to render %s' % source
raise RuntimeError(msg)
@ -41,15 +42,16 @@ def copy_asset_file(source, destination, context={}, renderer=None):
copyfile(source, destination)
def copy_asset(source, destination, excluded=lambda path: False, context={}, renderer=None):
def copy_asset(source, destination, excluded=lambda path: False, context=None, renderer=None):
"""Copy asset files to destination recursively.
On copying, it expands the template variables if the asset is a template file.
On copying, it expands the template variables if context argument is given and
the asset is a template file.
:param source: The path to source file or directory
:param destination: The path to destination directory
:param excluded: The matcher to determine the given path should be copied or not
:param context: The template variables
:param context: The template variables. If not given, template files are simply copied
:param renderer: The template engine
"""
ensuredir(destination)

View File

@ -50,12 +50,22 @@ def test_copy_asset_file(tmpdir):
# copy template file to subdir
src = (tmpdir / 'asset.txt_t')
src.write_text('# {{var1}} data')
subdir = (tmpdir / 'subdir')
subdir.makedirs()
subdir1 = (tmpdir / 'subdir')
subdir1.makedirs()
copy_asset_file(src, subdir, {'var1': 'template'}, renderer)
assert (subdir / 'asset.txt').exists()
assert (subdir / 'asset.txt').text() == '# template data'
copy_asset_file(src, subdir1, {'var1': 'template'}, renderer)
assert (subdir1 / 'asset.txt').exists()
assert (subdir1 / 'asset.txt').text() == '# template data'
# copy template file without context
src = (tmpdir / 'asset.txt_t')
subdir2 = (tmpdir / 'subdir2')
subdir2.makedirs()
copy_asset_file(src, subdir2)
assert not (subdir2 / 'asset.txt').exists()
assert (subdir2 / 'asset.txt_t').exists()
assert (subdir2 / 'asset.txt_t').text() == '# {{var1}} data'
@with_tempdir
@ -95,7 +105,8 @@ def test_copy_asset(tmpdir):
return ('sidebar.html' in path or 'basic.css' in path)
destdir = tmpdir / 'test3'
copy_asset(source, destdir, excluded, renderer=renderer)
copy_asset(source, destdir, excluded,
context=dict(var1='bar', var2='baz'), renderer=renderer)
assert (destdir / 'index.rst').exists()
assert (destdir / 'foo.rst').exists()
assert not (destdir / '_static' / 'basic.css').exists()