Skip to content

Commit

Permalink
Sphinx: Inline github link creation code
Browse files Browse the repository at this point in the history
  • Loading branch information
ottonemo committed Oct 27, 2017
1 parent 47e7f6f commit 2a0a6e6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 92 deletions.
87 changes: 81 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from sphinxext.github_link import make_linkcode_resolve


sys.path.insert(0, os.path.abspath('..'))


Expand Down Expand Up @@ -179,10 +178,86 @@ def setup(app):
'Miscellaneous'),
]

# -- GitHub source code link ----------------------------------------------

# Functionality to build github source URI, taken from sklearn.

from operator import attrgetter
import inspect
import subprocess
from functools import partial

REVISION_CMD = 'git rev-parse --short HEAD'

def _get_git_revision():
try:
revision = subprocess.check_output(REVISION_CMD.split()).strip()
except (subprocess.CalledProcessError, OSError):
print('Failed to execute git to get revision')
return None
return revision.decode('utf-8')


def _linkcode_resolve(domain, info, package, url_fmt, revision):
"""Determine a link to online source for a class/method/function
This is called by sphinx.ext.linkcode
An example with a long-untouched module that everyone has
>>> _linkcode_resolve('py', {'module': 'tty',
... 'fullname': 'setraw'},
... package='tty',
... url_fmt='http://hg.python.org/cpython/file/'
... '{revision}/Lib/{package}/{path}#L{lineno}',
... revision='xxxx')
'http://hg.python.org/cpython/file/xxxx/Lib/tty/tty.py#L18'
"""

if revision is None:
return
if domain not in ('py', 'pyx'):
return
if not info.get('module') or not info.get('fullname'):
return

class_name = info['fullname'].split('.')[0]
if type(class_name) != str:
# Python 2 only
class_name = class_name.encode('utf-8')
module = __import__(info['module'], fromlist=[class_name])
obj = attrgetter(info['fullname'])(module)

try:
fn = inspect.getsourcefile(obj)
except Exception:
fn = None
if not fn:
try:
fn = inspect.getsourcefile(sys.modules[obj.__module__])
except Exception:
fn = None
if not fn:
return

fn = os.path.relpath(fn,
start=os.path.dirname(__import__(package).__file__))
try:
lineno = inspect.getsourcelines(obj)[1]
except Exception:
lineno = ''
return url_fmt.format(revision=revision, package=package,
path=fn, lineno=lineno)

def project_linkcode_resolve(domain, info):
global _linkcode_git_revision
return _linkcode_resolve(domain, info,
package='skorch',
revision=_linkcode_git_revision,
url_fmt='https://github.com/dnouri/skorch/'
'blob/{revision}/'
'{package}/{path}#L{lineno}')

_linkcode_git_revision = _get_git_revision()

# The following is used by sphinx.ext.linkcode to provide links to github
linkcode_resolve = make_linkcode_resolve(
'skorch',
'https://github.com/dnouri/skorch/'
'blob/{revision}/'
'{package}/{path}#L{lineno}')
linkcode_resolve = project_linkcode_resolve
Empty file removed docs/sphinxext/__init__.py
Empty file.
86 changes: 0 additions & 86 deletions docs/sphinxext/github_link.py

This file was deleted.

0 comments on commit 2a0a6e6

Please sign in to comment.