diff --git a/docs/conf.py b/docs/conf.py index db61cbe1a..f05c86aa5 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,8 +12,10 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys +import inspect import os +import sys +import warnings # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the @@ -31,7 +33,7 @@ extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.todo', - 'sphinx.ext.viewcode', + "sphinx.ext.linkcode", ] # Add any paths that contain templates here, relative to this directory. @@ -58,9 +60,11 @@ # built documents. # # The short X.Y version. -version = '2.4.0' +import parcels + +version = parcels.__version__ # The full version, including alpha/beta/rc tags. -release = '2.4.0' +release = version # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -162,6 +166,65 @@ "doc_path": "docs", } +# based on pandas doc/source/conf.py +def linkcode_resolve(domain, info): + """Determine the URL corresponding to Python object.""" + if domain != "py": + return None + + modname = info["module"] + fullname = info["fullname"] + + submod = sys.modules.get(modname) + if submod is None: + return None + + obj = submod + for part in fullname.split("."): + try: + with warnings.catch_warnings(): + # Accessing deprecated objects will generate noisy warnings + warnings.simplefilter("ignore", FutureWarning) + obj = getattr(obj, part) + except AttributeError: + return None + + try: + fn = inspect.getsourcefile(inspect.unwrap(obj)) + except TypeError: + try: # property + fn = inspect.getsourcefile(inspect.unwrap(obj.fget)) + except (AttributeError, TypeError): + fn = None + if not fn: + return None + + try: + source, lineno = inspect.getsourcelines(obj) + except TypeError: + try: # property + source, lineno = inspect.getsourcelines(obj.fget) + except (AttributeError, TypeError): + lineno = None + except OSError: + lineno = None + + if lineno: + linespec = f"#L{lineno}-L{lineno + len(source) - 1}" + else: + linespec = "" + + fn = os.path.relpath(fn, start=os.path.dirname(parcels.__file__)) + + if "-" in parcels.__version__: + return f"https://github.com/OceanParcels/parcels/blob/master/parcels/{fn}{linespec}" + else: + return ( + f"https://github.com/OceanParcels/parcels/blob/" + f"{parcels.__version__}/parcels/{fn}{linespec}" + ) + + # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation.