本文整理汇总了Python中sphinx.util.osutil.copyfile函数的典型用法代码示例。如果您正苦于以下问题:Python copyfile函数的具体用法?Python copyfile怎么用?Python copyfile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copyfile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: copy_static
def copy_static(app, exception):
if app.builder.format != 'html' or exception:
return
for filename in ['jsdemo.js', 'jsdemo.css']:
src = os.path.join(os.path.abspath(os.path.dirname(__file__)), filename)
dst = os.path.join(app.builder.outdir, '_static', filename)
copyfile(src, dst)
示例2: install_lightbox_static_files
def install_lightbox_static_files(app):
source_static_path = os.path.join(app.builder.srcdir, '_static')
target_static_path = os.path.join(app.builder.outdir, '_static')
source_lightbox_path = os.path.join(source_static_path, 'lightbox2')
target_lightbox_path = os.path.join(target_static_path, 'lightbox2')
relative_file_paths = []
for root, _, file_names in os.walk(source_lightbox_path):
for file_name in file_names:
absolute_file_path = os.path.join(root, file_name)
relative_file_path = os.path.relpath(
absolute_file_path,
source_static_path,
)
relative_file_paths.append(relative_file_path)
if os.path.exists(target_lightbox_path):
shutil.rmtree(target_lightbox_path)
for relative_file_path in app.builder.status_iterator(
relative_file_paths,
'installing lightbox files... ',
brown,
len(relative_file_paths),
):
source_path = os.path.join(source_static_path, relative_file_path)
target_path = os.path.join(target_static_path, relative_file_path)
target_directory = os.path.dirname(target_path)
if not os.path.exists(target_directory):
ensuredir(target_directory)
copyfile(source_path, target_path)
if relative_file_path.endswith('.js'):
app.add_javascript(relative_file_path)
elif relative_file_path.endswith('.css'):
app.add_stylesheet(relative_file_path)
示例3: copy_static_entry
def copy_static_entry(source, targetdir, builder, context={},
exclude_matchers=(), level=0):
"""Copy a HTML builder static_path entry from source to targetdir.
Handles all possible cases of files, directories and subdirectories.
"""
if exclude_matchers:
relpath = relative_path(builder.srcdir, source)
for matcher in exclude_matchers:
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)
elif path.isdir(source):
if level == 0:
for entry in os.listdir(source):
if entry.startswith('.'):
continue
copy_static_entry(path.join(source, entry), targetdir,
builder, context, level=1,
exclude_matchers=exclude_matchers)
else:
target = path.join(targetdir, path.basename(source))
if path.exists(target):
shutil.rmtree(target)
shutil.copytree(source, target)
示例4: handle_page
def handle_page(self, pagename, ctx, templatename='page.html',
outfilename=None, event_arg=None):
ctx['current_page_name'] = pagename
self.add_sidebars(pagename, ctx)
if not outfilename:
outfilename = path.join(self.outdir,
os_path(pagename) + self.out_suffix)
self.app.emit('html-page-context', pagename, templatename,
ctx, event_arg)
ensuredir(path.dirname(outfilename))
f = open(outfilename, 'wb')
try:
self.implementation.dump(ctx, f, 2)
finally:
f.close()
# if there is a source file, copy the source file for the
# "show source" link
if ctx.get('sourcename'):
source_name = path.join(self.outdir, '_sources',
os_path(ctx['sourcename']))
ensuredir(path.dirname(source_name))
copyfile(self.env.doc2path(pagename), source_name)
示例5: install_backend_static_files
def install_backend_static_files(app, env):
STATICS_DIR_PATH = os.path.join(app.builder.outdir, STATICS_DIR_NAME)
dest_path = os.path.join(STATICS_DIR_PATH, 'sphinxcontrib-images',
app.sphinxcontrib_images_backend.__class__.__name__)
files_to_copy = app.sphinxcontrib_images_backend.STATIC_FILES
for source_file_path in app.builder.status_iterator(
files_to_copy,
'Copying static files for sphinxcontrib-images...',
brown, len(files_to_copy)):
dest_file_path = os.path.join(dest_path, source_file_path)
if not os.path.exists(os.path.dirname(dest_file_path)):
ensuredir(os.path.dirname(dest_file_path))
source_file_path = os.path.join(os.path.dirname(
sys.modules[app.sphinxcontrib_images_backend.__class__.__module__].__file__),
source_file_path)
copyfile(source_file_path, dest_file_path)
if dest_file_path.endswith('.js'):
app.add_javascript(os.path.relpath(dest_file_path, STATICS_DIR_PATH))
elif dest_file_path.endswith('.css'):
app.add_stylesheet(os.path.relpath(dest_file_path, STATICS_DIR_PATH))
示例6: copy_stylesheet
def copy_stylesheet(app, exception=None):
on_rtd = (os.environ.get('READTHEDOCS', None) == 'True')
if not on_rtd and (app.builder.name != 'html' or exception):
return
# TODO: change _static to variable from config (something like that exists?)
if on_rtd:
base_path = os.path.join(app.builder.srcdir, '_static')
else:
base_path = os.path.join(app.builder.outdir, '_static')
path = os.path.abspath(os.path.join(base_path, 'fancybox'))
if not os.path.exists(path):
os.makedirs(path)
app.info('Copying fancybox stylesheets... ', nonl=True)
for file in CSS_FILES:
copyfile(
os.path.join(os.path.dirname(__file__), file),
os.path.join(base_path, file)
)
app.info('done')
app.info('Copying fancybox javascript... ', nonl=True)
for file in JS_FILES:
copyfile(
os.path.join(os.path.dirname(__file__), file),
os.path.join(base_path, file)
)
app.info('done')
示例7: copy_assets
def copy_assets(app, exception):
""" Copy asset files to the output """
builders = ['html', 'singlehtml', 'dirhtml',
'readthedocs', 'readthedocsdirhtml',
'readthedocssinglehtml', 'readthedocssinglehtmllocalmedia',
'spelling']
builders.extend(app.config['sphinx_tabs_valid_builders'])
if exception:
return
if app.builder.name not in builders:
if not app.config['sphinx_tabs_nowarn']:
app.warn(
'Not copying tabs assets! Not compatible with %s builder' %
app.builder.name)
return
app.info('Copying tabs assets... ', nonl=True)
installdir = os.path.join(app.builder.outdir, '_static', 'sphinx_tabs')
for path in FILES:
source = os.path.join(DIR, path)
dest = os.path.join(installdir, path)
destdir = os.path.dirname(dest)
if not os.path.exists(destdir):
os.makedirs(destdir)
copyfile(source, dest)
app.info('done')
示例8: copy_asset_file
def copy_asset_file(source, destination, context=None, renderer=None):
"""Copy an asset file to destination.
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. If not given, template files are simply copied
:param renderer: The template engine. If not given, SphinxRenderer is used by default
"""
if not os.path.exists(source):
return
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') and context:
if renderer is None:
from sphinx.util.template import SphinxRenderer
renderer = SphinxRenderer()
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)
示例9: copy_assets
def copy_assets(app, exception):
""" Copy asset files to the output """
builders = ('html', 'readthedocs', 'readthedocssinglehtmllocalmedia',
'singlehtml')
if app.builder.name not in builders:
app.warn('Not copying tabs assets! Not compatible with %s builder' %
app.builder.name)
return
if exception:
app.warn('Not copying tabs assets! Error occurred previously')
return
app.info('Copying tabs assets... ', nonl=True)
installdir = os.path.join(app.builder.outdir, '_static', 'sphinx_tabs')
for path in FILES:
source = os.path.join(DIR, path)
dest = os.path.join(installdir, path)
destdir = os.path.dirname(dest)
if not os.path.exists(destdir):
os.makedirs(destdir)
copyfile(source, dest)
app.info('done')
示例10: handle_page
def handle_page(self, pagename, ctx, templatename='page.html',
outfilename=None, event_arg=None):
# type: (str, Dict, str, str, Any) -> None
ctx['current_page_name'] = pagename
self.add_sidebars(pagename, ctx)
if not outfilename:
outfilename = path.join(self.outdir,
os_path(pagename) + self.out_suffix)
# we're not taking the return value here, since no template is
# actually rendered
self.app.emit('html-page-context', pagename, templatename, ctx, event_arg)
# make context object serializable
for key in list(ctx):
if isinstance(ctx[key], types.FunctionType):
del ctx[key]
ensuredir(path.dirname(outfilename))
self.dump_context(ctx, outfilename)
# if there is a source file, copy the source file for the
# "show source" link
if ctx.get('sourcename'):
source_name = path.join(self.outdir, '_sources',
os_path(ctx['sourcename']))
ensuredir(path.dirname(source_name))
copyfile(self.env.doc2path(pagename), source_name)
示例11: copy_image_files_pil
def copy_image_files_pil(self):
"""Copy images using the PIL.
The method tries to read and write the files with the PIL,
converting the format and resizing the image if necessary/possible.
"""
ensuredir(path.join(self.outdir, self.imagedir))
for src in self.app.status_iterator(self.images, "copying images... ", brown, len(self.images)):
dest = self.images[src]
try:
img = Image.open(path.join(self.srcdir, src))
except IOError:
if not self.is_vector_graphics(src):
self.warn("cannot read image file %r: copying it instead" % (path.join(self.srcdir, src),))
try:
copyfile(path.join(self.srcdir, src), path.join(self.outdir, self.imagedir, dest))
except (IOError, OSError) as err:
self.warn("cannot copy image file %r: %s" % (path.join(self.srcdir, src), err))
continue
if self.config.epub_fix_images:
if img.mode in ("P",):
# See PIL documentation for Image.convert()
img = img.convert()
if self.config.epub_max_image_width > 0:
(width, height) = img.size
nw = self.config.epub_max_image_width
if width > nw:
nh = (height * nw) / width
img = img.resize((nw, nh), Image.BICUBIC)
try:
img.save(path.join(self.outdir, self.imagedir, dest))
except (IOError, OSError) as err:
self.warn("cannot write image file %r: %s" % (path.join(self.srcdir, src), err))
示例12: copy_assets
def copy_assets(app, exception):
""" Copy asset files to the output """
if 'getLogger' in dir(logging):
log = logging.getLogger(__name__).info # pylint: disable=no-member
else:
log = app.info
builders = get_compatible_builders(app)
if exception:
return
if app.builder.name not in builders:
if not app.config['sphinx_tabs_nowarn']:
app.warn(
'Not copying tabs assets! Not compatible with %s builder' %
app.builder.name)
return
log('Copying tabs assets')
installdir = os.path.join(app.builder.outdir, '_static', 'sphinx_tabs')
for path in FILES:
source = resource_filename('sphinx_tabs', path)
dest = os.path.join(installdir, path)
destdir = os.path.dirname(dest)
if not os.path.exists(destdir):
os.makedirs(destdir)
copyfile(source, dest)
示例13: handle_page
def handle_page(self, pagename, addctx, templatename='page.html',
outfilename=None, event_arg=None):
ctx = self.globalcontext.copy()
# current_page_name is backwards compatibility
ctx['pagename'] = ctx['current_page_name'] = pagename
default_baseuri = self.get_target_uri(pagename)
# in the singlehtml builder, default_baseuri still contains an #anchor
# part, which relative_uri doesn't really like...
default_baseuri = default_baseuri.rsplit('#', 1)[0]
def pathto(otheruri, resource=False, baseuri=default_baseuri):
if resource and '://' in otheruri:
# allow non-local resources given by scheme
return otheruri
elif not resource:
otheruri = self.get_target_uri(otheruri)
uri = relative_uri(baseuri, otheruri) or '#'
return uri
ctx['pathto'] = pathto
ctx['hasdoc'] = lambda name: name in self.env.all_docs
if self.name != 'htmlhelp':
ctx['encoding'] = encoding = self.config.html_output_encoding
else:
ctx['encoding'] = encoding = self.encoding
ctx['toctree'] = lambda **kw: self._get_local_toctree(pagename, **kw)
self.add_sidebars(pagename, ctx)
ctx.update(addctx)
newtmpl = self.app.emit_firstresult('html-page-context', pagename,
templatename, ctx, event_arg)
if newtmpl:
templatename = newtmpl
try:
output = self.templates.render(templatename, ctx)
except UnicodeError:
self.warn("a Unicode error occurred when rendering the page %s. "
"Please make sure all config values that contain "
"non-ASCII content are Unicode strings." % pagename)
return
if not outfilename:
outfilename = self.get_outfilename(pagename)
# outfilename's path is in general different from self.outdir
ensuredir(path.dirname(outfilename))
try:
f = codecs.open(outfilename, 'w', encoding, 'xmlcharrefreplace')
try:
f.write(output)
finally:
f.close()
except (IOError, OSError) as err:
self.warn("error writing file %s: %s" % (outfilename, err))
if self.copysource and ctx.get('sourcename'):
# copy the source file for the "show source" link
source_name = path.join(self.outdir, '_sources',
os_path(ctx['sourcename']))
ensuredir(path.dirname(source_name))
copyfile(self.env.doc2path(pagename), source_name)
示例14: copy_stylesheet
def copy_stylesheet(app, exception):
if app.builder.name != "html" or exception:
return
app.info(bold("Copying issuetracker stylesheet... "), nonl=True)
dest = path.join(app.builder.outdir, "_static", "issuetracker.css")
source = path.join(path.abspath(path.dirname(__file__)), "issuetracker.css")
copyfile(source, dest)
app.info("done")
示例15: copy_stylesheet
def copy_stylesheet(app, exception):
if app.builder.name != 'html' or exception:
return
app.info('Copying requirements stylesheet... ', nonl=True)
dest = os.path.join(app.builder.outdir, '_static', CSS_FILE)
source = os.path.join(os.path.abspath(os.path.dirname(__file__)), CSS_FILE)
copyfile(source, dest)
app.info('done')