[PATCH 2 of 7 sherpa] [doc] Implement ReST directive to include python package data file

Sylvain Thenault sylvain.thenault at logilab.fr
Wed Apr 12 11:46:51 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1491918335 -7200
#      Tue Apr 11 15:45:35 2017 +0200
# Node ID b955af3f20c12a91030a437de13d461a72948a8c
# Parent  343d5fb44946daa4e52f5d736d1386d5e5dd6b96
[doc] Implement ReST directive to include python package data file

diff --git a/MANIFEST.in b/MANIFEST.in
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,11 +1,11 @@
 recursive-include cubicweb_sherpa *.py
 recursive-include cubicweb_sherpa/data *.gif *.png *.ico *.css *.js *.otf *.ttf *.txt
 recursive-include cubicweb_sherpa/i18n *.po *.pot
 include cubicweb_sherpa/views/templates/*.html
 include tox.ini
-include doc/Makefile doc/conf.py doc/*.rst
+include doc/Makefile doc/*.py doc/*.rst
 include dev-requirements.txt
 include test/*.py test/data/bootstrap_cubes
 
 prune __pkginfo__.py
 prune docker
diff --git a/doc/rext.py b/doc/rext.py
new file mode 100644
--- /dev/null
+++ b/doc/rext.py
@@ -0,0 +1,34 @@
+"""ReST extensions"""
+
+from pkg_resources import Requirement, resource_filename, resource_exists
+
+from docutils.parsers.rst.directives import misc, register_directive
+
+
+class PkgResourcesInclude(misc.Include):
+    """Implement the `pkg-resources-include` directive, similar to the `include`
+    directive but taking a pypi project name as first argument and a data file
+    within the package as second argument.
+
+    Actual file location will be computed using pkg_resources facilities.
+
+    Example:
+
+        .. pkg-resources-include:: cubicweb-seda doc/profils.rst
+
+    """
+
+    required_arguments = 2
+
+    def run(self):
+        project, fpath = self.arguments
+
+        if not resource_exists(Requirement.parse(project), fpath):
+            raise Exception('Resource %s not found in %s' % (fpath, project))
+
+        filename = resource_filename(Requirement.parse(project), fpath)
+        self.arguments = [filename]
+        return super(PkgResourcesInclude, self).run()
+
+
+register_directive('pkg-resources-include', PkgResourcesInclude)


More information about the saem-devel mailing list