~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/reality/container.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2004-06-21 22:01:11 UTC
  • mto: (2.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040621220111-vkf909euqnyrp3nr
Tags: upstream-1.3.0
ImportĀ upstreamĀ versionĀ 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
# Twisted, the Framework of Your Internet
3
 
# Copyright (C) 2001 Matthew W. Lefkowitz
4
 
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.
8
 
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.
13
 
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
17
 
 
18
 
import thing
19
 
import error
20
 
 
21
 
class Container(thing.Thing):
22
 
    """Container
23
 
 
24
 
    A convenience class, setting a few defaults for objects intended
25
 
    to be containers.
26
 
    """
27
 
 
28
 
    hollow = 1
29
 
 
30
 
    def verb_put(self, sentence):
31
 
        """put foo (in|on) container
32
 
        Place one object in another.
33
 
        """
34
 
        obj = sentence.directObject()
35
 
        sub = sentence.subject
36
 
        # no fair making things disappear!
37
 
        if obj == self:
38
 
            Failure("Some would say it's already there. Anyway, you cant do that.")
39
 
        if self.surface:
40
 
            prep = 'on'
41
 
        else:
42
 
            prep = 'in'
43
 
        if obj.place is not sub:
44
 
            Failure("You're not holding that.")
45
 
        obj.move(destination = self, actor = sub)
46
 
        sub.broadcastToPair(self,
47
 
            to_subject = ("You put ",obj," ",prep," ",self,"."),
48
 
            to_target = (), # I'm an inanimate object.  What do I care?
49
 
            to_other = (sub,' puts ',obj,' ',prep,' ',self,'.') )
50
 
 
51
 
    verb_put_in = verb_put
52
 
    verb_put_on = verb_put
53
 
 
54
 
class _Contents(Container):
55
 
    "Bookkeeping class for the contents of boxes."
56
 
    surface = 0
57
 
    def containedPhrase(self, observer, other):
58
 
        "calls back up one level."
59
 
        return self.location.containedPhrase(observer, other)
60
 
 
61
 
class Box(thing.Thing):
62
 
    surface = 0
63
 
    isOpen = 0
64
 
    closedDesc = ''
65
 
    openDesc = ''
66
 
    contained_preposition = 'in'
67
 
 
68
 
    def setup(self):
69
 
        self.description = {'open/close': self.closedDesc}
70
 
        self.contents = _Contents("$"+self.name + "'s contents")
71
 
        self.contents.location = self
72
 
        self.contents.component = 1
73
 
 
74
 
    def action_Open(self, actor):
75
 
        self.isOpen = 1
76
 
        self.contents.surface = 1
77
 
        self.description = {'open/close': self.openDesc}
78
 
 
79
 
    def action_Close(self, actor):
80
 
        self.isOpen = 0
81
 
        self.contents.surface = 0
82
 
        self.description = {'open/close': self.closedDesc}
83
 
 
84
 
    def verb_open(self, sentence):
85
 
        sentence.shouldOnlyHave('')
86
 
        if self.isOpen:
87
 
            error.Failure("It's already open.")
88
 
        else:
89
 
            self.action_Open(sentence.subject)
90
 
 
91
 
    def verb_close(self, sentence):
92
 
        sentence.shouldOnlyHave('')
93
 
        if not self.isOpen:
94
 
            error.Failure("It's already closed.")
95
 
        else:
96
 
            self.action_Close(sentence.subject)
97
 
 
98
 
    def verb_put_in(self, sentence):
99
 
        sentence.shouldOnlyHave('','in')
100
 
        if self.isOpen:
101
 
            sentence.directObject().move(self.contents, sentence.subject)
102
 
        else:
103
 
            error.Failure("It's closed.")
104
 
 
105
 
    def destroy(self):
106
 
        self.contents.destroy()
107
 
        thing.Thing.destroy(self)