mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
copy_asset_file() does not expand if context not given
This commit is contained in:
parent
07ddff9933
commit
db23797c57
@ -15,21 +15,22 @@ from docutils.utils import relative_path
|
|||||||
from sphinx.util.osutil import copyfile, ensuredir, walk
|
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.
|
"""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 source: The path to source file
|
||||||
:param destination: The path to destination file or directory
|
: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
|
:param renderer: The template engine
|
||||||
"""
|
"""
|
||||||
if os.path.exists(destination) and os.path.isdir(destination):
|
if os.path.exists(destination) and os.path.isdir(destination):
|
||||||
# Use source filename if destination points a directory
|
# Use source filename if destination points a directory
|
||||||
destination = os.path.join(destination, os.path.basename(source))
|
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:
|
if renderer is None:
|
||||||
msg = 'Template engine is not initialized. Failed to render %s' % source
|
msg = 'Template engine is not initialized. Failed to render %s' % source
|
||||||
raise RuntimeError(msg)
|
raise RuntimeError(msg)
|
||||||
@ -41,15 +42,16 @@ def copy_asset_file(source, destination, context={}, renderer=None):
|
|||||||
copyfile(source, destination)
|
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.
|
"""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 source: The path to source file or directory
|
||||||
:param destination: The path to destination directory
|
:param destination: The path to destination directory
|
||||||
:param excluded: The matcher to determine the given path should be copied or not
|
: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
|
:param renderer: The template engine
|
||||||
"""
|
"""
|
||||||
ensuredir(destination)
|
ensuredir(destination)
|
||||||
|
@ -50,12 +50,22 @@ def test_copy_asset_file(tmpdir):
|
|||||||
# copy template file to subdir
|
# copy template file to subdir
|
||||||
src = (tmpdir / 'asset.txt_t')
|
src = (tmpdir / 'asset.txt_t')
|
||||||
src.write_text('# {{var1}} data')
|
src.write_text('# {{var1}} data')
|
||||||
subdir = (tmpdir / 'subdir')
|
subdir1 = (tmpdir / 'subdir')
|
||||||
subdir.makedirs()
|
subdir1.makedirs()
|
||||||
|
|
||||||
copy_asset_file(src, subdir, {'var1': 'template'}, renderer)
|
copy_asset_file(src, subdir1, {'var1': 'template'}, renderer)
|
||||||
assert (subdir / 'asset.txt').exists()
|
assert (subdir1 / 'asset.txt').exists()
|
||||||
assert (subdir / 'asset.txt').text() == '# template data'
|
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
|
@with_tempdir
|
||||||
@ -95,7 +105,8 @@ def test_copy_asset(tmpdir):
|
|||||||
return ('sidebar.html' in path or 'basic.css' in path)
|
return ('sidebar.html' in path or 'basic.css' in path)
|
||||||
|
|
||||||
destdir = tmpdir / 'test3'
|
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 / 'index.rst').exists()
|
||||||
assert (destdir / 'foo.rst').exists()
|
assert (destdir / 'foo.rst').exists()
|
||||||
assert not (destdir / '_static' / 'basic.css').exists()
|
assert not (destdir / '_static' / 'basic.css').exists()
|
||||||
|
Loading…
Reference in New Issue
Block a user