~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/spread/refpath.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

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