def find_obj(self, env, modname, classname, name, type, searchmode=0): """Ensures an object always resolves to the desired module if defined there.""" orig_matches = PythonDomain.find_obj(self, env, modname, classname, name, type, searchmode) if len(orig_matches) <= 1: return orig_matches # If multiple matches, try to take the shortest if all the modules are # the same first_match_name_sp = orig_matches[0][0].split('.') base_name = first_match_name_sp[0] min_len = len(first_match_name_sp) best_match = orig_matches[0] for match in orig_matches[1:]: match_name = match[0] match_name_sp = match_name.split('.') match_base = match_name_sp[0] # If we have mismatched bases, return them all to trigger warnings if match_base != base_name: return orig_matches # Otherwise, check and see if it's shorter if len(match_name_sp) < min_len: min_len = len(match_name_sp) best_match = match return (best_match, )
def test_get_full_qualified_name(): env = Mock(domaindata={}) domain = PythonDomain(env) # non-python references node = nodes.reference() assert domain.get_full_qualified_name(node) is None # simple reference node = nodes.reference(reftarget='func') assert domain.get_full_qualified_name(node) == 'func' # with py:module context kwargs = {'py:module': 'module1'} node = nodes.reference(reftarget='func', **kwargs) assert domain.get_full_qualified_name(node) == 'module1.func' # with py:class context kwargs = {'py:class': 'Class'} node = nodes.reference(reftarget='func', **kwargs) assert domain.get_full_qualified_name(node) == 'Class.func' # with both py:module and py:class context kwargs = {'py:module': 'module1', 'py:class': 'Class'} node = nodes.reference(reftarget='func', **kwargs) assert domain.get_full_qualified_name(node) == 'module1.Class.func'
def find_obj(self, env, modname, classname, name, type, searchmode=0): """Ensures an object always resolves to laurelin.ldap.Object if defined there.""" desired_name = _desired_base_module + '.' + name.strip('.') matches = PythonDomain.find_obj(self, env, modname, classname, name, type, searchmode) for match in matches: match_name = match[0] if match_name == desired_name: return [match] return matches
def find_obj(self, env, modname, classname, name, type, searchmode=0): """Ensures an object always resolves to the desired module if defined there.""" orig_matches = PythonDomain.find_obj(self, env, modname, classname, name, type, searchmode) matches = [] for match in orig_matches: match_name = match[0] desired_name = "pulpcore.app.models." + name.strip('.') if match_name == desired_name: matches.append(match) break if matches: return matches else: return orig_matches
def find_obj(self, env, modname, classname, name, type, searchmode=0): """ Ensures an object always resolves to the desired module if defined there. See: https://github.com/sphinx-doc/sphinx/issues/3866 """ orig_matches = PythonDomain.find_obj(self, env, modname, classname, name, type, searchmode) matches = [] for match in orig_matches: match_name = match[0] desired_name = 'osbrain' + '.' + name.strip('.') if match_name == desired_name: matches.append(match) break if matches: return matches else: return orig_matches
def __init__(self, *args, **kwargs): self._python = PythonDomain(*args, **kwargs) super(HTTPDomain, self).__init__(*args, **kwargs)
class HTTPDomain(Domain): """HTTP domain.""" name = 'http' label = 'HTTP' object_types = { 'options': ObjType('options', 'options', 'obj'), 'head': ObjType('head', 'head', 'obj'), 'post': ObjType('post', 'post', 'obj'), 'get': ObjType('get', 'get', 'obj'), 'put': ObjType('put', 'put', 'obj'), 'patch': ObjType('patch', 'patch', 'obj'), 'delete': ObjType('delete', 'delete', 'obj'), 'trace': ObjType('trace', 'trace', 'obj'), 'connect': ObjType('connect', 'connect', 'obj'), 'copy': ObjType('copy', 'copy', 'obj'), 'any': ObjType('any', 'any', 'obj') } directives = { 'options': HTTPOptions, 'head': HTTPHead, 'post': HTTPPost, 'get': HTTPGet, 'put': HTTPPut, 'patch': HTTPPatch, 'delete': HTTPDelete, 'trace': HTTPTrace, 'connect': HTTPConnect, 'copy': HTTPCopy, 'any': HTTPAny } roles = { 'options': HTTPXRefRole('options'), 'head': HTTPXRefRole('head'), 'post': HTTPXRefRole('post'), 'get': HTTPXRefRole('get'), 'put': HTTPXRefRole('put'), 'patch': HTTPXRefRole('patch'), 'delete': HTTPXRefRole('delete'), 'trace': HTTPXRefRole('trace'), 'connect': HTTPXRefRole('connect'), 'copy': HTTPXRefRole('copy'), 'any': HTTPXRefRole('any'), 'statuscode': HTTPXRefStatusRole(), 'method': HTTPXRefMethodRole(), 'header': HTTPXRefHeaderRole() } initial_data = { 'options': {}, # path: (docname, synopsis) 'head': {}, 'post': {}, 'get': {}, 'put': {}, 'patch': {}, 'delete': {}, 'trace': {}, 'connect': {}, 'copy': {}, 'any': {} } def __init__(self, *args, **kwargs): self._python = PythonDomain(*args, **kwargs) super(HTTPDomain, self).__init__(*args, **kwargs) indices = [HTTPIndex] @property def routes(self): return dict((key, self.data[key]) for key in self.object_types) def clear_doc(self, docname): for typ, routes in self.routes.items(): for path, info in list(routes.items()): if info[0] == docname: del routes[path] def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode): try: info = self.data[str(typ)][target] except KeyError: text = contnode.rawsource role = self.roles.get(typ) if role is None: resu = self._python.resolve_xref(env, fromdocname, builder, typ, target, node, contnode) return resu if fromdocname not in _doctree_cache: _doctree_cache[fromdocname] = env.get_doctree(fromdocname) doctree = _doctree_cache[fromdocname] resnode = role.result_nodes(doctree, env, node, None)[0][0] if isinstance(resnode, addnodes.pending_xref): text = node[0][0] reporter = doctree.reporter reporter.warning('Cannot resolve reference to %r' % text, line=node.line) return None return resnode else: anchor = http_resource_anchor(typ, target) title = typ.upper() + ' ' + target return make_refnode(builder, fromdocname, info[0], anchor, contnode, title) def resolve_any_xref(self, env, fromdocname, builder, target, node, contnode): """Resolve the pending_xref *node* with the given *target*. The reference comes from an "any" or similar role, which means that Sphinx don't know the type. For now sphinxcontrib-httpdomain doesn't resolve any xref nodes. :return: list of tuples ``('domain:role', newnode)``, where ``'domain:role'`` is the name of a role that could have created the same reference, """ return [] def get_objects(self): for method, routes in self.routes.items(): for path, info in routes.items(): anchor = http_resource_anchor(method, path) yield (path, path, method, info[0], anchor, 1)
def find_obj(self, env, modname, classname, name, type, searchmode=0): orig_matches = PythonDomain.find_obj(self, env, modname, classname, name, type, searchmode) # longest match is supposed to be original definition return sorted(orig_matches, key=lambda m: len(m[0]))[-1:]