Example #1
0
    def test_action_check(self):
        from coopy.decorators import readonly, unlocked, abort_exception

        def no_decorator():
            pass

        @readonly
        def read_method():
            pass
        
        @unlocked
        def not_locked():
            pass

        @abort_exception
        def abort_on_exception():
            pass

        @readonly
        @unlocked
        def readonly_unlocked():
            pass

        self.assertEquals(
                (False, False, False),
                utils.action_check(no_decorator)
        )
        
        self.assertEquals(
                (True, False, False),
                utils.action_check(read_method)
        )
        
        self.assertEquals(
                (False, True, False),
                utils.action_check(not_locked)
        )

        self.assertEquals(
                (False, False, True),
                utils.action_check(abort_on_exception)
        )

        self.assertEquals(
                (True, True, False),
                utils.action_check(readonly_unlocked)
        )
Example #2
0
    def __getattr__(self, name):
        method =  method_or_none(self.obj, name)

        if not method:
            return getattr(self.obj, name)

        (readonly,unlocked,abort_exception) = action_check(method)

        #TODO: re-write
        if not readonly and hasattr(self, 'slave') and self.slave:
            raise Exception('This is a slave/read-only instance.')

        def method(*args, **kwargs):
            exception = None
            try:
                if not unlocked:
                    self.lock.acquire(1)

                #record all calls to clock.now()
                self.obj._clock = RecordClock()

                thread_ident = thread.get_ident()
                action = Action(thread_ident,
                                name,
                                datetime.now(),
                                args,
                                kwargs)
                system = None
                if not readonly:
                    self.publisher.publish_before(action)

                try:
                    system = action.execute_action(self.obj)
                except Exception as e:
                    logger.debug(CORE_LOG_PREFIX + 'Error: ' + str(e))
                    if abort_exception:
                        logger.debug(CORE_LOG_PREFIX +
                                'Aborting action' + str(action))
                    if not abort_exception:
                        self.publisher.publish_exception(action)
                    exception = e

                #restore clock
                action.results = self.obj._clock.results

                if not readonly and not abort_exception:
                    self.publisher.publish(action)

            finally:
                if not unlocked:
                    self.lock.release()

            if exception:
                raise exception

            return system
        return method