summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSouheil CHELFOUH <trollfot@gmail.com>2011-03-25 17:06:56 +0000
committerSouheil CHELFOUH <trollfot@gmail.com>2011-03-25 17:06:56 +0000
commit27eba12961bfc9512108c8a2cba877323f7e26ed (patch)
tree19f3b293e6f5dda30523ffc86c43ee9be0f73fc9 /src
parentba2d6c00c2634a19e35a469487f2c9a92fa418d1 (diff)
downloadzope-traversing-27eba12961bfc9512108c8a2cba877323f7e26ed.tar.gz
Standard cleanup :
- Removed unused imports - Removed unused dependency on zope.tal - Added missing dependency on zope.browserpage - Cosmetics including pep8 normalization
Diffstat (limited to 'src')
-rw-r--r--src/zope/traversing/adapters.py10
-rw-r--r--src/zope/traversing/api.py10
-rw-r--r--src/zope/traversing/browser/absoluteurl.py11
-rw-r--r--src/zope/traversing/browser/interfaces.py2
-rw-r--r--src/zope/traversing/browser/tests.py21
-rw-r--r--src/zope/traversing/namespace.py20
-rw-r--r--src/zope/traversing/publicationtraverse.py9
-rw-r--r--src/zope/traversing/testing.py2
8 files changed, 60 insertions, 25 deletions
diff --git a/src/zope/traversing/adapters.py b/src/zope/traversing/adapters.py
index 353e4d1..4ebcc6a 100644
--- a/src/zope/traversing/adapters.py
+++ b/src/zope/traversing/adapters.py
@@ -16,19 +16,18 @@
from types import StringTypes
import zope.interface
-import zope.component
from zope.location.interfaces import ILocationInfo, LocationError
from zope.traversing.interfaces import ITraversable, ITraverser
-
from zope.traversing.namespace import namespaceLookup
-from zope.traversing.namespace import UnexpectedParameters
from zope.traversing.namespace import nsParse
-from zope.location.traversing import RootPhysicallyLocatable # BBB
+from zope.location.traversing import RootPhysicallyLocatable # BBB
+
_marker = object() # opaque marker that doesn't get security proxied
+
class DefaultTraversable(object):
"""Traverses objects via attribute and item lookup"""
zope.interface.implements(ITraversable)
@@ -49,6 +48,7 @@ class DefaultTraversable(object):
pass
raise LocationError(subject, name)
+
class Traverser(object):
"""Provide traverse features"""
@@ -119,7 +119,7 @@ def traversePathElement(obj, name, further_path, default=_marker,
if name == '..':
return obj.__parent__
-
+
if name and name[:1] in '@+':
ns, nm = nsParse(name)
if ns:
diff --git a/src/zope/traversing/api.py b/src/zope/traversing/api.py
index a921329..cc00bd8 100644
--- a/src/zope/traversing/api.py
+++ b/src/zope/traversing/api.py
@@ -14,7 +14,7 @@
"""Convenience functions for traversing the object tree.
"""
from zope.interface import moduleProvides
-from zope.location.interfaces import ILocationInfo, IRoot, LocationError
+from zope.location.interfaces import ILocationInfo, IRoot
from zope.traversing.interfaces import ITraversalAPI, ITraverser
@@ -23,6 +23,7 @@ __all__ = tuple(ITraversalAPI)
_marker = object()
+
def joinPath(path, *args):
"""Join the given relative paths to the given path.
@@ -54,16 +55,19 @@ def joinPath(path, *args):
raise ValueError("Leading or trailing slashes in path elements")
return _normalizePath(path + u'/'.join(args))
+
def getPath(obj):
"""Returns a string representing the physical path to the object.
"""
return ILocationInfo(obj).getPath()
+
def getRoot(obj):
"""Returns the root of the traversal for the given object.
"""
return ILocationInfo(obj).getRoot()
+
def traverse(object, path, default=_marker, request=None):
"""Traverse 'path' relative to the given object.
@@ -86,6 +90,7 @@ def traverse(object, path, default=_marker, request=None):
else:
return traverser.traverse(path, default=default, request=request)
+
def traverseName(obj, name, default=_marker, traversable=None, request=None):
"""Traverse a single step 'name' relative to the given object.
@@ -115,11 +120,13 @@ def traverseName(obj, name, default=_marker, traversable=None, request=None):
else:
return obj
+
def getName(obj):
"""Get the name an object was traversed via
"""
return ILocationInfo(obj).getName()
+
def getParent(obj):
"""Returns the container the object was traversed via.
@@ -185,6 +192,7 @@ def _normalizePath(path):
return prefix + u'/'.join(new_segments)
+
def canonicalPath(path_or_object):
"""Returns a canonical absolute unicode path for the given path or object.
diff --git a/src/zope/traversing/browser/absoluteurl.py b/src/zope/traversing/browser/absoluteurl.py
index 54602e2..aa05328 100644
--- a/src/zope/traversing/browser/absoluteurl.py
+++ b/src/zope/traversing/browser/absoluteurl.py
@@ -27,11 +27,13 @@ _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
+_safe = '@+' # Characters that we don't want to have quoted
+
def absoluteURL(ob, request):
return zope.component.getMultiAdapter((ob, request), IAbsoluteURL)()
+
class AbsoluteURL(BrowserView):
implements(IAbsoluteURL)
@@ -92,7 +94,7 @@ class AbsoluteURL(BrowserView):
if sameProxiedObjects(context, request.getVirtualHostRoot()) or \
isinstance(context, Exception):
- return ({'name':'', 'url': self.request.getApplicationURL()}, )
+ return ({'name': '', 'url': self.request.getApplicationURL()}, )
base = tuple(zope.component.getMultiAdapter(
(container, request), IAbsoluteURL).breadcrumbs())
@@ -110,6 +112,7 @@ class AbsoluteURL(BrowserView):
return base
+
class SiteAbsoluteURL(BrowserView):
implements(IAbsoluteURL)
@@ -138,9 +141,9 @@ class SiteAbsoluteURL(BrowserView):
request = self.request
if sameProxiedObjects(context, request.getVirtualHostRoot()):
- return ({'name':'', 'url': self.request.getApplicationURL()}, )
+ return ({'name': '', 'url': self.request.getApplicationURL()}, )
- base = ({'name':'', 'url': self.request.getApplicationURL()}, )
+ base = ({'name': '', 'url': self.request.getApplicationURL()}, )
name = getattr(context, '__name__', None)
if name:
diff --git a/src/zope/traversing/browser/interfaces.py b/src/zope/traversing/browser/interfaces.py
index e6d8c5a..0ef4f62 100644
--- a/src/zope/traversing/browser/interfaces.py
+++ b/src/zope/traversing/browser/interfaces.py
@@ -15,6 +15,7 @@
"""
from zope.interface import Interface
+
class IAbsoluteURL(Interface):
def __unicode__():
@@ -36,6 +37,7 @@ class IAbsoluteURL(Interface):
URL is the link for that segment of the breadcrumbs.
"""
+
class IAbsoluteURLAPI(Interface):
def absoluteURL(ob, request):
diff --git a/src/zope/traversing/browser/tests.py b/src/zope/traversing/browser/tests.py
index a52baf5..4235f04 100644
--- a/src/zope/traversing/browser/tests.py
+++ b/src/zope/traversing/browser/tests.py
@@ -31,21 +31,27 @@ from zope.location.location import LocationProxy
from zope.container.contained import contained
+
class IRoot(Interface):
pass
+
class Root(object):
implements(IRoot)
+
class TrivialContent(object):
"""Trivial content object, used because instances of object are rocks."""
+
class AdaptedContent(object):
"""A simple content object that has an ILocation adapter for it."""
-
+
+
class FooContent(object):
"""Class whose location will be provided by an adapter."""
+
class FooLocation(object):
"""Adapts FooAdapter to the ILocation protocol."""
implements(ILocation)
@@ -62,6 +68,7 @@ class FooLocation(object):
def __parent__(self):
return contained(TrivialContent(), Root(), name='bar')
+
class TestAbsoluteURL(PlacelessSetup, TestCase):
def setUp(self):
@@ -79,7 +86,7 @@ class TestAbsoluteURL(PlacelessSetup, TestCase):
# don't define a more specific adapter
zope.component.provideAdapter(LocationProxy, (Interface,),
ILocation)
-
+
def tearDown(self):
PlacelessSetup.tearDown(self)
@@ -121,7 +128,6 @@ class TestAbsoluteURL(PlacelessSetup, TestCase):
{'name': 'c', 'url': 'http://127.0.0.1/a/b/c'},
))
-
def testParentButNoLocation(self):
request = TestRequest()
@@ -141,7 +147,7 @@ class TestAbsoluteURL(PlacelessSetup, TestCase):
self.assertEqual(str(view), 'http://127.0.0.1/a/b/c')
self.assertEqual(absoluteURL(content3, request),
'http://127.0.0.1/a/b/c')
-
+
def testAdaptedContext(self):
request = TestRequest()
@@ -164,17 +170,15 @@ class TestAbsoluteURL(PlacelessSetup, TestCase):
# adapter
request = TestRequest()
-
content = FooContent()
content.__parent__ = Root()
content.__name__ = 'foo'
-
+
view = getMultiAdapter((content, request), name='absolute_url')
self.assertEqual(str(view), 'http://127.0.0.1/foo')
self.assertEqual(absoluteURL(content, request),
'http://127.0.0.1/foo')
-
def testBasicContext_unicode(self):
#Tests so that AbsoluteURL handle unicode names as well
request = TestRequest()
@@ -289,7 +293,6 @@ class TestAbsoluteURL(PlacelessSetup, TestCase):
self.assertEqual(str(view), 'http://127.0.0.1')
self.assertEqual(absoluteURL(None, request), 'http://127.0.0.1')
-
def testVirtualHostingWithoutContextInformation(self):
request = TestRequest()
request._vh_root = contained(TrivialContent(), Root(), name='a')
@@ -301,5 +304,5 @@ class TestAbsoluteURL(PlacelessSetup, TestCase):
def test_suite():
return makeSuite(TestAbsoluteURL)
-if __name__=='__main__':
+if __name__ == '__main__':
main(defaultTest='test_suite')
diff --git a/src/zope/traversing/namespace.py b/src/zope/traversing/namespace.py
index fd27578..5bee798 100644
--- a/src/zope/traversing/namespace.py
+++ b/src/zope/traversing/namespace.py
@@ -21,8 +21,8 @@ import zope.component
import zope.interface
from zope.i18n.interfaces import IModifiableUserPreferredLanguages
from zope.component.interfaces import ComponentLookupError
-from zope.interface import providedBy, directlyProvides, directlyProvidedBy
-from zope.location.interfaces import IRoot, LocationError
+from zope.interface import providedBy, directlyProvides
+from zope.location.interfaces import LocationError
from zope.publisher.interfaces.browser import IBrowserSkinType
from zope.publisher.skinnable import applySkin
from zope.security.proxy import removeSecurityProxy
@@ -34,9 +34,11 @@ from zope.traversing.interfaces import ITraversable
class UnexpectedParameters(LocationError):
"Unexpected namespace parameters were provided."
+
class ExcessiveDepth(LocationError):
"Too many levels of containment. We don't believe them."
+
def namespaceLookup(ns, name, object, request=None):
"""Lookup a value from a namespace
@@ -114,6 +116,7 @@ def namespaceLookup(ns, name, object, request=None):
namespace_pattern = re.compile('[+][+]([a-zA-Z0-9_]+)[+][+]')
+
def nsParse(name):
"""Parse a namespace-qualified name into a namespace name and a
name. Returns the namespace name and a name.
@@ -154,12 +157,14 @@ def nsParse(name):
return ns, name
+
def getResource(site, name, request):
resource = queryResource(site, name, request)
if resource is None:
raise LocationError(site, name)
return resource
+
def queryResource(site, name, request, default=None):
resource = zope.component.queryAdapter(request, name=name)
if resource is None:
@@ -174,6 +179,7 @@ def queryResource(site, name, request, default=None):
return resource
+
# ---- namespace processors below ----
class SimpleHandler(object):
@@ -194,6 +200,7 @@ class SimpleHandler(object):
"""
self.context = context
+
class acquire(SimpleHandler):
"""Traversal adapter for the acquire namespace
"""
@@ -262,6 +269,7 @@ class acquire(SimpleHandler):
raise ExcessiveDepth(self.context, name)
+
class attr(SimpleHandler):
def traverse(self, name, ignored):
@@ -277,6 +285,7 @@ class attr(SimpleHandler):
"""
return getattr(self.context, name)
+
class item(SimpleHandler):
def traverse(self, name, ignored):
@@ -291,6 +300,7 @@ class item(SimpleHandler):
"""
return self.context[name]
+
class etc(SimpleHandler):
def traverse(self, name, ignored):
@@ -330,6 +340,7 @@ class view(object):
return view
+
class resource(view):
def traverse(self, name, ignored):
@@ -337,6 +348,7 @@ class resource(view):
# resource, which is needed to generate the absolute URL.
return getResource(self.context, name, self.request)
+
class lang(view):
def traverse(self, name, ignored):
@@ -345,6 +357,7 @@ class lang(view):
languages.setPreferredLanguages([name])
return self.context
+
class skin(view):
def traverse(self, name, ignored):
@@ -356,6 +369,7 @@ class skin(view):
applySkin(self.request, skin)
return self.context
+
class vh(view):
def traverse(self, name, ignored):
@@ -511,7 +525,7 @@ class debug(view):
# if we want to enable tracebacks when also trying to
# debug a different skin?
skin = zope.component.getUtility(IBrowserSkinType, 'Debug')
- directlyProvides(request, providedBy(request)+skin)
+ directlyProvides(request, providedBy(request) + skin)
else:
raise ValueError("Unknown debug flag: %s" % flag)
return self.context
diff --git a/src/zope/traversing/publicationtraverse.py b/src/zope/traversing/publicationtraverse.py
index 57aadb0..a828c67 100644
--- a/src/zope/traversing/publicationtraverse.py
+++ b/src/zope/traversing/publicationtraverse.py
@@ -25,6 +25,7 @@ from zope.traversing.interfaces import TraversalError
from zope.publisher.interfaces import IPublishTraverse
from zope.publisher.interfaces.browser import IBrowserPublisher
+
class PublicationTraverser(object):
"""Traversal used for publication.
@@ -45,7 +46,7 @@ class PublicationTraverser(object):
return ProxyFactory(ob)
def traverseName(self, request, ob, name):
- nm = name # the name to look up the object with
+ nm = name # the name to look up the object with
if name and name[:1] in '@+':
# Process URI segment parameters.
@@ -92,9 +93,9 @@ class PublicationTraverser(object):
# Remove double dots
while '..' in path:
l = path.index('..')
- if l < 0 or l+2 > len(path):
+ if l < 0 or l + 2 > len(path):
break
- del path[l:l+2]
+ del path[l:l + 2]
pop = path.pop
@@ -119,9 +120,11 @@ class PublicationTraverser(object):
ob = self.traversePath(request, ob, path)
+
# alternate spelling
PublicationTraverse = PublicationTraverser
+
class PublicationTraverserWithoutProxy(PublicationTraverse):
def proxy(self, ob):
diff --git a/src/zope/traversing/testing.py b/src/zope/traversing/testing.py
index 948a282..2bbb4d8 100644
--- a/src/zope/traversing/testing.py
+++ b/src/zope/traversing/testing.py
@@ -28,6 +28,7 @@ from zope.traversing.browser import SiteAbsoluteURL, AbsoluteURL
from zope.traversing.browser.interfaces import IAbsoluteURL
from zope.traversing.namespace import etc
+
def setUp():
zope.component.provideAdapter(Traverser, (None,), ITraverser)
zope.component.provideAdapter(DefaultTraversable, (None,), ITraversable)
@@ -50,6 +51,7 @@ def browserView(for_, name, factory, providing=zope.interface.Interface):
zope.component.provideAdapter(factory, (for_, IDefaultBrowserLayer),
providing, name=name)
+
def browserResource(name, factory, providing=zope.interface.Interface):
zope.component.provideAdapter(factory, (IDefaultBrowserLayer,),
providing, name=name)