~divmod-dev/divmod.org/dangling-1091

« back to all changes in this revision

Viewing changes to Imaginary/imaginary/commands.py

  • Committer: exarkun
  • Date: 2006-05-28 17:02:53 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:6857
Merge greeter-930

Authors: exarkun, radix
Reviewer: idnar
Fixes #930

This adds a mouse NPC which squeaks at people, a better event for
things arriving at places, and better documentation for various
things.

Show diffs side-by-side

added added

removed removed

Lines of Context:
105
105
 
106
106
 
107
107
 
 
108
def runEventTransaction(store, func, *args, **kwargs):
 
109
    """
 
110
    This takes responsibility for setting up the transactional event
 
111
    broadcasting junk, handling action errors, and broadcasting commit or
 
112
    revert events.
 
113
    """
 
114
    broadcaster = TransactionalEventBroadcaster()
 
115
    def runHelper():
 
116
        # Set up event context for the duration of the action
 
117
        # run.  Additionally, handle raised ActionFailures by
 
118
        # adding their events to the revert event list and
 
119
        # re-raising them so they will revert the transaction.
 
120
        try:
 
121
            return context.call(
 
122
                {iimaginary.ITransactionalEventBroadcaster: broadcaster},
 
123
                func, *args, **kwargs)
 
124
        except eimaginary.ActionFailure, e:
 
125
            broadcaster.addRevertEvent(e.event.reify())
 
126
            raise
 
127
    try:
 
128
        result = store.transact(runHelper)
 
129
    except eimaginary.ActionFailure, e:
 
130
        broadcaster.broadcastRevertEvents()
 
131
        return None
 
132
    else:
 
133
        broadcaster.broadcastEvents()
 
134
        return result
 
135
 
 
136
 
 
137
 
108
138
class Command(object):
109
139
    __metaclass__ = CommandType
110
140
    infrastructure = True
119
149
        Take a player, line, and dictionary of parse results and execute the
120
150
        actual Action implementation.
121
151
 
122
 
        This takes responsibility for setting up the transactional event
123
 
        broadcasting junk, handling action errors, and broadcasting commit or
124
 
        revert events.
125
 
 
126
152
        @param player: A provider of C{self.actorInterface}
127
153
        @param line: A unicode string containing the original input
128
154
        @param match: A dictionary containing some parse results to pass
129
155
        through to the C{run} method.
 
156
 
130
157
        """
131
 
        broadcaster = TransactionalEventBroadcaster()
132
 
        def runHelper():
133
 
            # Set up event context for the duration of the action
134
 
            # run.  Additionally, handle raised ActionFailures by
135
 
            # adding their events to the revert event list and
136
 
            # re-raising them so they will revert the transaction.
137
 
            try:
138
 
                return context.call(
139
 
                    {iimaginary.ITransactionalEventBroadcaster: broadcaster},
140
 
                    self.run, player, line, **match)
141
 
            except eimaginary.ActionFailure, e:
142
 
                broadcaster.addRevertEvent(e.event.reify())
143
 
                raise
144
 
        try:
145
 
            result = player.store.transact(runHelper)
146
 
        except eimaginary.ActionFailure, e:
147
 
            broadcaster.broadcastRevertEvents()
148
 
            return None
149
 
        else:
150
 
            broadcaster.broadcastEvents()
151
 
            return result
 
158
        runEventTransaction(player.store, self.run, player, line, **match)
 
159
 
 
160