Skip to content

Commit

Permalink
remove the deprecated IContextURL
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Nov 19, 2016
1 parent 44563d9 commit b1e4b7f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 425 deletions.
14 changes: 7 additions & 7 deletions docs/narr/vhosting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ Hosting an Application Under a URL Prefix
``http://example.com/``).

If you use a "pure Python" environment, this functionality can be provided by
Paste's `urlmap <http://pythonpaste.org/modules/urlmap.html>`_ "composite" WSGI
application. Alternatively, you can use :term:`mod_wsgi` to serve your
`rutter <http://rutter.readthedocs.io/en/latest/>`_, forming a "composite"
WSGI application. Alternatively, you can use :term:`mod_wsgi` to serve your
application, which handles this virtual hosting translation for you "under the
hood".

If you use the ``urlmap`` composite application "in front" of a :app:`Pyramid`
If you use the ``rutter`` composite application "in front" of a :app:`Pyramid`
application or if you use :term:`mod_wsgi` to serve up a :app:`Pyramid`
application, nothing special needs to be done within the application for URLs
to be generated that contain a prefix. :mod:`paste.urlmap` and :term:`mod_wsgi`
to be generated that contain a prefix. Rutter and :term:`mod_wsgi`
manipulate the :term:`WSGI` environment in such a way that the ``PATH_INFO``
and ``SCRIPT_NAME`` variables are correct for some given prefix.

Here's an example of a PasteDeploy configuration snippet that includes a
``urlmap`` composite.
``rutter`` composite.

.. code-block:: ini
:linenos:
Expand All @@ -48,13 +48,13 @@ Here's an example of a PasteDeploy configuration snippet that includes a
use = egg:mypyramidapp
[composite:main]
use = egg:Paste#urlmap
use = egg:rutter#urlmap
/pyramidapp = mypyramidapp
This "roots" the :app:`Pyramid` application at the prefix ``/pyramidapp`` and
serves up the composite as the "main" application in the file.

.. note:: If you're using an Apache server to proxy to a Paste ``urlmap``
.. note:: If you're using an Apache server to proxy to a ``urlmap``
composite, you may have to use the `ProxyPreserveHost
<http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypreservehost>`_
directive to pass the original ``HTTP_HOST`` header along to the
Expand Down
52 changes: 0 additions & 52 deletions pyramid/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,58 +799,6 @@ class IResourceURL(Interface):
'The physical url path of the resource as a tuple. (New in 1.5)'
)

class IContextURL(IResourceURL):
"""
.. deprecated:: 1.3
An adapter which deals with URLs related to a context. Use
:class:`pyramid.interfaces.IResourceURL` instead.
"""
# this class subclasses IResourceURL because request.resource_url looks
# for IResourceURL via queryAdapter. queryAdapter will find a deprecated
# IContextURL registration if no registration for IResourceURL exists.
# In reality, however, IContextURL objects were never required to have
# the virtual_path or physical_path attributes spelled in IResourceURL.
# The inheritance relationship is purely to benefit adapter lookup,
# not to imply an inheritance relationship of interface attributes
# and methods.
#
# Mechanics:
#
# class Fudge(object):
# def __init__(self, one, two):
# print(one, two)
# class Another(object):
# def __init__(self, one, two):
# print(one, two)
# ob = object()
# r.registerAdapter(Fudge, (Interface, Interface), IContextURL)
# print(r.queryMultiAdapter((ob, ob), IResourceURL))
# r.registerAdapter(Another, (Interface, Interface), IResourceURL)
# print(r.queryMultiAdapter((ob, ob), IResourceURL))
#
# prints
#
# <object object at 0x7fa678f3e2a0> <object object at 0x7fa678f3e2a0>
# <__main__.Fudge object at 0x1cda890>
# <object object at 0x7fa678f3e2a0> <object object at 0x7fa678f3e2a0>
# <__main__.Another object at 0x1cda850>

def virtual_root():
""" Return the virtual root related to a request and the
current context"""

def __call__():
""" Return a URL that points to the context. """

deprecated(
'IContextURL',
'As of Pyramid 1.3 the, "pyramid.interfaces.IContextURL" interface is '
'scheduled to be removed. Use the '
'"pyramid.config.Configurator.add_resource_url_adapter" method to register '
'a class that implements "pyramid.interfaces.IResourceURL" instead. '
'See the "What\'s new In Pyramid 1.3" document for more details.'
)

