diff options
author | Michael Howitz <mh@gocept.com> | 2022-08-30 17:08:59 +0200 |
---|---|---|
committer | Michael Howitz <mh@gocept.com> | 2022-09-07 08:20:21 +0200 |
commit | 7c6fb15cd121a948856394d88a4d90c799bf23c7 (patch) | |
tree | 777ffde1a8e5b43718000f99bfc70e3eaed403c3 | |
parent | eb2e7aeab41cbe3006892cdb7c07e6ded707eba1 (diff) | |
download | zope-traversing-7c6fb15cd121a948856394d88a4d90c799bf23c7.tar.gz |
Lint the code.
21 files changed, 179 insertions, 130 deletions
@@ -17,3 +17,14 @@ use-flake8 = true [coverage] fail-under = 99 + +[flake8] +additional-config = [ + "# F401 imported but unused", + "# E402 module level import not at top of file", + "per-file-ignores =", + " src/zope/traversing/adapters.py: F401", + " src/zope/traversing/api.py: E402", + " src/zope/traversing/browser/__init__.py: F401", + " src/zope/traversing/interfaces.py: F401", + ] @@ -5,6 +5,13 @@ universal = 1 [flake8] doctests = 1 +# F401 imported but unused +# E402 module level import not at top of file +per-file-ignores = + src/zope/traversing/adapters.py: F401 + src/zope/traversing/api.py: E402 + src/zope/traversing/browser/__init__.py: F401 + src/zope/traversing/interfaces.py: F401 [check-manifest] ignore = @@ -27,6 +27,7 @@ def read(filename): with open(filename) as f: return f.read() + long_description = (read('README.rst') + '\n\n' + read('CHANGES.rst')) diff --git a/src/zope/traversing/adapters.py b/src/zope/traversing/adapters.py index f47a5c5..3ce5aee 100644 --- a/src/zope/traversing/adapters.py +++ b/src/zope/traversing/adapters.py @@ -157,10 +157,10 @@ def traversePathElement(obj, name, further_path, default=_marker, try: return traversable.traverse(nm, further_path) except UnicodeEncodeError: - # If we're on Python 2, and nm was a unicode string, and the traversable - # tried to do an attribute lookup, the nm would have been encoded using the - # system encoding (usually ascii). Failure to encode means invalid attribute - # name. + # If we're on Python 2, and nm was a unicode string, and the + # traversable tried to do an attribute lookup, the nm would have been + # encoded using the system encoding (usually ascii). Failure to encode + # means invalid attribute name. if default is not _marker: return default raise LocationError(obj, name) diff --git a/src/zope/traversing/api.py b/src/zope/traversing/api.py index 71feb3e..0f7ec65 100644 --- a/src/zope/traversing/api.py +++ b/src/zope/traversing/api.py @@ -206,6 +206,7 @@ def canonicalPath(path_or_object): # Break path into segments. Process '.' and '..' segments. return _normalizePath(path) + # import this down here to avoid circular imports from zope.traversing.adapters import traversePathElement diff --git a/src/zope/traversing/browser/absoluteurl.py b/src/zope/traversing/browser/absoluteurl.py index c083fad..0cdddae 100644 --- a/src/zope/traversing/browser/absoluteurl.py +++ b/src/zope/traversing/browser/absoluteurl.py @@ -40,9 +40,10 @@ from zope.traversing.browser.interfaces import IAbsoluteURL _ = MessageFactory('zope') -_insufficientContext = _("There isn't enough context to get URL information. " - "This is probably due to a bug in setting up location " - "information.") +_insufficientContext = _( + "There isn't enough context to get URL information. " + "This is probably due to a bug in setting up location " + "information.") _safe = '@+' # Characters that we don't want to have quoted @@ -50,6 +51,7 @@ _safe = '@+' # Characters that we don't want to have quoted def absoluteURL(ob, request): return zope.component.getMultiAdapter((ob, request), IAbsoluteURL)() + class _EncodedUnicode(object): def __unicode__(self): @@ -72,7 +74,7 @@ class AbsoluteURL(_EncodedUnicode, # beginning of the URL, such as skins, virtual host specifications and # so on. if (context is None - or sameProxiedObjects(context, request.getVirtualHostRoot())): + or sameProxiedObjects(context, request.getVirtualHostRoot())): return request.getApplicationURL() # first try to get the __parent__ of the object, no matter whether @@ -117,7 +119,7 @@ class AbsoluteURL(_EncodedUnicode, raise TypeError(_insufficientContext) if sameProxiedObjects(context, request.getVirtualHostRoot()) or \ - isinstance(context, Exception): + isinstance(context, Exception): return ({'name': '', 'url': self.request.getApplicationURL()}, ) base = tuple(zope.component.getMultiAdapter( diff --git a/src/zope/traversing/browser/tests.py b/src/zope/traversing/browser/tests.py index a128a9b..dde3eeb 100644 --- a/src/zope/traversing/browser/tests.py +++ b/src/zope/traversing/browser/tests.py @@ -77,8 +77,10 @@ class FooLocation(object): class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): - assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegex', - getattr(unittest.TestCase, 'assertRaisesRegexp')) + assertRaisesRegex = getattr( + unittest.TestCase, + 'assertRaisesRegex', + getattr(unittest.TestCase, 'assertRaisesRegexp')) # PY2 def setUp(self): PlacelessSetup.setUp(self) @@ -132,11 +134,11 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): breadcrumbs = view.breadcrumbs() self.assertEqual(breadcrumbs, - ({'name': '', 'url': 'http://127.0.0.1'}, + ({'name': '', 'url': 'http://127.0.0.1'}, {'name': 'a', 'url': 'http://127.0.0.1/a'}, {'name': 'b', 'url': 'http://127.0.0.1/a/b'}, {'name': 'c', 'url': 'http://127.0.0.1/a/b/c'}, - )) + )) def testParentButNoLocation(self): request = TestRequest() @@ -169,10 +171,10 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): breadcrumbs = view.breadcrumbs() self.assertEqual(breadcrumbs, - ({'name': '', 'url': 'http://127.0.0.1'}, + ({'name': '', 'url': 'http://127.0.0.1'}, {'name': 'bar', 'url': 'http://127.0.0.1/bar'}, {'name': 'foo', 'url': 'http://127.0.0.1/bar/foo'}, - )) + )) def testParentTrumpsAdapter(self): # if we have a location adapter for a content object but @@ -190,7 +192,7 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): 'http://127.0.0.1/foo') def testBasicContext_unicode(self): - #Tests so that AbsoluteURL handle unicode names as well + # Tests so that AbsoluteURL handle unicode names as well request = TestRequest() root = Root() root.__name__ = u'\u0439' @@ -209,17 +211,18 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): 'http://127.0.0.1/%D0%B9/%D1%82/%D0%B5/%D1%81') breadcrumbs = view.breadcrumbs() - self.assertEqual(breadcrumbs, - ({'name': '', 'url': 'http://127.0.0.1'}, - {'name': u'\u0439', 'url': 'http://127.0.0.1/%D0%B9'}, - {'name': u'\u0442', - 'url': 'http://127.0.0.1/%D0%B9/%D1%82'}, - {'name': u'\u0435', - 'url': 'http://127.0.0.1/%D0%B9/%D1%82/%D0%B5'}, - {'name': u'\u0441', - 'url': - 'http://127.0.0.1/%D0%B9/%D1%82/%D0%B5/%D1%81'}, - )) + self.assertEqual( + breadcrumbs, ( + {'name': '', + 'url': 'http://127.0.0.1'}, + {'name': u'\u0439', + 'url': 'http://127.0.0.1/%D0%B9'}, + {'name': u'\u0442', + 'url': 'http://127.0.0.1/%D0%B9/%D1%82'}, + {'name': u'\u0435', + 'url': 'http://127.0.0.1/%D0%B9/%D1%82/%D0%B5'}, + {'name': u'\u0441', + 'url': 'http://127.0.0.1/%D0%B9/%D1%82/%D0%B5/%D1%81'})) def testRetainSkin(self): request = TestRequest() @@ -235,11 +238,11 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): breadcrumbs = view.breadcrumbs() self.assertEqual(breadcrumbs, - ({'name': '', 'url': base + ''}, + ({'name': '', 'url': base + ''}, {'name': 'a', 'url': base + '/a'}, {'name': 'b', 'url': base + '/a/b'}, {'name': 'c', 'url': base + '/a/b/c'}, - )) + )) def testVirtualHosting(self): request = TestRequest() @@ -254,10 +257,10 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): breadcrumbs = view.breadcrumbs() self.assertEqual(breadcrumbs, - ({'name': '', 'url': 'http://127.0.0.1'}, + ({'name': '', 'url': 'http://127.0.0.1'}, {'name': 'b', 'url': 'http://127.0.0.1/b'}, {'name': 'c', 'url': 'http://127.0.0.1/b/c'}, - )) + )) def testVirtualHostingWithVHElements(self): request = TestRequest() @@ -272,10 +275,10 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): breadcrumbs = view.breadcrumbs() self.assertEqual(breadcrumbs, - ({'name': '', 'url': 'http://127.0.0.1'}, + ({'name': '', 'url': 'http://127.0.0.1'}, {'name': 'b', 'url': 'http://127.0.0.1/b'}, {'name': 'c', 'url': 'http://127.0.0.1/b/c'}, - )) + )) def testVirtualHostingInFront(self): request = TestRequest() @@ -291,11 +294,11 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): breadcrumbs = view.breadcrumbs() self.assertEqual(breadcrumbs, - ({'name': '', 'url': 'http://127.0.0.1'}, + ({'name': '', 'url': 'http://127.0.0.1'}, {'name': 'a', 'url': 'http://127.0.0.1/a'}, {'name': 'b', 'url': 'http://127.0.0.1/a/b'}, {'name': 'c', 'url': 'http://127.0.0.1/a/b/c'}, - )) + )) def testNoContextInformation(self): request = TestRequest() @@ -310,7 +313,6 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): self.assertEqual(str(view), 'http://127.0.0.1') self.assertEqual(absoluteURL(None, request), 'http://127.0.0.1') - def test_breadcrumbs_no_parent(self): view = AbsoluteURL(self, None) @@ -345,8 +347,8 @@ class TestAbsoluteURL(PlacelessSetup, unittest.TestCase): browserView(type(self), '', DummyAbsoluteURL, IAbsoluteURL) request = TestRequest() - self.assertIsInstance(zope.component.getMultiAdapter((self, request), IAbsoluteURL), - DummyAbsoluteURL) + self.assertIsInstance(zope.component.getMultiAdapter( + (self, request), IAbsoluteURL), DummyAbsoluteURL) context = Context() # First the view diff --git a/src/zope/traversing/interfaces.py b/src/zope/traversing/interfaces.py index a16b0e3..5c61f94 100644 --- a/src/zope/traversing/interfaces.py +++ b/src/zope/traversing/interfaces.py @@ -107,7 +107,8 @@ class ITraversalAPI(Interface): :param str path: a string with path segments separated by '/'. :keyword request: Passed in when traversing from presentation code. This allows paths like "@@foo" to work. - :raises zope.location.interfaces.LocationError: if *path* cannot be found + :raises zope.location.interfaces.LocationError: if *path* cannot be + found .. note:: Calling `traverse` with a path argument taken from an untrusted source, such as an HTTP request form variable, @@ -202,7 +203,6 @@ class BeforeTraverseEvent(ObjectEvent): Default implementation of `IBeforeTraverseEvent`. """ - def __init__(self, ob, request): ObjectEvent.__init__(self, ob) self.request = request diff --git a/src/zope/traversing/namespace.py b/src/zope/traversing/namespace.py index 2ecf2a1..31f4ef4 100644 --- a/src/zope/traversing/namespace.py +++ b/src/zope/traversing/namespace.py @@ -88,9 +88,8 @@ class ExcessiveDepth(LocationError): "Too many levels of containment. We don't believe them." -def namespaceLookup(ns, name, object, request=None): # pylint:disable=redefined-builtin - """ - Lookup a value from a namespace. +def namespaceLookup(ns, name, object, request=None): + """Lookup a value from a namespace. We look up a value by getting an adapter from the *object* to :class:`~zope.traversing.interfaces.ITraversable` named *ns*. If @@ -154,7 +153,7 @@ def namespaceLookup(ns, name, object, request=None): # pylint:disable=redefined- >>> from zope.testing.cleanup import cleanUp >>> cleanUp() - """ + """ # noqa: E501 line too long if request is not None: traverser = zope.component.queryMultiAdapter((object, request), ITraversable, ns) @@ -307,7 +306,7 @@ class acquire(SimpleHandler): try: # ??? what do we do if the path gets bigger? path = [] - after = traversable.traverse(name, path) # pylint:disable=too-many-function-args,assignment-from-no-return + after = traversable.traverse(name, path) if path: continue except LocationError: @@ -457,7 +456,7 @@ class lang(view): def traverse(self, name, ignored): self.request.shiftNameToApplication() languages = IModifiableUserPreferredLanguages(self.request) - languages.setPreferredLanguages([name]) # pylint:disable=too-many-function-args + languages.setPreferredLanguages([name]) return self.context @@ -543,7 +542,6 @@ class adapter(SimpleHandler): `zope.traversing.interfaces.IPathAdapter`. """"" - def traverse(self, name, ignored): """ To demonstrate this, we need to register some adapters: @@ -638,8 +636,8 @@ class debug(view): >>> Debug.providedBy(request) True - Interfaces already directly provided by the request are still provided by it - once the debug skin is applied. + Interfaces already directly provided by the request are still provided + by it once the debug skin is applied. >>> request = TestRequest() >>> class IFoo(Interface): @@ -666,9 +664,9 @@ class debug(view): ... print('unknown debugging flag') unknown debugging flag - Of course, if Python was started with the ``-O`` flag to - disable debugging, none of this is allowed (we simulate this - with a private setting on the instance): + Of course, if Python was started with the ``-O`` flag to disable + debugging, none of this is allowed (we simulate this with a private + setting on the instance): >>> adapter.enable_debug = False >>> adapter.traverse('source', ()) @@ -687,16 +685,17 @@ class debug(view): elif flag == 'tal': request.debug.showTAL = True elif flag == 'errors': - # Note that we don't use applySkin(), because it removes all existing - # skins. We may want to get tracebacks while trying to debug a - # different skin. - debug_skin = zope.component.getUtility(IBrowserSkinType, 'Debug') + # Note that we don't use applySkin(), because it removes all + # existing skins. We may want to get tracebacks while trying to + # debug a different skin. + debug_skin = zope.component.getUtility( + IBrowserSkinType, 'Debug') alsoProvides(request, debug_skin) else: raise ValueError("Unknown debug flag: %s" % flag) return self.context - if not __debug__: # pragma: no cover + if not __debug__: # pragma: no cover # If not in debug mode, we should get an error: traverse.__doc__ = """Disabled debug traversal adapter diff --git a/src/zope/traversing/publicationtraverse.py b/src/zope/traversing/publicationtraverse.py index 6c03113..301d939 100644 --- a/src/zope/traversing/publicationtraverse.py +++ b/src/zope/traversing/publicationtraverse.py @@ -47,6 +47,7 @@ class PublicationTraverser(object): - This version has a method, :meth:`traverseRelativeURL`, that supports "browserDefault" traversal. """ + def proxy(self, ob): return ProxyFactory(ob) diff --git a/src/zope/traversing/testing.py b/src/zope/traversing/testing.py index bdec349..5a54f33 100644 --- a/src/zope/traversing/testing.py +++ b/src/zope/traversing/testing.py @@ -39,6 +39,7 @@ class Contained(object): __parent__ = None __name__ = None + @zope.interface.implementer(IContained) class ContainedProxy(object): __parent__ = None @@ -55,7 +56,7 @@ class ContainedProxy(object): if name in ['__parent__', '__name__', '__obj__']: self.__dict__[name] = value return - setattr(self.__obj__, name, value) # pragma: no cover + setattr(self.__obj__, name, value) # pragma: no cover def __eq__(self, value): return self.__obj__ == value @@ -69,7 +70,9 @@ def contained(obj, root, name=None): return obj # BBB: Kept for backward-compatibility, in case some package depends on it. -def setUp(): # pragma: no cover + + +def setUp(): # pragma: no cover zope.component.provideAdapter(Traverser, (None,), ITraverser) zope.component.provideAdapter(DefaultTraversable, (None,), ITraversable) zope.component.provideAdapter(LocationPhysicallyLocatable, diff --git a/src/zope/traversing/tests/test_conveniencefunctions.py b/src/zope/traversing/tests/test_conveniencefunctions.py index a42c3f3..1f8baf7 100644 --- a/src/zope/traversing/tests/test_conveniencefunctions.py +++ b/src/zope/traversing/tests/test_conveniencefunctions.py @@ -34,11 +34,11 @@ from zope.traversing.testing import contained class C(object): __parent__ = None __name__ = None + def __init__(self, name): self.name = name - class TestFunctional(PlacelessSetup, unittest.TestCase): def setUp(self): @@ -64,7 +64,8 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): self.tr = Traverser(root) zope.component.provideAdapter(Traverser, (None,), ITraverser) - zope.component.provideAdapter(DefaultTraversable, (None,), ITraversable) + zope.component.provideAdapter( + DefaultTraversable, (None,), ITraversable) zope.component.provideAdapter(LocationPhysicallyLocatable, (None,), ILocationInfo) zope.component.provideAdapter(RootPhysicallyLocatable, @@ -75,7 +76,7 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): self.assertEqual( traverse(self.item, '/folder/item'), self.tr.traverse('/folder/item') - ) + ) def test_traverse_with_default(self): from zope.traversing.api import traverse @@ -90,7 +91,7 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): TypeError, traverse, self.unwrapped_item, '/folder/item' - ) + ) def testTraverseName(self): from zope.traversing.api import traverseName @@ -123,17 +124,17 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): LocationError, traverseName, self.folder, '../root' - ) + ) self.assertRaises( LocationError, traverseName, self.folder, '/root' - ) + ) self.assertRaises( LocationError, traverseName, self.folder, './item' - ) + ) def testTraverseNameUnicode(self): from zope.interface import implementer @@ -152,27 +153,26 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): traverseName, BrokenTraversable(), '') - def testGetName(self): from zope.traversing.api import getName self.assertEqual( getName(self.item), 'item' - ) + ) def testGetParent(self): from zope.traversing.api import getParent self.assertEqual( getParent(self.item), self.folder - ) + ) def testGetParentFromRoot(self): from zope.traversing.api import getParent self.assertEqual( getParent(self.root), None - ) + ) def testGetParentBrokenChain(self): from zope.traversing.api import getParent @@ -180,7 +180,7 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): TypeError, getParent, self.broken_chain_folder - ) + ) def testGetParentFromUnwrapped(self): from zope.traversing.api import getParent @@ -188,14 +188,14 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): TypeError, getParent, self.unwrapped_item - ) + ) def testGetParents(self): from zope.traversing.api import getParents self.assertEqual( getParents(self.item), [self.folder, self.root] - ) + ) def testGetParentsBrokenChain(self): from zope.traversing.api import getParents @@ -203,35 +203,35 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): TypeError, getParents, self.broken_chain_item - ) + ) def testGetPath(self): from zope.traversing.api import getPath self.assertEqual( getPath(self.item), u'/folder/item' - ) + ) def testGetPathOfRoot(self): from zope.traversing.api import getPath self.assertEqual( getPath(self.root), u'/', - ) + ) def testGetNameOfRoot(self): from zope.traversing.api import getName self.assertEqual( getName(self.root), u'', - ) + ) def testGetRoot(self): from zope.traversing.api import getRoot self.assertEqual( getRoot(self.item), self.root - ) + ) def testCanonicalPath(self): @@ -250,7 +250,7 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): (IndexError, '/a/../..'), (ValueError, '/a//v'), - ) + ) # sequence of N-tuples: # (loc_returned_as_string, input, input, ...) @@ -263,17 +263,17 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): # arguments to try in addition to the above '/xx/yy/zz', '/xx/./yy/ww/../zz', - ), + ), (u'/xx/yy/zz', '/xx/yy/zz', - ), + ), (u'/xx', '/xx', - ), + ), (u'/', '/', self.root, - ), + ), ) from zope.traversing.api import canonicalPath @@ -287,7 +287,6 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): self.assertEqual(canonicalPath(argument), correct_answer, "failure on %s" % argument) - def test_normalizePath(self): _bad_locations = ( @@ -295,7 +294,7 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): (ValueError, '/foo//bar'), (IndexError, '/a/../..'), (IndexError, '/a/./../..'), - ) + ) # sequence of N-tuples: # (loc_returned_as_string, input, input, ...) @@ -309,25 +308,24 @@ class TestFunctional(PlacelessSetup, unittest.TestCase): '/xx/yy/zz', '/xx/./yy/ww/../zz', '/xx/./yy/ww/./../zz', - ), + ), ('xx/yy/zz', # arguments to try in addition to the above 'xx/yy/zz', 'xx/./yy/ww/../zz', 'xx/./yy/ww/./../zz', - ), + ), ('/xx/yy/zz', '/xx/yy/zz', - ), + ), ('/xx', '/xx', - ), + ), ('/', '/', - ), + ), ) - from zope.traversing.api import _normalizePath for error_type, value in _bad_locations: @@ -390,14 +388,18 @@ class TestStandalone(unittest.TestCase): # Unlike TestFunctional, we don't register gobs of # adapters, making these tests more self-contained - assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegex', - getattr(unittest.TestCase, 'assertRaisesRegexp')) + assertRaisesRegex = getattr( + unittest.TestCase, + 'assertRaisesRegex', + getattr(unittest.TestCase, 'assertRaisesRegexp')) # PY2 def test_getParent_no_location_info(self): from zope.traversing.api import getParent test = self + class Context(object): called = False + def __conform__(self, iface): self.called = True test.assertEqual(iface, ILocationInfo) diff --git a/src/zope/traversing/tests/test_dependencies.py b/src/zope/traversing/tests/test_dependencies.py index 7466181..49fa092 100644 --- a/src/zope/traversing/tests/test_dependencies.py +++ b/src/zope/traversing/tests/test_dependencies.py @@ -1,6 +1,5 @@ import unittest -import zope.component from zope.configuration.xmlconfig import XMLConfig from zope.publisher.browser import TestRequest @@ -19,9 +18,3 @@ class ZCMLDependencies(unittest.TestCase): import zope.traversing.namespace self.assertTrue(isinstance(res, zope.traversing.namespace.lang)) self.assertTrue(res.context is self) - - -def test_suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(ZCMLDependencies)) - return suite diff --git a/src/zope/traversing/tests/test_lang.py b/src/zope/traversing/tests/test_lang.py index 23a53f8..8590161 100644 --- a/src/zope/traversing/tests/test_lang.py +++ b/src/zope/traversing/tests/test_lang.py @@ -34,6 +34,7 @@ class TestRequest(test_browserlanguages.TestRequest): def shiftNameToApplication(self): self.shifted = True + class Test(CleanUp, unittest.TestCase): def setUp(self): diff --git a/src/zope/traversing/tests/test_namespacetrversal.py b/src/zope/traversing/tests/test_namespacetrversal.py index bde1aeb..cadbd78 100644 --- a/src/zope/traversing/tests/test_namespacetrversal.py +++ b/src/zope/traversing/tests/test_namespacetrversal.py @@ -37,6 +37,7 @@ class TestSimpleHandler(unittest.TestCase): h = namespace.SimpleHandler(42, 43) self.assertEqual(h.context, 42) + class TestFunctions(unittest.TestCase): def test_getResource_not_found(self): @@ -76,6 +77,7 @@ class TestEtc(PlacelessSetup, unittest.TestCase): def test_traverse_site_no_manager(self): test = self + class Context(object): def __getattribute__(self, name): test.assertEqual(name, 'getSiteManager') @@ -91,6 +93,7 @@ class TestEtc(PlacelessSetup, unittest.TestCase): def test_traverse_site_lookup_error(self): class Context(object): called = False + def getSiteManager(self): self.called = True from zope.component import ComponentLookupError @@ -106,7 +109,8 @@ class TestEtc(PlacelessSetup, unittest.TestCase): def test_traverse_utility(self): from zope.traversing.interfaces import IEtcNamespace - component.provideUtility(self, provides=IEtcNamespace, name='my etc name') + component.provideUtility( + self, provides=IEtcNamespace, name='my etc name') result = namespace.etc(None, None).traverse('my etc name', ()) self.assertIs(result, self) @@ -124,14 +128,17 @@ class TestView(unittest.TestCase): class TestVh(unittest.TestCase): - assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegex', - getattr(unittest.TestCase, 'assertRaisesRegexp')) + assertRaisesRegex = getattr( + unittest.TestCase, + 'assertRaisesRegex', + getattr(unittest.TestCase, 'assertRaisesRegexp')) # PY2 def test_invalid_vh(self): with self.assertRaisesRegex(ValueError, 'Vhost directive should have the form'): namespace.vh(None, None).traverse(u'invalid name', ()) + def test_suite(): checker = RENormalizing([ # Python 3 includes module name in exceptions diff --git a/src/zope/traversing/tests/test_presentation.py b/src/zope/traversing/tests/test_presentation.py index 10a9039..17b9df6 100644 --- a/src/zope/traversing/tests/test_presentation.py +++ b/src/zope/traversing/tests/test_presentation.py @@ -29,15 +29,18 @@ from zope.traversing.testing import browserView class IContent(Interface): pass + @implementer(IContent) class Content(object): pass + class Resource(object): def __init__(self, request): pass + class View(object): def __init__(self, content, request): diff --git a/src/zope/traversing/tests/test_publicationtraverse.py b/src/zope/traversing/tests/test_publicationtraverse.py index 086177e..8218314 100644 --- a/src/zope/traversing/tests/test_publicationtraverse.py +++ b/src/zope/traversing/tests/test_publicationtraverse.py @@ -135,7 +135,6 @@ class TestPublicationTraverser(CleanUp, unittest.TestCase): self.assertRaises( NotFound, t.traversePath, request, ob, '/++skin++missingskin') - def test_traversePath_trailing_slash(self): class Traverser(PublicationTraverser): def __init__(self): @@ -144,7 +143,6 @@ class TestPublicationTraverser(CleanUp, unittest.TestCase): def traverseName(self, request, ob, name): self.names.append(name) - t = Traverser() t.traversePath(None, None, 'abc/def/') self.assertEqual(t.names, ['abc', 'def']) @@ -158,7 +156,6 @@ class TestPublicationTraverser(CleanUp, unittest.TestCase): # complained yet. self.assertEqual(t.names, ['abc', 'def', '', '']) - def test_traversePath_double_dots_cannot_remove(self): class Traverser(PublicationTraverser): def __init__(self): @@ -167,20 +164,20 @@ class TestPublicationTraverser(CleanUp, unittest.TestCase): def traverseName(self, request, ob, name): self.names.append(name) - t = Traverser() t.traversePath(None, None, '..') self.assertEqual(t.names, ['..']) def test_traverseRelativeURL_to_no_browser_publisher(self): test = self + class Traverser(PublicationTraverser): def traversePath(self, request, ob, path): return ob - class Context(object): called = False + def __conform__(self, iface): self.called = True test.assertEqual(iface, IBrowserPublisher) @@ -193,6 +190,7 @@ class TestPublicationTraverser(CleanUp, unittest.TestCase): self.assertTrue(context.called) + class TestBeforeTraverseEvent(unittest.TestCase): def test_interfaces(self): @@ -208,17 +206,21 @@ class TestBeforeTraverseEvent(unittest.TestCase): self.assertIs(target, ob.object) verifyObject(IBeforeTraverseEvent, ob) + class IContent(Interface): pass + @implementer(IContent) class Content(object): pass + class View(object): def __init__(self, name): self.name = name + @implementer(ITraversable) class DummyViewTraverser(object): @@ -228,6 +230,7 @@ class DummyViewTraverser(object): def traverse(self, name, furtherPath): return View(name) + @implementer(IPublishTraverse) class DummyPublishTraverse(object): @@ -237,6 +240,7 @@ class DummyPublishTraverse(object): def publishTraverse(self, request, name): return View(name) + @implementer(IBrowserPublisher) class DummyBrowserPublisher(object): diff --git a/src/zope/traversing/tests/test_skin.py b/src/zope/traversing/tests/test_skin.py index bd2c4d1..8460428 100644 --- a/src/zope/traversing/tests/test_skin.py +++ b/src/zope/traversing/tests/test_skin.py @@ -26,8 +26,11 @@ class FauxRequest(object): def shiftNameToApplication(self): self.shifted = 1 + class IFoo(Interface): pass + + directlyProvides(IFoo, IBrowserSkinType) diff --git a/src/zope/traversing/tests/test_traverser.py b/src/zope/traversing/tests/test_traverser.py index 492cccd..69ff88b 100644 --- a/src/zope/traversing/tests/test_traverser.py +++ b/src/zope/traversing/tests/test_traverser.py @@ -48,10 +48,12 @@ class ParticipationStub(object): self.principal = principal self.interaction = None + class C(Contained): def __init__(self, name): self.name = name + class TraverserTests(PlacelessSetup, unittest.TestCase): def setUp(self): @@ -72,6 +74,7 @@ class TraverserTests(PlacelessSetup, unittest.TestCase): def test_traverse_empty_path_is_context(self): self.assertIs(self.item, self.tr.traverse('')) + class UnrestrictedNoTraverseTests(unittest.TestCase): def setUp(self): self.root = root = C('root') @@ -88,11 +91,13 @@ class UnrestrictedNoTraverseTests(unittest.TestCase): self.assertRaises(LocationError, self.tr.traverse, 'folder') + class UnrestrictedTraverseTests(PlacelessSetup, unittest.TestCase): def setUp(self): PlacelessSetup.setUp(self) - zope.component.provideAdapter(DefaultTraversable, (None,), ITraversable) + zope.component.provideAdapter( + DefaultTraversable, (None,), ITraversable) zope.component.provideAdapter(LocationPhysicallyLocatable, (None,), ILocationInfo) zope.component.provideAdapter(RootPhysicallyLocatable, @@ -158,7 +163,7 @@ class UnrestrictedTraverseTests(PlacelessSetup, unittest.TestCase): 'foo': 'bar', 'anotherdict': {'bar': 'foo'}, 'items': '123', - } + } tr = Traverser(adict) self.assertEqual(tr.traverse('items'), adict.items) self.assertEqual(tr.traverse('anotherdict/bar'), 'foo') @@ -189,7 +194,8 @@ class RestrictedTraverseTests(PlacelessSetup, unittest.TestCase): def setUp(self): PlacelessSetup.setUp(self) - zope.component.provideAdapter(DefaultTraversable, (None,), ITraversable) + zope.component.provideAdapter( + DefaultTraversable, (None,), ITraversable) zope.component.provideAdapter(LocationPhysicallyLocatable, (None,), ILocationInfo) zope.component.provideAdapter(RootPhysicallyLocatable, @@ -295,9 +301,11 @@ class TestFunctions(unittest.TestCase): def test_traversePathElement_UnicodeEncodeError_with_default(self): test = self + class Traversable(object): called = False fail = test.fail + def traverse(self, nm, further_path): self.called = True u'\xff'.encode("ascii") @@ -310,10 +318,10 @@ class TestFunctions(unittest.TestCase): traversable=t)) self.assertTrue(t.called) - def test_traversePathElement_LocationError_with_default(self): class Traversable(object): called = False + def traverse(self, nm, further_path): self.called = True raise LocationError() diff --git a/src/zope/traversing/tests/test_vh.py b/src/zope/traversing/tests/test_vh.py index 70d88ec..d70c408 100644 --- a/src/zope/traversing/tests/test_vh.py +++ b/src/zope/traversing/tests/test_vh.py @@ -40,6 +40,7 @@ class TestRequest(object): del self._traversed_names[:] self._app_names = names or [] + class TestVHNamespace(unittest.TestCase): def test_vh(self): @@ -66,32 +67,22 @@ class TestVHNamespace(unittest.TestCase): handler = vh(ob, request) self.assertRaises(ValueError, handler.traverse, 'http:host:80', ()) - def test_vh_host(self): from zope.traversing.namespace import vh request = TestRequest(['folder1'], ['folder1_1', '++']) ob = object() - result = vh(ob, request).traverse('http:www.fubarco.com:80', ()) + vh(ob, request).traverse('http:www.fubarco.com:80', ()) self.assertEqual(request._app_server, 'http://www.fubarco.com:80') - def test_unicode_vh_host(self): from zope.traversing.namespace import vh request = TestRequest(['folder1'], ['folder1_1', '++']) ob = object() - result = vh(ob, request).traverse(u'http:www.fubarco.com:80', ()) - self.assertTrue(isinstance(request._app_server, str), repr(request._app_server)) - - -def test_suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.makeSuite(TestVHNamespace)) - return suite - - -if __name__ == '__main__': - unittest.main() + vh(ob, request).traverse(u'http:www.fubarco.com:80', ()) + self.assertTrue( + isinstance(request._app_server, str), + repr(request._app_server)) diff --git a/src/zope/traversing/tests/test_vhosting.py b/src/zope/traversing/tests/test_vhosting.py index 137a997..df35500 100644 --- a/src/zope/traversing/tests/test_vhosting.py +++ b/src/zope/traversing/tests/test_vhosting.py @@ -48,9 +48,11 @@ class MyObj(Contained): def __getitem__(self, key): return traverse(self, '/foo/bar/' + key) + class IFolder(zope.interface.Interface): pass + @zope.interface.implementer(IFolder, IBrowserPublisher) class Folder(Contained, dict): def __init__(self): @@ -64,7 +66,7 @@ class Folder(Contained, dict): def publishTraverse(self, request, name): subob = self.get(name, None) if subob is None: - raise NotFound(self.context, name, request) # pragma: no cover + raise NotFound(self.context, name, request) # pragma: no cover return subob @@ -87,21 +89,27 @@ class ZopeTraverser(object): object = traversePathElement(object, name, path_items, request=request) return object + + zopeTraverser = ZopeTraverser() + class PathExpr(expressions.PathExpr): def __init__(self, name, expr, engine): super(PathExpr, self).__init__(name, expr, engine, zopeTraverser) + def Engine(): e = ExpressionEngine() for pt in PathExpr._default_type_names: e.registerType(pt, PathExpr) return e + Engine = Engine() + class MyTalesPage(object): def __init__(self, source): @@ -131,6 +139,7 @@ class MyPageEval(BrowserView): index = __call__ + class MyFolderPage(BrowserView): def __call__(self, **kw): @@ -242,7 +251,7 @@ class TestVirtualHosting(unittest.TestCase): """addFolders('/a/b/c/d') would traverse and/or create three nested folders (a, b, c) and return a tuple (c, 'd') where c is a Folder instance at /a/b/c.""" - folder = self.app #self.connection.root()['Application'] + folder = self.app # self.connection.root()['Application'] if path[0] == '/': path = path[1:] path = path.split('/') @@ -331,7 +340,8 @@ class DummyPublication(object): """Post-callObject hook (if it was successful). """ - def handleException(self, ob, request, exc_info, retry_allowed=1): # pragma: no cover + def handleException( + self, ob, request, exc_info, retry_allowed=1): # pragma: no cover """Handle an exception Either: |