|
| 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