|
| 1 | +import itertools |
| 2 | +import os |
| 3 | + |
| 4 | +import jinja2 |
| 5 | +import six |
| 6 | +import sphinx |
| 7 | +import sphinx.ext.napoleon as napoleon |
| 8 | +from sphinx.ext.napoleon.docstring import GoogleDocstring |
| 9 | + |
| 10 | + |
| 11 | +def setup(app): |
| 12 | + app.connect('autodoc-process-docstring', _process_docstring) |
| 13 | + app.connect('autodoc-skip-member', napoleon._skip_member) |
| 14 | + |
| 15 | + conf = napoleon.Config._config_values |
| 16 | + |
| 17 | + for name, (default, rebuild) in six.iteritems(conf): |
| 18 | + app.add_config_value(name, default, rebuild) |
| 19 | + return {'version': sphinx.__display_version__, 'parallel_read_safe': True} |
| 20 | + |
| 21 | + |
| 22 | +def _process_docstring(app, what, name, obj, options, lines): |
| 23 | + result_lines = lines |
| 24 | + docstring = GitlabDocstring(result_lines, app.config, app, what, name, obj, |
| 25 | + options) |
| 26 | + result_lines = docstring.lines() |
| 27 | + lines[:] = result_lines[:] |
| 28 | + |
| 29 | + |
| 30 | +class GitlabDocstring(GoogleDocstring): |
| 31 | + def _build_doc(self): |
| 32 | + cls = self._obj.obj_cls |
| 33 | + md_create_list = list(itertools.chain(cls.requiredUrlAttrs, |
| 34 | + cls.requiredCreateAttrs)) |
| 35 | + opt_create_list = cls.optionalCreateAttrs |
| 36 | + |
| 37 | + md_create_keys = opt_create_keys = "None" |
| 38 | + if md_create_list: |
| 39 | + md_create_keys = "%s" % ", ".join(['``%s``' % i for i in |
| 40 | + md_create_list]) |
| 41 | + if opt_create_list: |
| 42 | + opt_create_keys = "%s" % ", ".join(['``%s``' % i for i in |
| 43 | + opt_create_list]) |
| 44 | + |
| 45 | + md_update_list = list(itertools.chain(cls.requiredUrlAttrs, |
| 46 | + cls.requiredUpdateAttrs)) |
| 47 | + opt_update_list = cls.optionalUpdateAttrs |
| 48 | + |
| 49 | + md_update_keys = opt_update_keys = "None" |
| 50 | + if md_update_list: |
| 51 | + md_update_keys = "%s" % ", ".join(['``%s``' % i for i in |
| 52 | + md_update_list]) |
| 53 | + if opt_update_list: |
| 54 | + opt_update_keys = "%s" % ", ".join(['``%s``' % i for i in |
| 55 | + opt_update_list]) |
| 56 | + |
| 57 | + tmpl_file = os.path.join(os.path.dirname(__file__), 'template.j2') |
| 58 | + with open(tmpl_file) as fd: |
| 59 | + template = jinja2.Template(fd.read(), trim_blocks=False) |
| 60 | + output = template.render(filename=tmpl_file, |
| 61 | + cls=cls, |
| 62 | + md_create_keys=md_create_keys, |
| 63 | + opt_create_keys=opt_create_keys, |
| 64 | + md_update_keys=md_update_keys, |
| 65 | + opt_update_keys=opt_update_keys) |
| 66 | + |
| 67 | + return output.split('\n') |
| 68 | + |
| 69 | + def __init__(self, *args, **kwargs): |
| 70 | + super(GitlabDocstring, self).__init__(*args, **kwargs) |
| 71 | + |
| 72 | + if not hasattr(self._obj, 'obj_cls') or self._obj.obj_cls is None: |
| 73 | + return |
| 74 | + |
| 75 | + self._parsed_lines = self._build_doc() |
0 commit comments