~dustin-spy/twisted/dustin

« back to all changes in this revision

Viewing changes to twisted/cred/service.py

  • Committer: glyph
  • Date: 2002-01-27 23:10:25 UTC
  • Revision ID: vcs-imports@canonical.com-20020127231025-6180b0f76c6aba84
Moved twisted.internet.passport into twisted.cred.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Twisted Cred Service
 
3
"""
 
4
 
 
5
# Twisted Imports
 
6
from twisted.python import defer
 
7
 
 
8
# Sibling Imports
 
9
from perspective import Perspective
 
10
 
 
11
class Service:
 
12
    """I am a service that internet applications interact with.
 
13
 
 
14
    I represent a set of abstractions which users may interact with over a
 
15
    specified protocol.
 
16
 
 
17
    (See Also: twisted.spread.pb.Service)
 
18
    """
 
19
    # ugh, load order
 
20
    perspectiveClass = Perspective
 
21
 
 
22
    application = None
 
23
    serviceType = None
 
24
    serviceName = None
 
25
 
 
26
    def __init__(self, serviceName, application=None):
 
27
        """Create me, attached to the given application.
 
28
 
 
29
        Arguments: application, a twisted.internet.app.Application instance.
 
30
        """
 
31
        self.serviceName = serviceName
 
32
        self.perspectives = {}
 
33
        self.setApplication(application)
 
34
 
 
35
    def cachePerspective(self, perspective):
 
36
        """Cache a perspective loaded from an external data source.
 
37
 
 
38
        Perspectives that were 'loaded' from memory will not be uncached.
 
39
        """
 
40
        if self.perspectives.has_key(perspective.perspectiveName):
 
41
            return
 
42
        self.perspectives[perspective.perspectiveName] = perspective
 
43
        perspective._service_cached = 1
 
44
 
 
45
    def uncachePerspective(self, perspective):
 
46
        """Uncache a perspective loaded from an external data source.
 
47
 
 
48
        Perspectives that were 'loaded' from memory will not be uncached.
 
49
        """
 
50
        if self.perspectives.has_key(perspective.perspectiveName):
 
51
            if perspective._service_cached:
 
52
                del self.perspectives[perspective.perspectiveName]
 
53
 
 
54
    def setApplication(self, application):
 
55
        if self.application is not application:
 
56
            assert not self.application, "Application already set!"
 
57
        if application:
 
58
            self.application = application
 
59
            application.addService(self)
 
60
 
 
61
    def createPerspective(self, name):
 
62
        """Create a perspective from self.perspectiveClass and add it to this service.
 
63
        """
 
64
        p = self.perspectiveClass(name)
 
65
        self.perspectives[name] = p
 
66
        p.setService(self)
 
67
        return p
 
68
 
 
69
    def addPerspective(self, perspective):
 
70
        """Add a perspective to this Service.
 
71
        """
 
72
        perspective.setService(self)
 
73
        self.perspectives[perspective.getPerspectiveName()] = perspective
 
74
 
 
75
    def getPerspectiveNamed(self, name):
 
76
        """Return a perspective that represents a user for this service. (DEPRECATED)
 
77
 
 
78
        Raises a KeyError if no such user exists.  Override this method to
 
79
        provide dynamic instantiation of perspectives.
 
80
        """
 
81
        return self.perspectives[name]
 
82
 
 
83
    def loadPerspective(self, name):
 
84
        """Load a perspective from an external data-source.
 
85
 
 
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.
 
90
        """
 
91
        return defer.fail("No such perspective %s" % name)
 
92
 
 
93
    def getPerspectiveRequest(self, name):
 
94
        """Return a Deferred which is a request for a perspective on this service.
 
95
        """
 
96
        try:
 
97
            p = self.getPerspectiveNamed(name)
 
98
        except KeyError:
 
99
            return self.loadPerspective(name)
 
100
        else:
 
101
            return defer.succeed(p)
 
102
 
 
103
    def getServiceName(self):
 
104
        """The name of this service.
 
105
        """
 
106
        return self.serviceName or self.getServiceType()
 
107
 
 
108
    def getServiceType(self):
 
109
        """Get a string describing the type of this service.
 
110
        """
 
111
        return self.serviceType or str(self.__class__)
 
112