class IPEP302Loader(Interface):
""" See http://www.python.org/dev/peps/pep-0302/#id30.
"""
Expand Down
223 changes: 35 additions & 188 deletions pyramid/tests/test_traversal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
import warnings

from pyramid.testing import cleanUp

Expand All @@ -11,11 +10,6 @@
PY2,
)

with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always')
from pyramid.interfaces import IContextURL
assert(len(w) == 1)

class TraversalPathTests(unittest.TestCase):
def _callFUT(self, path):
from pyramid.traversal import traversal_path
Expand Down Expand Up @@ -870,182 +864,12 @@ def _getTargetClass(self):
from pyramid.traversal import ResourceURL
return ResourceURL

def _registerTraverser(self, traverser):
from pyramid.threadlocal import get_current_registry
reg = get_current_registry()
from pyramid.interfaces import ITraverser
from zope.interface import Interface
reg.registerAdapter(traverser, (Interface,), ITraverser)

def test_class_conforms_to_IContextURL(self):
# bw compat
from zope.interface.verify import verifyClass
verifyClass(IContextURL, self._getTargetClass())

def test_instance_conforms_to_IContextURL(self):
from zope.interface.verify import verifyObject
context = DummyContext()
request = DummyRequest()
verifyObject(IContextURL, self._makeOne(context, request))

def test_instance_conforms_to_IResourceURL(self):
from pyramid.interfaces import IResourceURL
from zope.interface.verify import verifyObject
context = DummyContext()
request = DummyRequest()
verifyObject(IResourceURL, self._makeOne(context, request))

def test_call_withlineage(self):
baz = DummyContext()
bar = DummyContext(baz)
foo = DummyContext(bar)
root = DummyContext(foo)
root.__parent__ = None
root.__name__ = None
foo.__parent__ = root
foo.__name__ = 'foo '
bar.__parent__ = foo
bar.__name__ = 'bar'
baz.__parent__ = bar
baz.__name__ = 'baz'
request = DummyRequest()
context_url = self._makeOne(baz, request)
result = context_url()
self.assertEqual(result, 'http://example.com:5432/foo%20/bar/baz/')

def test_call_nolineage(self):
context = DummyContext()
context.__name__ = ''
context.__parent__ = None
request = DummyRequest()
context_url = self._makeOne(context, request)
result = context_url()
self.assertEqual(result, 'http://example.com:5432/')

def test_call_unicode_mixed_with_bytes_in_resource_names(self):
root = DummyContext()
root.__parent__ = None
root.__name__ = None
one = DummyContext()
one.__parent__ = root
one.__name__ = text_(b'La Pe\xc3\xb1a', 'utf-8')
two = DummyContext()
two.__parent__ = one
two.__name__ = b'La Pe\xc3\xb1a'
request = DummyRequest()
context_url = self._makeOne(two, request)
result = context_url()
self.assertEqual(
result,
'http://example.com:5432/La%20Pe%C3%B1a/La%20Pe%C3%B1a/')

def test_call_with_virtual_root_path(self):
from pyramid.interfaces import VH_ROOT_KEY
root = DummyContext()
root.__parent__ = None
root.__name__ = None
one = DummyContext()
one.__parent__ = root
one.__name__ = 'one'
two = DummyContext()
two.__parent__ = one
two.__name__ = 'two'
request = DummyRequest({VH_ROOT_KEY:'/one'})
context_url = self._makeOne(two, request)
result = context_url()
self.assertEqual(result, 'http://example.com:5432/two/')

request = DummyRequest({VH_ROOT_KEY:'/one/two'})
context_url = self._makeOne(two, request)
result = context_url()
self.assertEqual(result, 'http://example.com:5432/')

def test_call_with_virtual_root_path_physical_not_startwith_vroot(self):
from pyramid.interfaces import VH_ROOT_KEY
root = DummyContext()
root.__parent__ = None
root.__name__ = None
one = DummyContext()
one.__parent__ = root
one.__name__ = 'one'
two = DummyContext()
two.__parent__ = one
two.__name__ = 'two'
request = DummyRequest({VH_ROOT_KEY:'/wrong'})
context_url = self._makeOne(two, request)
result = context_url()
self.assertEqual(result, 'http://example.com:5432/one/two/')

def test_call_empty_names_not_ignored(self):
bar = DummyContext()
empty = DummyContext(bar)
root = DummyContext(empty)
root.__parent__ = None
root.__name__ = None
empty.__parent__ = root
empty.__name__ = ''
bar.__parent__ = empty
bar.__name__ = 'bar'
request = DummyRequest()
context_url = self._makeOne(bar, request)
result = context_url()
self.assertEqual(result, 'http://example.com:5432//bar/')

