2
# Twisted, the Framework of Your Internet
3
# Copyright (C) 2001 Matthew W. Lefkowitz
5
# This library is free software; you can redistribute it and/or
6
# modify it under the terms of version 2.1 of the GNU Lesser General Public
7
# License as published by the Free Software Foundation.
9
# This library is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public
15
# License along with this library; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
from twisted.python import defer
26
from perspective import Perspective
33
"""I am a service that internet applications interact with.
35
I represent a set of abstractions which users may interact with over a
38
(See Also: twisted.spread.pb.Service)
41
perspectiveClass = Perspective
47
def __init__(self, serviceName, application=None):
48
"""Create me, attached to the given application.
50
Arguments: application, a twisted.internet.app.Application instance.
52
if not isinstance(serviceName, types.StringType):
54
self.serviceName = serviceName
55
self.perspectives = {}
56
self.setApplication(application)
58
def cachePerspective(self, perspective):
59
"""Cache a perspective loaded from an external data source.
61
Perspectives that were 'loaded' from memory will not be uncached.
63
if self.perspectives.has_key(perspective.perspectiveName):
65
self.perspectives[perspective.perspectiveName] = perspective
66
perspective._service_cached = 1
68
def uncachePerspective(self, perspective):
69
"""Uncache a perspective loaded from an external data source.
71
Perspectives that were 'loaded' from memory will not be uncached.
73
if self.perspectives.has_key(perspective.perspectiveName):
74
if perspective._service_cached:
75
del self.perspectives[perspective.perspectiveName]
77
def setApplication(self, application):
78
from twisted.internet import app
79
if application and not isinstance(application, app.Application):
81
if self.application and self.application is not application:
82
raise RuntimeError, "Application already set!"
84
self.application = application
85
application.addService(self)
87
def createPerspective(self, name):
88
"""Create a perspective from self.perspectiveClass and add it to this service.
90
p = self.perspectiveClass(name)
91
self.perspectives[name] = p
95
def addPerspective(self, perspective):
96
"""Add a perspective to this Service.
98
if not isinstance(perspective, Perspective):
100
perspective.setService(self)
101
self.perspectives[perspective.getPerspectiveName()] = perspective
103
def getPerspectiveNamed(self, name):
104
"""Return a perspective that represents a user for this service. (DEPRECATED)
106
Raises a KeyError if no such user exists. Override this method to
107
provide dynamic instantiation of perspectives.
109
return self.perspectives[name]
111
def loadPerspective(self, name):
112
"""Load a perspective from an external data-source.
114
If no such data-source exists, return None. Implement this if you want
115
to load your perspectives from somewhere else (e.g. LDAP or a
116
database). It is not recommended to call this directly, since
117
getPerspectiveRequest provides management of caching perspectives.
119
return defer.fail("No such perspective %s" % name)
121
def getPerspectiveRequest(self, name):
122
"""Return a Deferred which is a request for a perspective on this service.
125
p = self.getPerspectiveNamed(name)
127
return self.loadPerspective(name)
129
return defer.succeed(p)
131
def getServiceName(self):
132
"""The name of this service.
134
return self.serviceName or self.getServiceType()
136
def getServiceType(self):
137
"""Get a string describing the type of this service.
139
return self.serviceType or str(self.__class__)