~ubuntu-branches/ubuntu/precise/z3c.pt/precise

« back to all changes in this revision

Viewing changes to src/z3c/pt/namespaces.py

  • Committer: Package Import Robot
  • Author(s): Gediminas Paulauskas
  • Date: 2012-02-03 16:03:32 UTC
  • Revision ID: package-import@ubuntu.com-20120203160332-y8iyshk0u8rn4w37
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import zope.component
 
2
from zope.traversing.interfaces import IPathAdapter
 
3
 
 
4
class AdapterNamespaces(object):
 
5
    """Simulate tales function namespaces with adapter lookup.
 
6
 
 
7
    When we are asked for a namespace, we return an object that
 
8
    actually computes an adapter when called:
 
9
 
 
10
    To demonstrate this, we need to register an adapter:
 
11
 
 
12
      >>> def adapter1(ob):
 
13
      ...     return 1
 
14
      >>> zope.component.getGlobalSiteManager().registerAdapter(
 
15
      ...     adapter1, [zope.interface.Interface], IPathAdapter, 'a1')
 
16
 
 
17
    Now, with this adapter in place, we can try out the namespaces:
 
18
 
 
19
      >>> ob = object()
 
20
      >>> namespaces = AdapterNamespaces()
 
21
      >>> namespace = namespaces['a1']
 
22
      >>> namespace(ob)
 
23
      1
 
24
      >>> namespace = namespaces['a2']
 
25
      >>> namespace(ob)
 
26
      Traceback (most recent call last):
 
27
      ...
 
28
      KeyError: 'a2'
 
29
    """
 
30
 
 
31
    def __init__(self):
 
32
        self.namespaces = {}
 
33
 
 
34
    def __getitem__(self, name):
 
35
        namespace = self.namespaces.get(name)
 
36
        if namespace is None:
 
37
            def namespace(object):
 
38
                try:
 
39
                    return zope.component.getAdapter(object, IPathAdapter, name)
 
40
                except zope.component.ComponentLookupError:
 
41
                    raise KeyError(name)
 
42
 
 
43
            self.namespaces[name] = namespace
 
44
        return namespace
 
45
 
 
46
 
 
47
    def registerFunctionNamespace(self, namespacename, namespacecallable):
 
48
        """Register a function namespace
 
49
 
 
50
        namespace - a string containing the name of the namespace to
 
51
                    be registered
 
52
 
 
53
        namespacecallable - a callable object which takes the following
 
54
                            parameter:
 
55
 
 
56
                            context - the object on which the functions
 
57
                                      provided by this namespace will
 
58
                                      be called
 
59
 
 
60
                            This callable should return an object which
 
61
                            can be traversed to get the functions provided
 
62
                            by the this namespace.
 
63
 
 
64
        example:
 
65
 
 
66
           class stringFuncs(object):
 
67
 
 
68
              def __init__(self,context):
 
69
                 self.context = str(context)
 
70
 
 
71
              def upper(self):
 
72
                 return self.context.upper()
 
73
 
 
74
              def lower(self):
 
75
                 return self.context.lower()
 
76
 
 
77
            engine.registerFunctionNamespace('string',stringFuncs)
 
78
        """
 
79
        self.namespaces[namespacename] = namespacecallable
 
80
 
 
81
 
 
82
    def getFunctionNamespace(self, namespacename):
 
83
        """ Returns the function namespace """
 
84
        return self.namespaces[namespacename]
 
85
 
 
86
try:
 
87
    # If zope.app.pagetemplates is available, use the adapter
 
88
    # registered with the main zope.app.pagetemplates engine so that
 
89
    # we don't need to re-register them.
 
90
    from zope.app.pagetemplates.engine import Engine
 
91
    function_namespaces = Engine.namespaces
 
92
except (ImportError, AttributeError):
 
93
    function_namespaces = AdapterNamespaces()
 
94