/usr/lib/python2.7/dist-packages/sphinx_gallery/downloads.py is in python-sphinx-gallery 0.1.13-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | # -*- coding: utf-8 -*-
r"""
Utilities for downloadable items
================================
"""
# Author: Óscar Nájera
# License: 3-clause BSD
from __future__ import absolute_import, division, print_function
import os
import zipfile
CODE_DOWNLOAD = """
\n.. only :: html
.. container:: sphx-glr-footer
\n .. container:: sphx-glr-download
:download:`Download Python source code: {0} <{0}>`\n
\n .. container:: sphx-glr-download
:download:`Download Jupyter notebook: {1} <{1}>`\n"""
CODE_ZIP_DOWNLOAD = """
\n.. only :: html
.. container:: sphx-glr-footer
\n .. container:: sphx-glr-download
:download:`Download all examples in Python source code: {0} </{1}>`\n
\n .. container:: sphx-glr-download
:download:`Download all examples in Jupyter notebooks: {2} </{3}>`\n"""
def python_zip(file_list, gallery_path, extension='.py'):
"""Stores all files in file_list into an zip file
Parameters
----------
file_list : list of strings
Holds all the file names to be included in zip file
gallery_path : str
path to where the zipfile is stored
extension : str
'.py' or '.ipynb' In order to deal with downloads of python
sources and jupyter notebooks the file extension from files in
file_list will be removed and replace with the value of this
variable while generating the zip file
Returns
-------
zipname : str
zip file name, written as `target_dir_{python,jupyter}.zip`
depending on the extension
"""
zipname = os.path.basename(gallery_path)
zipname += '_python' if extension == '.py' else '_jupyter'
zipname = os.path.join(gallery_path, zipname + '.zip')
zipf = zipfile.ZipFile(zipname, mode='w')
for fname in file_list:
file_src = os.path.splitext(fname)[0] + extension
zipf.write(file_src, os.path.relpath(file_src, gallery_path))
zipf.close()
return zipname
def list_downloadable_sources(target_dir):
"""Returns a list of python source files is target_dir
Parameters
----------
target_dir : str
path to the directory where python source file are
Returns
-------
list
list of paths to all Python source files in `target_dir`
"""
return [os.path.join(target_dir, fname)
for fname in os.listdir(target_dir)
if fname.endswith('.py')]
def generate_zipfiles(gallery_dir):
"""
Collects all Python source files and Jupyter notebooks in
gallery_dir and makes zipfiles of them
Parameters
----------
gallery_dir : str
path of the gallery to collect downloadable sources
Return
------
download_rst: str
RestructuredText to include download buttons to the generated files
"""
listdir = list_downloadable_sources(gallery_dir)
for directory in sorted(os.listdir(gallery_dir)):
if os.path.isdir(os.path.join(gallery_dir, directory)):
target_dir = os.path.join(gallery_dir, directory)
listdir.extend(list_downloadable_sources(target_dir))
py_zipfile = python_zip(listdir, gallery_dir)
jy_zipfile = python_zip(listdir, gallery_dir, ".ipynb")
def rst_path(filepath):
return filepath.replace(os.sep, '/')
dw_rst = CODE_ZIP_DOWNLOAD.format(os.path.basename(py_zipfile),
rst_path(py_zipfile),
os.path.basename(jy_zipfile),
rst_path(jy_zipfile))
return dw_rst
|