diff options
| author | Georg Brandl <georg@python.org> | 2011-09-23 11:03:23 +0200 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2011-09-23 11:03:23 +0200 |
| commit | 75606c01aab9d04574114e994f420c3619c7f71b (patch) | |
| tree | 370b12cba4f2d19a7405c4efef31c408108def8f /sphinx/domains/python.py | |
| parent | b03a49a513b97643b13f751350606400ee97af44 (diff) | |
| parent | 90ee1df433d7e6443eba29a5a0da9d49f7493cee (diff) | |
| download | sphinx-75606c01aab9d04574114e994f420c3619c7f71b.tar.gz | |
Merge with 1.0
Diffstat (limited to 'sphinx/domains/python.py')
| -rw-r--r-- | sphinx/domains/python.py | 107 |
1 files changed, 68 insertions, 39 deletions
diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index e812b610..98d93545 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -88,6 +88,7 @@ class PyObject(ObjectDescription): option_spec = { 'noindex': directives.flag, 'module': directives.unchanged, + 'annotation': directives.unchanged, } doc_field_types = [ @@ -110,22 +111,21 @@ class PyObject(ObjectDescription): ] def get_signature_prefix(self, sig): - """ - May return a prefix to put before the object name in the signature. + """May return a prefix to put before the object name in the + signature. """ return '' def needs_arglist(self): - """ - May return true if an empty argument list is to be generated even if + """May return true if an empty argument list is to be generated even if the document contains none. """ return False def handle_signature(self, sig, signode): - """ - Transform a Python signature into RST nodes. - Returns (fully qualified name of the thing, classname if any). + """Transform a Python signature into RST nodes. + + Return (fully qualified name of the thing, classname if any). If inside a class, the current class name is handled intelligently: * it is stripped from the displayed name if present @@ -181,6 +181,8 @@ class PyObject(ObjectDescription): nodetext = modname + '.' signode += addnodes.desc_addname(nodetext, nodetext) + anno = self.options.get('annotation') + signode += addnodes.desc_name(name, name) if not arglist: if self.needs_arglist(): @@ -188,16 +190,19 @@ class PyObject(ObjectDescription): signode += addnodes.desc_parameterlist() if retann: signode += addnodes.desc_returns(retann, retann) + if anno: + signode += addnodes.desc_annotation(' ' + anno, ' ' + anno) return fullname, name_prefix + _pseudo_parse_arglist(signode, arglist) if retann: signode += addnodes.desc_returns(retann, retann) + if anno: + signode += addnodes.desc_annotation(' ' + anno, ' ' + anno) return fullname, name_prefix def get_index_text(self, modname, name): - """ - Return the text for the index entry of the object. - """ + """Return the text for the index entry of the object.""" raise NotImplementedError('must be implemented in subclasses') def add_target_and_index(self, name_cls, sig, signode): @@ -223,7 +228,7 @@ class PyObject(ObjectDescription): indextext = self.get_index_text(modname, name_cls) if indextext: self.indexnode['entries'].append(('single', indextext, - fullname, fullname)) + fullname, '')) def before_content(self): # needed for automatic qualification of members (reset in subclasses) @@ -359,6 +364,38 @@ class PyClassmember(PyObject): self.clsname_set = True +class PyDecoratorMixin(object): + """ + Mixin for decorator directives. + """ + def handle_signature(self, sig, signode): + ret = super(PyDecoratorMixin, self).handle_signature(sig, signode) + signode.insert(0, addnodes.desc_addname('@', '@')) + return ret + + def needs_arglist(self): + return False + + +class PyDecoratorFunction(PyDecoratorMixin, PyModulelevel): + """ + Directive to mark functions meant to be used as decorators. + """ + def run(self): + # a decorator function is a function after all + self.name = 'py:function' + return PyModulelevel.run(self) + + +class PyDecoratorMethod(PyDecoratorMixin, PyClassmember): + """ + Directive to mark methods meant to be used as decorators. + """ + def run(self): + self.name = 'py:method' + return PyClassmember.run(self) + + class PyModule(Directive): """ Directive to mark description of a new module. @@ -385,26 +422,17 @@ class PyModule(Directive): env.domaindata['py']['modules'][modname] = \ (env.docname, self.options.get('synopsis', ''), self.options.get('platform', ''), 'deprecated' in self.options) - # make a duplicate entry in 'objects' to facilitate searching for - # the module in PythonDomain.find_obj() + # make a duplicate entry in 'objects' to facilitate searching for the + # module in PythonDomain.find_obj() env.domaindata['py']['objects'][modname] = (env.docname, 'module') - targetnode = nodes.target('', '', ids=['module-' + modname], - ismod=True) + targetnode = nodes.target('', '', ids=['module-' + modname], ismod=True) self.state.document.note_explicit_target(targetnode) + # the platform and synopsis aren't printed; in fact, they are only used + # in the modindex currently ret.append(targetnode) - # XXX this behavior of the module directive is a mess... - if 'platform' in self.options: - platform = self.options['platform'] - node = nodes.paragraph() - node += nodes.emphasis('', _('Platforms: ')) - node += nodes.Text(platform, platform) - ret.append(node) - # the synopsis isn't printed; in fact, it is only used in the - # modindex currently - if not noindex: indextext = _('%s (module)') % modname inode = addnodes.index(entries=[('single', indextext, - 'module-' + modname, modname)]) + 'module-' + modname, '')]) ret.append(inode) return ret @@ -539,16 +567,18 @@ class PythonDomain(Domain): } directives = { - 'function': PyModulelevel, - 'data': PyModulelevel, - 'class': PyClasslike, - 'exception': PyClasslike, - 'method': PyClassmember, - 'classmethod': PyClassmember, - 'staticmethod': PyClassmember, - 'attribute': PyClassmember, - 'module': PyModule, - 'currentmodule': PyCurrentModule, + 'function': PyModulelevel, + 'data': PyModulelevel, + 'class': PyClasslike, + 'exception': PyClasslike, + 'method': PyClassmember, + 'classmethod': PyClassmember, + 'staticmethod': PyClassmember, + 'attribute': PyClassmember, + 'module': PyModule, + 'currentmodule': PyCurrentModule, + 'decorator': PyDecoratorFunction, + 'decoratormethod': PyDecoratorMethod, } roles = { 'data': PyXRefRole(), @@ -578,9 +608,8 @@ class PythonDomain(Domain): del self.data['modules'][modname] def find_obj(self, env, modname, classname, name, type, searchmode=0): - """ - Find a Python object for "name", perhaps using the given module and/or - classname. Returns a list of (name, object entry) tuples. + """Find a Python object for "name", perhaps using the given module + and/or classname. Returns a list of (name, object entry) tuples. """ # skip parens if name[-2:] == '()': |