def test_call_local_url_returns_None(self):
resource = DummyContext()
def resource_url(request, info):
self.assertEqual(info['virtual_path'], '/')
self.assertEqual(info['physical_path'], '/')
return None
resource.__resource_url__ = resource_url
request = DummyRequest()
context_url = self._makeOne(resource, request)
result = context_url()
self.assertEqual(result, 'http://example.com:5432/')

def test_call_local_url_returns_url(self):
resource = DummyContext()
def resource_url(request, info):
self.assertEqual(info['virtual_path'], '/')
self.assertEqual(info['physical_path'], '/')
return 'abc'
resource.__resource_url__ = resource_url
request = DummyRequest()
context_url = self._makeOne(resource, request)
result = context_url()
self.assertEqual(result, 'abc')

def test_virtual_root_no_virtual_root_path(self):
root = DummyContext()
root.__name__ = None
root.__parent__ = None
one = DummyContext()
one.__name__ = 'one'
one.__parent__ = root
request = DummyRequest()
context_url = self._makeOne(one, request)
self.assertEqual(context_url.virtual_root(), root)

def test_virtual_root_no_virtual_root_path_with_root_on_request(self):
context = DummyContext()
context.__parent__ = None
request = DummyRequest()
request.root = DummyContext()
context_url = self._makeOne(context, request)
self.assertEqual(context_url.virtual_root(), request.root)

def test_virtual_root_with_virtual_root_path(self):
from pyramid.interfaces import VH_ROOT_KEY
context = DummyContext()
context.__parent__ = None
traversed_to = DummyContext()
environ = {VH_ROOT_KEY:'/one'}
request = DummyRequest(environ)
traverser = make_traverser({'context':traversed_to, 'view_name':''})
self._registerTraverser(traverser)
context_url = self._makeOne(context, request)
self.assertEqual(context_url.virtual_root(), traversed_to)
self.assertEqual(context.request.environ['PATH_INFO'], '/one')

def test_IResourceURL_attributes_with_vroot(self):
from pyramid.interfaces import VH_ROOT_KEY
Expand Down Expand Up @@ -1114,14 +938,44 @@ def _callFUT(self, resource, request):
from pyramid.traversal import virtual_root
return virtual_root(resource, request)

def test_registered(self):
def _registerTraverser(self, traverser):
from pyramid.threadlocal import get_current_registry
reg = get_current_registry()
from pyramid.interfaces import ITraverser
from zope.interface import Interface
request = _makeRequest()
request.registry.registerAdapter(DummyContextURL, (Interface,Interface),
IContextURL)
reg.registerAdapter(traverser, (Interface,), ITraverser)

def test_virtual_root_no_virtual_root_path(self):
root = DummyContext()
root.__name__ = None
root.__parent__ = None
one = DummyContext()
one.__name__ = 'one'
one.__parent__ = root
request = DummyRequest()
result = self._callFUT(one, request)
self.assertEqual(result, root)

def test_virtual_root_no_virtual_root_path_with_root_on_request(self):
context = DummyContext()
context.__parent__ = None
request = DummyRequest()
request.root = DummyContext()
result = self._callFUT(context, request)
self.assertEqual(result, '123')
self.assertEqual(result, request.root)

def test_virtual_root_with_virtual_root_path(self):
from pyramid.interfaces import VH_ROOT_KEY
context = DummyContext()
context.__parent__ = None
traversed_to = DummyContext()
environ = {VH_ROOT_KEY:'/one'}
request = DummyRequest(environ)
traverser = make_traverser({'context':traversed_to, 'view_name':''})
self._registerTraverser(traverser)
result = self._callFUT(context, request)
self.assertEqual(result, traversed_to)
self.assertEqual(context.request.environ['PATH_INFO'], '/one')

def test_default(self):
context = DummyContext()
Expand Down Expand Up @@ -1347,13 +1201,6 @@ def _set_path_info(self, v):
path_info = property(_get_path_info, _set_path_info)


class DummyContextURL:
def __init__(self, context, request):
pass

def virtual_root(self):
return '123'

def _makeRequest(environ=None):
from pyramid.registry import Registry
request = DummyRequest()
Expand Down
Loading

0 comments on commit b1e4b7f

Please sign in to comment.