| 1 |
""" |
| 2 |
cloud_sptheme.utils -- internal helper utilities |
| 3 |
""" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from functools import update_wrapper |
| 9 |
import logging; log = logging.getLogger(__name__) |
| 10 |
import os |
| 11 |
import sys |
| 12 |
|
| 13 |
import sphinx |
| 14 |
from sphinx.util.fileutil import copyfile |
| 15 |
|
| 16 |
|
| 17 |
__all__ = [ |
| 18 |
|
| 19 |
'PY2', 'PY3', 'u', 'ru', |
| 20 |
'unicode', 'unicode_or_str', |
| 21 |
|
| 22 |
|
| 23 |
"patchapplier", |
| 24 |
"monkeypatch", |
| 25 |
] |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
PY2 = sys.version_info < (3,0) |
| 31 |
PY3 = not PY2 |
| 32 |
|
| 33 |
|
| 34 |
if PY2: |
| 35 |
def u(s): |
| 36 |
return s.decode("unicode_escape") |
| 37 |
def ru(s): |
| 38 |
return s.decode("ascii") |
| 39 |
unicode = unicode |
| 40 |
unicode_or_str = (unicode, str) |
| 41 |
else: |
| 42 |
def u(s): |
| 43 |
return s |
| 44 |
ru = u |
| 45 |
unicode = str |
| 46 |
unicode_or_str = (str,) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def patchapplier(func): |
| 52 |
""" |
| 53 |
function decorator to help functions that apply a monkeypatch. |
| 54 |
makes them only run once. |
| 55 |
""" |
| 56 |
def wrapper(): |
| 57 |
if wrapper.patched: |
| 58 |
return False |
| 59 |
func() |
| 60 |
wrapper.patched = True |
| 61 |
logging.getLogger(func.__module__).debug("%s: patch applied", func.__name__) |
| 62 |
return True |
| 63 |
wrapper.patched = False |
| 64 |
update_wrapper(wrapper, func) |
| 65 |
return wrapper |
| 66 |
|
| 67 |
def monkeypatch(target, name=None): |
| 68 |
""" |
| 69 |
helper to monkeypatch another object. |
| 70 |
the decorated function is wrapped around the existing function in |
| 71 |
:samp:`target.{name}`, and used to replace it. |
| 72 |
|
| 73 |
**name** defaults to the name of the function being decorated. |
| 74 |
|
| 75 |
the original value is passed in as the first positional argument to the function. |
| 76 |
""" |
| 77 |
def builder(func): |
| 78 |
attr = name or func.__name__ |
| 79 |
wrapped = getattr(target, attr) |
| 80 |
def wrapper(*args, **kwds): |
| 81 |
return func(wrapped, *args, **kwds) |
| 82 |
update_wrapper(wrapper, wrapped) |
| 83 |
wrapper.__wrapped__ = wrapped |
| 84 |
setattr(target, attr, wrapper) |
| 85 |
return func |
| 86 |
return builder |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
def _patch_html_extra_static(builder): |
| 93 |
""" |
| 94 |
monkeypatch hook for add_static_file() to use |
| 95 |
""" |
| 96 |
config = builder.config |
| 97 |
if hasattr(config, "html_extra_static"): |
| 98 |
return |
| 99 |
|
| 100 |
@monkeypatch(builder) |
| 101 |
def copy_static_files(wrapped): |
| 102 |
wrapped() |
| 103 |
|
| 104 |
ctx = builder.globalcontext.copy() |
| 105 |
for source, target in config.html_extra_static: |
| 106 |
|
| 107 |
|
| 108 |
copyfile(source, target) |
| 109 |
|
| 110 |
config.html_extra_static = [] |
| 111 |
|
| 112 |
|
| 113 |
def add_static_file(builder, source, name=None, stylesheet=False, |
| 114 |
javascript=False): |
| 115 |
""" |
| 116 |
monkeypatch sphinx's html builder to include specified static file. |
| 117 |
|
| 118 |
will copy file to :samp:`{outdir}/_static/{name}`. |
| 119 |
|
| 120 |
:param builder: |
| 121 |
ref to HTMLBuilder |
| 122 |
|
| 123 |
:param source: |
| 124 |
path to source file |
| 125 |
|
| 126 |
:param name: |
| 127 |
name for target file; defaults to base name of source file. |
| 128 |
|
| 129 |
:param stylesheet: |
| 130 |
register file as additional stylesheet |
| 131 |
""" |
| 132 |
app = builder.app |
| 133 |
_patch_html_extra_static(builder) |
| 134 |
|
| 135 |
if not os.path.exists(source): |
| 136 |
builder.warn('static asset %r does not exist' % source) |
| 137 |
return |
| 138 |
|
| 139 |
if not name: |
| 140 |
name = os.path.basename(source) |
| 141 |
|
| 142 |
target = os.path.join(builder.outdir, "_static", name) |
| 143 |
if stylesheet: |
| 144 |
if sphinx.version_info < (1, 8): |
| 145 |
app.add_stylesheet(name) |
| 146 |
else: |
| 147 |
app.add_css_file(name) |
| 148 |
|
| 149 |
if javascript: |
| 150 |
if sphinx.version_info < (1, 8): |
| 151 |
app.add_javascript(name) |
| 152 |
else: |
| 153 |
app.add_js_file(name) |
| 154 |
|
| 155 |
builder.config.html_extra_static.append((source, target)) |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|