~landscape/zope3/newer-from-ztk

« back to all changes in this revision

Viewing changes to src/twisted/spread/refpath.py

  • Committer: Thomas Hervé
  • Date: 2009-07-08 13:52:04 UTC
  • Revision ID: thomas@canonical.com-20090708135204-df5eesrthifpylf8
Remove twisted copy

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
 
# See LICENSE for details.
3
 
 
4
 
 
5
 
__version__ = "$Revision: 1.17 $"[11:-2]
6
 
 
7
 
"""
8
 
 
9
 
Path-based references for PB, and other reference-based protocols.
10
 
 
11
 
Maintainer: U{Glyph Lefkowitz<mailto:glyph@twistedmatrix.com>}
12
 
 
13
 
Stability: semi-stable
14
 
 
15
 
Future Plans: None at this point besides a final overview and finalization
16
 
pass.
17
 
 
18
 
"""
19
 
 
20
 
 
21
 
from twisted.python import log
22
 
 
23
 
from flavors import Referenceable, Viewable
24
 
from copy import copy
25
 
import os
26
 
 
27
 
 
28
 
 
29
 
### "Server"-side objects
30
 
 
31
 
class PathReferenceContext:
32
 
    def __init__(self, path, root):
33
 
        self.metadata = {}
34
 
        self.path = path
35
 
        self.root = root
36
 
 
37
 
    def __setitem__(self, key, item):
38
 
        self.metadata[key] = item
39
 
 
40
 
    def __getitem__(self, key):
41
 
        return self.metadata[key]
42
 
 
43
 
    def getObject(self):
44
 
        o = self.root
45
 
        for p in self.path:
46
 
            o = o.getChild(p, self)
47
 
        return o
48
 
 
49
 
class PathReference:
50
 
    def __init__(self):
51
 
        self.children = {}
52
 
    def getChild(self, child, ctx):
53
 
        return self.children[child]
54
 
 
55
 
class PathReferenceDirectory(Referenceable):
56
 
    def __init__(self, root, prefix="remote"):
57
 
        self.root = root
58
 
        self.prefix = prefix
59
 
    def remote_callPath(self, path, name, *args, **kw):
60
 
        ctx = PathReferenceContext(path, self)
61
 
        obj = ctx.getObject()
62
 
        return apply(getattr(obj, "%s_%s" % (self.prefix, name)), args, kw)
63
 
 
64
 
class PathReferenceContextDirectory(Referenceable):
65
 
    def __init__(self, root, prefix="remote"):
66
 
        self.root = root
67
 
        self.prefix = prefix
68
 
    def remote_callPath(self, path, name, *args, **kw):
69
 
        ctx = PathReferenceContext(path, self)
70
 
        obj = ctx.getObject()
71
 
        return apply(getattr(obj, "%s_%s" % (self.prefix, name)),
72
 
                     (ctx,)+args, kw)
73
 
 
74
 
class PathViewDirectory(Viewable):
75
 
    def __init__(self, root, prefix="view"):
76
 
        self.root = root
77
 
        self.prefix = prefix
78
 
    def view_callPath(self, perspective, path, name, *args, **kw):
79
 
        ctx = PathReferenceContext(path, self)
80
 
        obj = ctx.getObject()
81
 
        return apply(getattr(obj, "%s_%s" % (self.prefix, name)),
82
 
                     (perspective,)+args, kw)
83
 
 
84
 
class PathViewContextDirectory(Viewable):
85
 
    def __init__(self, root, prefix="view"):
86
 
        self.root = root
87
 
        self.prefix = prefix
88
 
    def view_callPath(self, perspective, path, name, *args, **kw):
89
 
        ctx = PathReferenceContext(path, self)
90
 
        obj = ctx.getObject()
91
 
        return apply(getattr(obj, "%s_%s" % (self.prefix, name)),
92
 
                     (perspective,ctx)+args, kw)
93
 
 
94
 
### "Client"-side objects
95
 
 
96
 
class RemotePathReference:
97
 
    def __init__(self, ref, path):
98
 
        self.ref = ref
99
 
        self.path = path
100
 
 
101
 
    def callRemote(self, name, *args, **kw):
102
 
        apply(self.ref.callRemote,
103
 
              ("callPath", self.path, name)+args, kw)