Skip to content

Commit ca4e802

Browse files
committed
[MAINT] Install sphinx-gallery v0.1.0
1 parent 2a9492b commit ca4e802

File tree

9 files changed

+1603
-0
lines changed

9 files changed

+1603
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
==============
3+
Sphinx Gallery
4+
==============
5+
6+
"""
7+
import os
8+
__version__ = '0.1.0'
9+
10+
11+
def glr_path_static():
12+
"""Returns path to packaged static files"""
13+
return os.path.abspath(os.path.join(os.path.dirname(__file__), '_static'))
20.9 KB
Loading
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
.sphx-glr-thumbcontainer {
2+
background: #fff;
3+
border: solid #fff 1px;
4+
-moz-border-radius: 5px;
5+
-webkit-border-radius: 5px;
6+
border-radius: 5px;
7+
box-shadow: none;
8+
float: left;
9+
margin: 5px;
10+
min-height: 230px;
11+
padding-top: 5px;
12+
position: relative;
13+
}
14+
.sphx-glr-thumbcontainer:hover {
15+
border: solid #b4ddfc 1px;
16+
box-shadow: 0 0 15px rgba(142, 176, 202, 0.5);
17+
}
18+
.sphx-glr-thumbcontainer a.internal {
19+
bottom: 0;
20+
display: block;
21+
left: 0;
22+
padding: 150px 10px 0;
23+
position: absolute;
24+
right: 0;
25+
top: 0;
26+
}
27+
.sphx-glr-thumbcontainer p {
28+
margin: 0 0 .1em 0;
29+
}
30+
.sphx-glr-thumbcontainer .figure {
31+
margin: 10px;
32+
width: 160px;
33+
}
34+
.sphx-glr-thumbcontainer img {
35+
display: inline;
36+
max-height: 160px;
37+
max-width: 100%;
38+
}
39+
.sphx-glr-thumbcontainer[tooltip]:hover:after {
40+
background: rgba(0, 0, 0, 0.8);
41+
-webkit-border-radius: 5px;
42+
-moz-border-radius: 5px;
43+
border-radius: 5px;
44+
color: #fff;
45+
content: attr(tooltip);
46+
left: 95%;
47+
padding: 5px 15px;
48+
position: absolute;
49+
z-index: 98;
50+
width: 220px;
51+
bottom: 52%;
52+
}
53+
.sphx-glr-thumbcontainer[tooltip]:hover:before {
54+
border: solid;
55+
border-color: #333 transparent;
56+
border-width: 18px 0 0 20px;
57+
bottom: 58%;
58+
content: '';
59+
left: 85%;
60+
position: absolute;
61+
z-index: 99;
62+
}
63+
64+
.highlight-pytb pre {
65+
background-color: #ffe4e4;
66+
border: 1px solid #f66;
67+
margin-top: 10px;
68+
padding: 7px;
69+
}
70+
71+
.sphx-glr-script-out {
72+
color: #888;
73+
margin: 0;
74+
}
75+
.sphx-glr-script-out .highlight {
76+
background-color: transparent;
77+
margin-left: 2.5em;
78+
margin-top: -1.4em;
79+
}
80+
.sphx-glr-script-out .highlight pre {
81+
background-color: #fafae2;
82+
border: 0;
83+
max-height: 30em;
84+
overflow: auto;
85+
padding-left: 1ex;
86+
margin: 0px;
87+
word-break: break-word;
88+
}
89+
.sphx-glr-script-out + p {
90+
margin-top: 1.8em;
91+
}
92+
blockquote.sphx-glr-script-out {
93+
margin-left: 0pt;
94+
}
95+
.sphx-glr-download {
96+
background-color: #ffc;
97+
border: 1px solid #c2c22d;
98+
border-radius: 4px;
99+
margin: 1em auto 1ex auto;
100+
max-width: 45ex;
101+
padding: 1ex;
102+
}
103+
.sphx-glr-download a {
104+
color: #4b4600;
105+
}
106+
107+
ul.sphx-glr-horizontal {
108+
list-style: none;
109+
padding: 0;
110+
}
111+
ul.sphx-glr-horizontal li {
112+
display: inline;
113+
}
114+
ul.sphx-glr-horizontal img {
115+
height: auto !important;
116+
}
4.21 KB
Loading
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: Óscar Nájera
3+
# License: 3-clause BSD
4+
"""
5+
========================
6+
Backreferences Generator
7+
========================
8+
9+
Reviews generated example files in order to keep track of used modules
10+
"""
11+
12+
from __future__ import print_function
13+
import ast
14+
import os
15+
16+
17+
# Try Python 2 first, otherwise load from Python 3
18+
try:
19+
import cPickle as pickle
20+
except ImportError:
21+
import pickle
22+
23+
24+
class NameFinder(ast.NodeVisitor):
25+
"""Finds the longest form of variable names and their imports in code
26+
27+
Only retains names from imported modules.
28+
"""
29+
30+
def __init__(self):
31+
super(NameFinder, self).__init__()
32+
self.imported_names = {}
33+
self.accessed_names = set()
34+
35+
def visit_Import(self, node, prefix=''):
36+
for alias in node.names:
37+
local_name = alias.asname or alias.name
38+
self.imported_names[local_name] = prefix + alias.name
39+
40+
def visit_ImportFrom(self, node):
41+
self.visit_Import(node, node.module + '.')
42+
43+
def visit_Name(self, node):
44+
self.accessed_names.add(node.id)
45+
46+
def visit_Attribute(self, node):
47+
attrs = []
48+
while isinstance(node, ast.Attribute):
49+
attrs.append(node.attr)
50+
node = node.value
51+
52+
if isinstance(node, ast.Name):
53+
# This is a.b, not e.g. a().b
54+
attrs.append(node.id)
55+
self.accessed_names.add('.'.join(reversed(attrs)))
56+
else:
57+
# need to get a in a().b
58+
self.visit(node)
59+
60+
def get_mapping(self):
61+
for name in self.accessed_names:
62+
local_name = name.split('.', 1)[0]
63+
remainder = name[len(local_name):]
64+
if local_name in self.imported_names:
65+
# Join import path to relative path
66+
full_name = self.imported_names[local_name] + remainder
67+
yield name, full_name
68+
69+
70+
def get_short_module_name(module_name, obj_name):
71+
""" Get the shortest possible module name """
72+
parts = module_name.split('.')
73+
short_name = module_name
74+
for i in range(len(parts) - 1, 0, -1):
75+
short_name = '.'.join(parts[:i])
76+
try:
77+
exec('from %s import %s' % (short_name, obj_name))
78+
except ImportError:
79+
# get the last working module name
80+
short_name = '.'.join(parts[:(i + 1)])
81+
break
82+
return short_name
83+
84+
85+
def identify_names(code):
86+
"""Builds a codeobj summary by identifying and resolving used names
87+
88+
>>> code = '''
89+
... from a.b import c
90+
... import d as e
91+
... print(c)
92+
... e.HelloWorld().f.g
93+
... '''
94+
>>> for name, o in sorted(identify_names(code).items()):
95+
... print(name, o['name'], o['module'], o['module_short'])
96+
c c a.b a.b
97+
e.HelloWorld HelloWorld d d
98+
"""
99+
finder = NameFinder()
100+
finder.visit(ast.parse(code))
101+
102+
example_code_obj = {}
103+
for name, full_name in finder.get_mapping():
104+
# name is as written in file (e.g. np.asarray)
105+
# full_name includes resolved import path (e.g. numpy.asarray)
106+
module, attribute = full_name.rsplit('.', 1)
107+
# get shortened module name
108+
module_short = get_short_module_name(module, attribute)
109+
cobj = {'name': attribute, 'module': module,
110+
'module_short': module_short}
111+
example_code_obj[name] = cobj
112+
return example_code_obj
113+
114+
115+
def scan_used_functions(example_file, gallery_conf):
116+
"""save variables so we can later add links to the documentation"""
117+
example_code_obj = identify_names(open(example_file).read())
118+
if example_code_obj:
119+
codeobj_fname = example_file[:-3] + '_codeobj.pickle'
120+
with open(codeobj_fname, 'wb') as fid:
121+
pickle.dump(example_code_obj, fid, pickle.HIGHEST_PROTOCOL)
122+
123+
backrefs = set('{module_short}.{name}'.format(**entry)
124+
for entry in example_code_obj.values()
125+
if entry['module'].startswith(gallery_conf['doc_module']))
126+
127+
return backrefs
128+
129+
130+
THUMBNAIL_TEMPLATE = """
131+
.. raw:: html
132+
133+
<div class="sphx-glr-thumbcontainer" tooltip="{snippet}">
134+
135+
.. only:: html
136+
137+
.. figure:: /{thumbnail}
138+
139+
:ref:`sphx_glr_{ref_name}`
140+
141+
.. raw:: html
142+
143+
</div>
144+
"""
145+
146+
BACKREF_THUMBNAIL_TEMPLATE = THUMBNAIL_TEMPLATE + """
147+
.. only:: not html
148+
149+
* :ref:`sphx_glr_{ref_name}`
150+
"""
151+
152+
153+
def _thumbnail_div(full_dir, fname, snippet, is_backref=False):
154+
"""Generates RST to place a thumbnail in a gallery"""
155+
thumb = os.path.join(full_dir, 'images', 'thumb',
156+
'sphx_glr_%s_thumb.png' % fname[:-3])
157+
ref_name = os.path.join(full_dir, fname).replace(os.path.sep, '_')
158+
159+
template = BACKREF_THUMBNAIL_TEMPLATE if is_backref else THUMBNAIL_TEMPLATE
160+
return template.format(snippet=snippet, thumbnail=thumb, ref_name=ref_name)
161+
162+
163+
def write_backreferences(seen_backrefs, gallery_conf,
164+
target_dir, fname, snippet):
165+
"""Writes down back reference files, which include a thumbnail list
166+
of examples using a certain module"""
167+
example_file = os.path.join(target_dir, fname)
168+
backrefs = scan_used_functions(example_file, gallery_conf)
169+
for backref in backrefs:
170+
include_path = os.path.join(gallery_conf['mod_example_dir'],
171+
'%s.examples' % backref)
172+
seen = backref in seen_backrefs
173+
with open(include_path, 'a' if seen else 'w') as ex_file:
174+
if not seen:
175+
heading = '\n\nExamples using ``%s``' % backref
176+
ex_file.write(heading + '\n')
177+
ex_file.write('^' * len(heading) + '\n')
178+
ex_file.write(_thumbnail_div(target_dir, fname, snippet,
179+
is_backref=True))
180+
seen_backrefs.add(backref)

0 commit comments

Comments
 (0)