mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add sphinx.util.fileutil.copy_asset_file()
This commit is contained in:
parent
f72879f071
commit
17cd06f237
@ -1069,6 +1069,7 @@ class SerializingHTMLBuilder(StandaloneHTMLBuilder):
|
||||
self.theme = None # no theme necessary
|
||||
self.templates = None # no template bridge necessary
|
||||
self.init_translator_class()
|
||||
self.init_templates()
|
||||
self.init_highlighter()
|
||||
|
||||
def get_target_uri(self, docname, typ=None):
|
||||
|
@ -18,7 +18,7 @@ import posixpath
|
||||
import traceback
|
||||
import unicodedata
|
||||
from os import path
|
||||
from codecs import open, BOM_UTF8
|
||||
from codecs import BOM_UTF8
|
||||
from collections import deque
|
||||
|
||||
from six import iteritems, text_type, binary_type
|
||||
@ -32,6 +32,7 @@ import jinja2
|
||||
import sphinx
|
||||
from sphinx.errors import PycodeError, SphinxParallelError, ExtensionError
|
||||
from sphinx.util.console import strip_colors
|
||||
from sphinx.util.fileutil import copy_asset_file
|
||||
from sphinx.util.osutil import fs_encoding
|
||||
|
||||
# import other utilities; partly for backwards compatibility, so don't
|
||||
@ -158,16 +159,7 @@ def copy_static_entry(source, targetdir, builder, context={},
|
||||
if matcher(relpath):
|
||||
return
|
||||
if path.isfile(source):
|
||||
target = path.join(targetdir, path.basename(source))
|
||||
if source.lower().endswith('_t') and builder.templates:
|
||||
# templated!
|
||||
fsrc = open(source, 'r', encoding='utf-8')
|
||||
fdst = open(target[:-2], 'w', encoding='utf-8')
|
||||
fdst.write(builder.templates.render_string(fsrc.read(), context))
|
||||
fsrc.close()
|
||||
fdst.close()
|
||||
else:
|
||||
copyfile(source, target)
|
||||
copy_asset_file(source, targetdir, context, builder.templates)
|
||||
elif path.isdir(source):
|
||||
if not path.isdir(targetdir):
|
||||
os.mkdir(targetdir)
|
||||
|
39
sphinx/util/fileutil.py
Normal file
39
sphinx/util/fileutil.py
Normal file
@ -0,0 +1,39 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
sphinx.util.fileutil
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
File utility functions for Sphinx.
|
||||
|
||||
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
import os
|
||||
import codecs
|
||||
from sphinx.util.osutil import copyfile
|
||||
|
||||
|
||||
def copy_asset_file(source, destination, context={}, renderer=None):
|
||||
"""Copy an asset file to destination.
|
||||
|
||||
On copying, it expands the template variables if 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 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 renderer is None:
|
||||
msg = 'Template engine is not initialized. Failed to render %s' % source
|
||||
raise RuntimeError(msg)
|
||||
|
||||
with codecs.open(source, 'r', encoding='utf-8') as fsrc:
|
||||
with codecs.open(destination[:-2], 'w', encoding='utf-8') as fdst:
|
||||
fdst.write(renderer.render_string(fsrc.read(), context))
|
||||
else:
|
||||
copyfile(source, destination)
|
58
tests/test_util_fileutil.py
Normal file
58
tests/test_util_fileutil.py
Normal file
@ -0,0 +1,58 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
test_util_fileutil
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Tests sphinx.util.fileutil functions.
|
||||
|
||||
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
from sphinx.util.fileutil import copy_asset_file
|
||||
from sphinx.jinja2glue import BuiltinTemplateLoader
|
||||
|
||||
from mock import Mock
|
||||
from util import with_tempdir
|
||||
|
||||
|
||||
class DummyTemplateLoader(BuiltinTemplateLoader):
|
||||
def __init__(self):
|
||||
BuiltinTemplateLoader.__init__(self)
|
||||
builder = Mock()
|
||||
builder.config.templates_path = []
|
||||
builder.app.translater = None
|
||||
self.init(builder)
|
||||
|
||||
|
||||
@with_tempdir
|
||||
def test_copy_asset_file(tmpdir):
|
||||
renderer = DummyTemplateLoader()
|
||||
|
||||
# copy normal file
|
||||
src = (tmpdir / 'asset.txt')
|
||||
src.write_text('# test data')
|
||||
dest = (tmpdir / 'output.txt')
|
||||
|
||||
copy_asset_file(src, dest)
|
||||
assert dest.exists()
|
||||
assert src.text() == dest.text()
|
||||
|
||||
# copy template file
|
||||
src = (tmpdir / 'asset.txt_t')
|
||||
src.write_text('# {{var1}} data')
|
||||
dest = (tmpdir / 'output.txt_t')
|
||||
|
||||
copy_asset_file(src, dest, {'var1': 'template'}, renderer)
|
||||
assert not dest.exists()
|
||||
assert (tmpdir / 'output.txt').exists()
|
||||
assert (tmpdir / 'output.txt').text() == '# template data'
|
||||
|
||||
# copy template file to subdir
|
||||
src = (tmpdir / 'asset.txt_t')
|
||||
src.write_text('# {{var1}} data')
|
||||
subdir = (tmpdir / 'subdir')
|
||||
subdir.makedirs()
|
||||
|
||||
copy_asset_file(src, subdir, {'var1': 'template'}, renderer)
|
||||
assert (subdir / 'asset.txt').exists()
|
||||
assert (subdir / 'asset.txt').text() == '# template data'
|
Loading…
Reference in New Issue
Block a user