diff --git a/jandd/sphinxext/ip.py b/jandd/sphinxext/ip.py index ed2dc04..96efbe8 100644 --- a/jandd/sphinxext/ip.py +++ b/jandd/sphinxext/ip.py @@ -18,14 +18,18 @@ from docutils.parsers.rst import Directive from sphinx import addnodes from sphinx.domains import Domain, ObjType -from sphinx.environment import NoUri -from sphinx.locale import l_ +from sphinx.errors import NoUri +from sphinx.locale import _ from sphinx.roles import XRefRole +from sphinx.util import logging from sphinx.util.nodes import make_refnode __version__ = '0.2.5.dev1' +logger = logging.getLogger(__name__) + + def ip_object_anchor(typ, path): path = re.sub(r'[.:/]', '-', path) return typ.lower() + '-' + path @@ -52,13 +56,17 @@ class IPXRefRole(XRefRole): super(IPXRefRole, self).__init__( innernodeclass=innernodeclass, **kwargs) - def __call__(self, typ, rawtext, text, lineno, inliner, - options={}, content=[]): + def __cal__(self, typ, rawtext, text, lineno, inliner, + options=None, content=None): + if content is None: + content = [] + if options is None: + options = {} try: Network(text) except ValueError as e: env = inliner.document.settings.env - env.warn(env.docname, "invalid ip address/range %s" % text, lineno) + logger.warning("invalid ip address/range %s" % text, location=(env.docname, lineno)) return [nodes.literal(text, text), []] return super(IPXRefRole, self).__call__( typ, rawtext, text, lineno, inliner, options, content) @@ -140,20 +148,20 @@ class IPv4Range(IPRange): typ = 'v4range' def get_prefix_title(self): - return l_('IPv4 address range ') + return _('IPv4 address range ') def get_index_text(self): - return "%s; %s" % (l_('IPv4 range'), self.rangespec) + return "%s; %s" % (_('IPv4 range'), self.rangespec) class IPv6Range(IPRange): typ = 'v6range' def get_prefix_title(self): - return l_('IPv6 address range ') + return _('IPv6 address range ') def get_index_text(self): - return "%s; %s" % (l_('IPv6 range'), self.rangespec) + return "%s; %s" % (_('IPv6 range'), self.rangespec) class IPDomain(Domain): @@ -164,10 +172,10 @@ class IPDomain(Domain): label = 'IP addresses and ranges.' object_types = { - 'v4': ObjType(l_('v4'), 'v4', 'obj'), - 'v6': ObjType(l_('v6'), 'v6', 'obj'), - 'v4range': ObjType(l_('v4range'), 'v4range', 'obj'), - 'v6range': ObjType(l_('v6range'), 'v6range', 'obj'), + 'v4': ObjType(_('v4'), 'v4', 'obj'), + 'v6': ObjType(_('v6'), 'v6', 'obj'), + 'v4range': ObjType(_('v4range'), 'v4range', 'obj'), + 'v6range': ObjType(_('v6range'), 'v6range', 'obj'), } directives = { @@ -176,10 +184,10 @@ class IPDomain(Domain): } roles = { - 'v4': IPXRefRole('v4', l_('IPv4 address')), - 'v6': IPXRefRole('v6', l_('IPv6 address')), - 'v4range': IPXRefRole('v4range', l_('IPv4 range')), - 'v6range': IPXRefRole('v6range', l_('IPv6 range')), + 'v4': IPXRefRole('v4', _('IPv4 address')), + 'v6': IPXRefRole('v6', _('IPv6 address')), + 'v4range': IPXRefRole('v4range', _('IPv4 range')), + 'v6range': IPXRefRole('v6range', _('IPv6 range')), } initial_data = { @@ -277,7 +285,7 @@ def process_ip_nodes(app, doctree, fromdocname): env = app.builder.env domaindata = env.domaindata[IPDomain.name] - header = (l_('IP address'), l_('Used by')) + header = (_('IP address'), _('Used by')) colwidths = (1, 3) for node in doctree.traverse(ip_range): @@ -286,8 +294,14 @@ def process_ip_nodes(app, doctree, fromdocname): ips = {} for key, value in [ (ip_info['ip'], ip_info) for ip_info in - domaindata['ips'] if ip_info['ip'] in net + domaindata['ips'] ]: + try: + if not key in net: + continue + except ValueError as e: + logger.info("invalid IP address info %s", e.args) + continue addrlist = ips.get(key, []) addrlist.append(value) ips[key] = addrlist @@ -337,7 +351,7 @@ def process_ip_nodes(app, doctree, fromdocname): tbody += create_table_row([para, refnode]) content.append(table) else: - para = nodes.paragraph(l_('No IP addresses in this range')) + para = nodes.paragraph(_('No IP addresses in this range')) content.append(para) node.replace_self(content) diff --git a/tests/run.py b/tests/run.py index 95236c2..64be58a 100755 --- a/tests/run.py +++ b/tests/run.py @@ -29,13 +29,11 @@ def run(extra_args=[]): print("The sphinx package is needed to run the jandd.sphinxext.ip " "test suite.") - import test_ip + from .test_ip import TestIPExtension print("Running jandd.sphinxext.ip test suite ...") - suite = unittest.TestLoader().loadTestsFromTestCase( - test_ip.TestIPExtension - ) + suite = unittest.TestLoader().loadTestsFromTestCase(TestIPExtension) unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/tests/test_ip.py b/tests/test_ip.py index 8d83348..a150025 100644 --- a/tests/test_ip.py +++ b/tests/test_ip.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from io import StringIO -from util import TestApp, test_root +from .util import TestApp, test_root import unittest diff --git a/tests/util.py b/tests/util.py index 1420769..d3df98a 100644 --- a/tests/util.py +++ b/tests/util.py @@ -7,21 +7,14 @@ :license: BSD, see LICENSE for details. """ -import sys import io -import tempfile import shutil - -try: - from functools import wraps -except ImportError: - # functools is new in 2.4 - wraps = lambda f: (lambda w: w) - -from sphinx import application -from sphinx.ext.autodoc import AutoDirective +import sys +import tempfile +from functools import wraps from path import Path +from sphinx import application __all__ = [ 'test_root', @@ -40,6 +33,7 @@ def _excstr(exc): return str(tuple(map(_excstr, exc))) return exc.__name__ + def raises(exc, func, *args, **kwds): """ Raise :exc:`AssertionError` if ``func(*args, **kwds)`` does not @@ -53,6 +47,7 @@ def raises(exc, func, *args, **kwds): raise AssertionError('%s did not raise %s' % (func.__name__, _excstr(exc))) + def raises_msg(exc, msg, func, *args, **kwds): """ Raise :exc:`AssertionError` if ``func(*args, **kwds)`` does not @@ -71,6 +66,7 @@ class Struct(object): def __init__(self, **kwds): self.__dict__.update(kwds) + class ListOutput(object): """ File-like object that collects written text in a list. @@ -85,6 +81,7 @@ class ListOutput(object): def write(self, text): self.content.append(text) + class TestApp(application.Sphinx): """ A subclass of :class:`Sphinx` that runs on the test root, with some @@ -139,7 +136,6 @@ class TestApp(application.Sphinx): freshenv, warningiserror, tags) def cleanup(self, doctrees=False): - AutoDirective._registry.clear() for tree in self.cleanup_trees: shutil.rmtree(tree, True) @@ -176,6 +172,7 @@ def gen_with_app(*args, **kwargs): return deco return generator + def with_tempdir(func): def new_func(): tempdir = Path(tempfile.mkdtemp()) @@ -184,10 +181,12 @@ def with_tempdir(func): new_func.__name__ = func.__name__ return new_func + def write_file(name, contents): f = open(str(name), 'wb') f.write(contents) f.close() + def sprint(*args): sys.stderr.write(' '.join(map(str, args)) + '\n')