{"action":"created","comment":{"url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/comments/29578230","id":29578230,"diff_hunk":"@@ -0,0 +1,167 @@\n+# -*- coding: utf-8 -*-\n+# Author: Óscar Nájera\n+# License: 3-clause BSD\n+\"\"\"\n+Reviews generated example files in order to keep track of used modules\n+\"\"\"\n+\n+from __future__ import print_function\n+import ast\n+import os\n+# Try Python 2 first, otherwise load from Python 3\n+try:\n+    import cPickle as pickle\n+except ImportError:\n+    import pickle\n+\n+\n+class NameFinder(ast.NodeVisitor):\n+    \"\"\"Finds the longest form of variable names and their imports in code\n+\n+    Only retains names from imported modules.\n+    \"\"\"\n+\n+    def __init__(self):\n+        super(NameFinder, self).__init__()\n+        self.imported_names = {}\n+        self.accessed_names = set()\n+\n+    def visit_Import(self, node, prefix=''):\n+        for alias in node.names:\n+            local_name = alias.asname or alias.name\n+            self.imported_names[local_name] = prefix + alias.name\n+\n+    def visit_ImportFrom(self, node):\n+        self.visit_Import(node, node.module + '.')\n+\n+    def visit_Name(self, node):\n+        self.accessed_names.add(node.id)\n+\n+    def visit_Attribute(self, node):\n+        attrs = []\n+        while isinstance(node, ast.Attribute):\n+            attrs.append(node.attr)\n+            node = node.value\n+\n+        if isinstance(node, ast.Name):\n+            # This is a.b, not e.g. a().b\n+            attrs.append(node.id)\n+            self.accessed_names.add('.'.join(reversed(attrs)))\n+        else:\n+            # need to get a in a().b\n+            self.visit(node)\n+\n+    def get_mapping(self):\n+        for name in self.accessed_names:\n+            local_name = name.split('.', 1)[0]\n+            remainder = name[len(local_name):]\n+            if local_name in self.imported_names:\n+                # Join import path to relative path\n+                full_name = self.imported_names[local_name] + remainder\n+                yield name, full_name\n+\n+\n+def get_short_module_name(module_name, obj_name):\n+    \"\"\" Get the shortest possible module name \"\"\"\n+    parts = module_name.split('.')\n+    short_name = module_name\n+    for i in range(len(parts) - 1, 0, -1):\n+        short_name = '.'.join(parts[:i])\n+        try:\n+            exec('from %s import %s' % (short_name, obj_name))\n+        except ImportError:\n+            # get the last working module name\n+            short_name = '.'.join(parts[:(i + 1)])\n+            break\n+    return short_name\n+\n+\n+def identify_names(code):\n+    \"\"\"Builds a codeobj summary by identifying and resovles used names\n+\n+    >>> code = '''\n+    ... from a.b import c\n+    ... import d as e\n+    ... print(c)\n+    ... e.HelloWorld().f.g\n+    ... '''\n+    >>> for name, o in sorted(identify_names(code).items()):\n+    ...     print(name, o['name'], o['module'], o['module_short'])\n+    c c a.b a.b\n+    e.HelloWorld HelloWorld d d\n+    \"\"\"\n+    finder = NameFinder()\n+    finder.visit(ast.parse(code))\n+\n+    example_code_obj = {}\n+    for name, full_name in finder.get_mapping():\n+        # name is as written in file (e.g. np.asarray)\n+        # full_name includes resolved import path (e.g. numpy.asarray)\n+        module, attribute = full_name.rsplit('.', 1)\n+        # get shortened module name\n+        module_short = get_short_module_name(module, attribute)\n+        cobj = {'name': attribute, 'module': module,\n+                'module_short': module_short}\n+        example_code_obj[name] = cobj\n+    return example_code_obj\n+\n+\n+def scan_used_functions(example_file, gallery_conf):\n+    \"\"\"save variables so we can later add links to the documentation\"\"\"\n+    example_code_obj = identify_names(open(example_file).read())\n+    if example_code_obj:\n+        codeobj_fname = example_file[:-3] + '_codeobj.pickle'\n+        with open(codeobj_fname, 'wb') as fid:\n+            pickle.dump(example_code_obj, fid, pickle.HIGHEST_PROTOCOL)\n+\n+    backrefs = set('{module_short}.{name}'.format(**entry)\n+                   for entry in example_code_obj.values()\n+                   if entry['module'].startswith(gallery_conf['doc_module']))\n+\n+    return backrefs\n+\n+\n+def _thumbnail_div(full_dir, fname, snippet):\n+    \"\"\"Generates RST to place a thumbnail in a gallery\"\"\"\n+    thumb = os.path.join(full_dir, 'images', 'thumb',\n+                         'sphx_glr_%s.png' % fname[:-3])\n+    link_name = os.path.join(full_dir, fname)\n+    ref_name = link_name.replace(os.path.sep, '_')\n+    if ref_name.startswith('._'):\n+        ref_name = ref_name[2:]\n+\n+    out = \"\"\"\n+.. raw:: html\n+\n+    <div class=\"sphx-glr-thumbContainer\" tooltip=\"{}\">\n+\n+.. figure:: /{}\n+    :target: /{}.html","path":"sphinxgallery/backreferences.py","position":139,"original_position":139,"commit_id":"87b0af9bb0a8f71c4f68da4f514065e343461b4f","original_commit_id":"87b0af9bb0a8f71c4f68da4f514065e343461b4f","user":{"login":"lesteve","id":1680079,"avatar_url":"https://avatars.githubusercontent.com/u/1680079?v=3","gravatar_id":"","url":"https://api.github.com/users/lesteve","html_url":"https://github.com/lesteve","followers_url":"https://api.github.com/users/lesteve/followers","following_url":"https://api.github.com/users/lesteve/following{/other_user}","gists_url":"https://api.github.com/users/lesteve/gists{/gist_id}","starred_url":"https://api.github.com/users/lesteve/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lesteve/subscriptions","organizations_url":"https://api.github.com/users/lesteve/orgs","repos_url":"https://api.github.com/users/lesteve/repos","events_url":"https://api.github.com/users/lesteve/events{/privacy}","received_events_url":"https://api.github.com/users/lesteve/received_events","type":"User","site_admin":false},"body":"Probably very naive: but it seems that removing the leading `/` in the target fixes the link without running a webserver. Is there a reason why you need it?\r\n\r\nSame thing goes for the `/` in figure, it doesn't look like it is needed.","created_at":"2015-05-04T11:30:00Z","updated_at":"2015-05-04T11:30:00Z","html_url":"https://github.com/sphinx-gallery/sphinx-gallery/pull/26#discussion_r29578230","pull_request_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/26","_links":{"self":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/comments/29578230"},"html":{"href":"https://github.com/sphinx-gallery/sphinx-gallery/pull/26#discussion_r29578230"},"pull_request":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/26"}}},"pull_request":{"url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/26","id":33103527,"html_url":"https://github.com/sphinx-gallery/sphinx-gallery/pull/26","diff_url":"https://github.com/sphinx-gallery/sphinx-gallery/pull/26.diff","patch_url":"https://github.com/sphinx-gallery/sphinx-gallery/pull/26.patch","issue_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/issues/26","number":26,"state":"open","locked":false,"title":"scale image in stead of make thumbnail and independent backrefereces function","user":{"login":"Titan-C","id":713451,"avatar_url":"https://avatars.githubusercontent.com/u/713451?v=3","gravatar_id":"","url":"https://api.github.com/users/Titan-C","html_url":"https://github.com/Titan-C","followers_url":"https://api.github.com/users/Titan-C/followers","following_url":"https://api.github.com/users/Titan-C/following{/other_user}","gists_url":"https://api.github.com/users/Titan-C/gists{/gist_id}","starred_url":"https://api.github.com/users/Titan-C/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Titan-C/subscriptions","organizations_url":"https://api.github.com/users/Titan-C/orgs","repos_url":"https://api.github.com/users/Titan-C/repos","events_url":"https://api.github.com/users/Titan-C/events{/privacy}","received_events_url":"https://api.github.com/users/Titan-C/received_events","type":"User","site_admin":false},"body":"Here I refactor in 3 things.\r\n- First I include the change from the mayavi branch that turns make thumbnail into scale image\r\n- Second I split the backreferece file generation into a separate function\r\n- Last the fhindex is no more moved around the gen_dir_rst functions it is now a return value","created_at":"2015-04-11T13:51:11Z","updated_at":"2015-05-04T11:30:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7ac69e10141c7f805b2dae964f84668b3c01d0d1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/26/commits","review_comments_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/26/comments","review_comment_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/comments{/number}","comments_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/issues/26/comments","statuses_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/statuses/87b0af9bb0a8f71c4f68da4f514065e343461b4f","head":{"label":"Titan-C:scim","ref":"scim","sha":"87b0af9bb0a8f71c4f68da4f514065e343461b4f","user":{"login":"Titan-C","id":713451,"avatar_url":"https://avatars.githubusercontent.com/u/713451?v=3","gravatar_id":"","url":"https://api.github.com/users/Titan-C","html_url":"https://github.com/Titan-C","followers_url":"https://api.github.com/users/Titan-C/followers","following_url":"https://api.github.com/users/Titan-C/following{/other_user}","gists_url":"https://api.github.com/users/Titan-C/gists{/gist_id}","starred_url":"https://api.github.com/users/Titan-C/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Titan-C/subscriptions","organizations_url":"https://api.github.com/users/Titan-C/orgs","repos_url":"https://api.github.com/users/Titan-C/repos","events_url":"https://api.github.com/users/Titan-C/events{/privacy}","received_events_url":"https://api.github.com/users/Titan-C/received_events","type":"User","site_admin":false},"repo":{"id":26191147,"name":"sphinx-gallery","full_name":"Titan-C/sphinx-gallery","owner":{"login":"Titan-C","id":713451,"avatar_url":"https://avatars.githubusercontent.com/u/713451?v=3","gravatar_id":"","url":"https://api.github.com/users/Titan-C","html_url":"https://github.com/Titan-C","followers_url":"https://api.github.com/users/Titan-C/followers","following_url":"https://api.github.com/users/Titan-C/following{/other_user}","gists_url":"https://api.github.com/users/Titan-C/gists{/gist_id}","starred_url":"https://api.github.com/users/Titan-C/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Titan-C/subscriptions","organizations_url":"https://api.github.com/users/Titan-C/orgs","repos_url":"https://api.github.com/users/Titan-C/repos","events_url":"https://api.github.com/users/Titan-C/events{/privacy}","received_events_url":"https://api.github.com/users/Titan-C/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Titan-C/sphinx-gallery","description":"Sphinx extension for automatic generation of an example gallery","fork":true,"url":"https://api.github.com/repos/Titan-C/sphinx-gallery","forks_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/forks","keys_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/teams","hooks_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/hooks","issue_events_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/issues/events{/number}","events_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/events","assignees_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/assignees{/user}","branches_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/branches{/branch}","tags_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/tags","blobs_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/git/refs{/sha}","trees_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/statuses/{sha}","languages_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/languages","stargazers_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/stargazers","contributors_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/contributors","subscribers_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/subscribers","subscription_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/subscription","commits_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/commits{/sha}","git_commits_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/git/commits{/sha}","comments_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/comments{/number}","issue_comment_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/issues/comments{/number}","contents_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/contents/{+path}","compare_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/merges","archive_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/downloads","issues_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/issues{/number}","pulls_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/pulls{/number}","milestones_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/milestones{/number}","notifications_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/labels{/name}","releases_url":"https://api.github.com/repos/Titan-C/sphinx-gallery/releases{/id}","created_at":"2014-11-04T22:13:32Z","updated_at":"2015-05-01T14:03:11Z","pushed_at":"2015-05-01T14:05:07Z","git_url":"git://github.com/Titan-C/sphinx-gallery.git","ssh_url":"git@github.com:Titan-C/sphinx-gallery.git","clone_url":"https://github.com/Titan-C/sphinx-gallery.git","svn_url":"https://github.com/Titan-C/sphinx-gallery","homepage":null,"size":672,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"sphinx-gallery:master","ref":"master","sha":"1a5a1e57e99ffde2d7f6a21b4e206fce95e38044","user":{"login":"sphinx-gallery","id":9541799,"avatar_url":"https://avatars.githubusercontent.com/u/9541799?v=3","gravatar_id":"","url":"https://api.github.com/users/sphinx-gallery","html_url":"https://github.com/sphinx-gallery","followers_url":"https://api.github.com/users/sphinx-gallery/followers","following_url":"https://api.github.com/users/sphinx-gallery/following{/other_user}","gists_url":"https://api.github.com/users/sphinx-gallery/gists{/gist_id}","starred_url":"https://api.github.com/users/sphinx-gallery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sphinx-gallery/subscriptions","organizations_url":"https://api.github.com/users/sphinx-gallery/orgs","repos_url":"https://api.github.com/users/sphinx-gallery/repos","events_url":"https://api.github.com/users/sphinx-gallery/events{/privacy}","received_events_url":"https://api.github.com/users/sphinx-gallery/received_events","type":"Organization","site_admin":false},"repo":{"id":25860190,"name":"sphinx-gallery","full_name":"sphinx-gallery/sphinx-gallery","owner":{"login":"sphinx-gallery","id":9541799,"avatar_url":"https://avatars.githubusercontent.com/u/9541799?v=3","gravatar_id":"","url":"https://api.github.com/users/sphinx-gallery","html_url":"https://github.com/sphinx-gallery","followers_url":"https://api.github.com/users/sphinx-gallery/followers","following_url":"https://api.github.com/users/sphinx-gallery/following{/other_user}","gists_url":"https://api.github.com/users/sphinx-gallery/gists{/gist_id}","starred_url":"https://api.github.com/users/sphinx-gallery/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sphinx-gallery/subscriptions","organizations_url":"https://api.github.com/users/sphinx-gallery/orgs","repos_url":"https://api.github.com/users/sphinx-gallery/repos","events_url":"https://api.github.com/users/sphinx-gallery/events{/privacy}","received_events_url":"https://api.github.com/users/sphinx-gallery/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/sphinx-gallery/sphinx-gallery","description":"Sphinx extension for automatic generation of an example gallery","fork":false,"url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery","forks_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/forks","keys_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/teams","hooks_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/hooks","issue_events_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/issues/events{/number}","events_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/events","assignees_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/assignees{/user}","branches_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/branches{/branch}","tags_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/tags","blobs_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/git/refs{/sha}","trees_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/statuses/{sha}","languages_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/languages","stargazers_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/stargazers","contributors_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/contributors","subscribers_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/subscribers","subscription_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/subscription","commits_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/commits{/sha}","git_commits_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/git/commits{/sha}","comments_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/comments{/number}","issue_comment_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/issues/comments{/number}","contents_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/contents/{+path}","compare_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/merges","archive_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/downloads","issues_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/issues{/number}","pulls_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls{/number}","milestones_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/milestones{/number}","notifications_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/labels{/name}","releases_url":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/releases{/id}","created_at":"2014-10-28T08:41:46Z","updated_at":"2015-05-01T14:03:17Z","pushed_at":"2015-05-01T14:05:08Z","git_url":"git://github.com/sphinx-gallery/sphinx-gallery.git","ssh_url":"git@github.com:sphinx-gallery/sphinx-gallery.git","clone_url":"https://github.com/sphinx-gallery/sphinx-gallery.git","svn_url":"https://github.com/sphinx-gallery/sphinx-gallery","homepage":"http://sphinx-gallery.readthedocs.org","size":841,"stargazers_count":9,"watchers_count":9,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":11,"forks":4,"open_issues":11,"watchers":9,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/26"},"html":{"href":"https://github.com/sphinx-gallery/sphinx-gallery/pull/26"},"issue":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/issues/26"},"comments":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/issues/26/comments"},"review_comments":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/26/comments"},"review_comment":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/pulls/26/commits"},"statuses":{"href":"https://api.github.com/repos/sphinx-gallery/sphinx-gallery/statuses/87b0af9bb0a8f71c4f68da4f514065e343461b4f"}}}}