mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2026-09-03 20:52:55 -05:00
inheritance_diagram: Move _import_class_or_module() method to function
This commit is contained in:
@@ -63,32 +63,7 @@ class_sig_re = re.compile(r'''^([\w.]*\.)? # module names
|
||||
''', re.VERBOSE)
|
||||
|
||||
|
||||
class InheritanceException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InheritanceGraph(object):
|
||||
"""
|
||||
Given a list of classes, determines the set of classes that they inherit
|
||||
from all the way to the root "object", and then is able to generate a
|
||||
graphviz dot graph from them.
|
||||
"""
|
||||
def __init__(self, class_names, currmodule, show_builtins=False,
|
||||
private_bases=False, parts=0):
|
||||
"""*class_names* is a list of child classes to show bases from.
|
||||
|
||||
If *show_builtins* is True, then Python builtins will be shown
|
||||
in the graph.
|
||||
"""
|
||||
self.class_names = class_names
|
||||
classes = self._import_classes(class_names, currmodule)
|
||||
self.class_info = self._class_info(classes, show_builtins,
|
||||
private_bases, parts)
|
||||
if not self.class_info:
|
||||
raise InheritanceException('No classes found for '
|
||||
'inheritance diagram')
|
||||
|
||||
def _import_class_or_module(self, name, currmodule):
|
||||
def import_classes(name, currmodule):
|
||||
"""Import a class using its fully-qualified *name*."""
|
||||
try:
|
||||
path, base = class_sig_re.match(name).groups()
|
||||
@@ -133,11 +108,37 @@ class InheritanceGraph(object):
|
||||
raise InheritanceException('%r specified for inheritance diagram is '
|
||||
'not a class or module' % name)
|
||||
|
||||
|
||||
class InheritanceException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InheritanceGraph(object):
|
||||
"""
|
||||
Given a list of classes, determines the set of classes that they inherit
|
||||
from all the way to the root "object", and then is able to generate a
|
||||
graphviz dot graph from them.
|
||||
"""
|
||||
def __init__(self, class_names, currmodule, show_builtins=False,
|
||||
private_bases=False, parts=0):
|
||||
"""*class_names* is a list of child classes to show bases from.
|
||||
|
||||
If *show_builtins* is True, then Python builtins will be shown
|
||||
in the graph.
|
||||
"""
|
||||
self.class_names = class_names
|
||||
classes = self._import_classes(class_names, currmodule)
|
||||
self.class_info = self._class_info(classes, show_builtins,
|
||||
private_bases, parts)
|
||||
if not self.class_info:
|
||||
raise InheritanceException('No classes found for '
|
||||
'inheritance diagram')
|
||||
|
||||
def _import_classes(self, class_names, currmodule):
|
||||
"""Import a list of classes."""
|
||||
classes = []
|
||||
for name in class_names:
|
||||
classes.extend(self._import_class_or_module(name, currmodule))
|
||||
classes.extend(import_classes(name, currmodule))
|
||||
return classes
|
||||
|
||||
def _class_info(self, classes, show_builtins, private_bases, parts):
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
# example.py
|
||||
@@ -0,0 +1,5 @@
|
||||
# example.sphinx
|
||||
|
||||
|
||||
class DummyClass(object):
|
||||
pass
|
||||
@@ -9,9 +9,54 @@
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
|
||||
from util import with_app
|
||||
import sys
|
||||
from util import with_app, rootdir, raises
|
||||
from sphinx.ext.inheritance_diagram import InheritanceException, import_classes
|
||||
|
||||
|
||||
@with_app('html', testroot='ext-inheritance_diagram')
|
||||
def test_inheritance_diagram_html(app, status, warning):
|
||||
app.builder.build_all()
|
||||
|
||||
|
||||
def test_import_classes():
|
||||
from sphinx.application import Sphinx, TemplateBridge
|
||||
|
||||
try:
|
||||
sys.path.append(rootdir / 'roots/test-ext-inheritance_diagram')
|
||||
|
||||
# got exception for unknown class or module
|
||||
raises(InheritanceException, import_classes, 'unknown', None)
|
||||
raises(InheritanceException, import_classes, 'unknown.Unknown', None)
|
||||
|
||||
# a module having no classes
|
||||
classes = import_classes('sphinx', None)
|
||||
assert classes == []
|
||||
|
||||
classes = import_classes('sphinx', 'foo')
|
||||
assert classes == []
|
||||
|
||||
# all of classes in the module
|
||||
classes = import_classes('sphinx.application', None)
|
||||
assert set(classes) == set([Sphinx, TemplateBridge])
|
||||
|
||||
# specified class in the module
|
||||
classes = import_classes('sphinx.application.Sphinx', None)
|
||||
assert classes == [Sphinx]
|
||||
|
||||
# specified class in current module
|
||||
classes = import_classes('Sphinx', 'sphinx.application')
|
||||
assert classes == [Sphinx]
|
||||
|
||||
# ignore current module if name include the module name
|
||||
raises(InheritanceException, import_classes, 'i18n.CatalogInfo', 'sphinx.util')
|
||||
|
||||
# got exception for functions
|
||||
raises(InheritanceException, import_classes, 'encode_uri', 'sphinx.util')
|
||||
|
||||
# try to load example.sphinx, but inheritance_diagram imports sphinx instead
|
||||
# refs: #3164
|
||||
classes = import_classes('sphinx', 'example')
|
||||
assert classes == []
|
||||
finally:
|
||||
sys.path.pop()
|
||||
|
||||
Reference in New Issue
Block a user