6
from twisted.python import defer
9
from perspective import Perspective
12
"""I am a service that internet applications interact with.
14
I represent a set of abstractions which users may interact with over a
17
(See Also: twisted.spread.pb.Service)
20
perspectiveClass = Perspective
26
def __init__(self, serviceName, application=None):
27
"""Create me, attached to the given application.
29
Arguments: application, a twisted.internet.app.Application instance.
31
self.serviceName = serviceName
32
self.perspectives = {}
33
self.setApplication(application)
35
def cachePerspective(self, perspective):
36
"""Cache a perspective loaded from an external data source.
38
Perspectives that were 'loaded' from memory will not be uncached.
40
if self.perspectives.has_key(perspective.perspectiveName):
42
self.perspectives[perspective.perspectiveName] = perspective
43
perspective._service_cached = 1
45
def uncachePerspective(self, perspective):
46
"""Uncache a perspective loaded from an external data source.
48
Perspectives that were 'loaded' from memory will not be uncached.
50
if self.perspectives.has_key(perspective.perspectiveName):
51
if perspective._service_cached:
52
del self.perspectives[perspective.perspectiveName]
54
def setApplication(self, application):
55
if self.application is not application:
56
assert not self.application, "Application already set!"
58
self.application = application
59
application.addService(self)
61
def createPerspective(self, name):
62
"""Create a perspective from self.perspectiveClass and add it to this service.
64
p = self.perspectiveClass(name)
65
self.perspectives[name] = p
69
def addPerspective(self, perspective):
70
"""Add a perspective to this Service.
72
perspective.setService(self)
73
self.perspectives[perspective.getPerspectiveName()] = perspective
75
def getPerspectiveNamed(self, name):
76
"""Return a perspective that represents a user for this service. (DEPRECATED)
78
Raises a KeyError if no such user exists. Override this method to
79
provide dynamic instantiation of perspectives.
81
return self.perspectives[name]
83
def loadPerspective(self, name):
84
"""Load a perspective from an external data-source.
86
If no such data-source exists, return None. Implement this if you want
87
to load your perspectives from somewhere else (e.g. LDAP or a
88
database). It is not recommended to call this directly, since
89
getPerspectiveRequest provides management of caching perspectives.
91
return defer.fail("No such perspective %s" % name)
93
def getPerspectiveRequest(self, name):
94
"""Return a Deferred which is a request for a perspective on this service.
97
p = self.getPerspectiveNamed(name)
99
return self.loadPerspective(name)
101
return defer.succeed(p)
103
def getServiceName(self):
104
"""The name of this service.
106
return self.serviceName or self.getServiceType()
108
def getServiceType(self):
109
"""Get a string describing the type of this service.
111
return self.serviceType or str(self.__class__)