Lockable.py :  » Web-Frameworks » Zope » Zope-2.6.0 » lib » python » webdav » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » lib » python » webdav » Lockable.py
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################

__version__ = "$Revision: 1.8 $"[11:-2]

from WriteLockInterface import WriteLockInterface,LockItemInterface
from EtagSupport import EtagSupport
from LockItem import LockItem
from AccessControl import ClassSecurityInfo
from Globals import PersistentMapping
import Acquisition

class ResourceLockedError(Exception): pass

class LockableItem(EtagSupport):
    """\
    Implements the WriteLockInterface, and is inherited by Resource which
    is then inherited by the majority of Zope objects.  For an object to
    be lockable, however, it should have the WriteLockInterface in its
    __implements__ list, ie:

    __implements__ = (WriteLockInterface,)
    """

    # Protect methods using declarative security
    security = ClassSecurityInfo()
    security.declarePrivate('wl_lockmapping')
    security.declarePublic('wl_isLocked', 'wl_getLock', 'wl_isLockedByUser',
                           'wl_lockItems', 'wl_lockValues', 'wl_lockTokens',)
    security.declareProtected('WebDAV Lock items',
                              'wl_grantLockToUser', 'wl_setLock')
    security.declareProtected('WebDAV Unlock items', 'wl_delLock')
    security.declareProtected('Manage WebDAV Locks', 'wl_clearLocks')

    # Setting default roles for permissions - we want owners of conent
    # to be able to lock.
    security.setPermissionDefault('WebDAV Lock items', ('Manager', 'Owner',))
    security.setPermissionDefault('WebDAV Unlock items',('Manager','Owner',))


    def wl_lockmapping(self, killinvalids=0, create=0):
        """ if 'killinvalids' is 1, locks who are no longer valid
        will be deleted """

        try: locks = getattr(self, '_dav_writelocks', None)
        except: locks = None

        if locks is None:
            if create:
                locks = self._dav_writelocks = PersistentMapping()
            else:
                # Don't generate a side effect transaction.
                locks = {}
            return locks
        elif killinvalids:
            # Delete invalid locks
            for token, lock in locks.items():
                if not lock.isValid():
                    del locks[token]
            if (not locks) and hasattr(Acquisition.aq_base(self),
                                       '__no_valid_write_locks__'):
                self.__no_valid_write_locks__()
            return locks
        else:
            return locks

    def wl_lockItems(self, killinvalids=0):
        return self.wl_lockmapping(killinvalids).items()

    def wl_lockValues(self, killinvalids=0):
        return self.wl_lockmapping(killinvalids).values()

    def wl_lockTokens(self, killinvalids=0):
        return self.wl_lockmapping(killinvalids).keys()

    def wl_hasLock(self, token, killinvalids=0):
        if not token: return 0
        return token in self.wl_lockmapping(killinvalids).keys()

    def wl_isLocked(self):
        # returns true if 'self' is locked at all
        # We set 'killinvalids' to 1 to delete all locks who are no longer
        # valid (timeout has been exceeded)
        locks = self.wl_lockmapping(killinvalids=1)

        if locks.keys(): return 1
        else: return 0

    def wl_setLock(self, locktoken, lock):
        locks = self.wl_lockmapping(create=1)
        if LockItemInterface.isImplementedBy(lock):
            if locktoken == lock.getLockToken():
                locks[locktoken] = lock
            else:
                raise ValueError, 'Lock tokens do not match'
        else:
            raise ValueError, 'Lock does not implement the LockItem Interface'

    def wl_getLock(self, locktoken):
        locks = self.wl_lockmapping(killinvalids=1)
        return locks.get(locktoken, None)

    def wl_delLock(self, locktoken):
        locks = self.wl_lockmapping()
        if locks.has_key(locktoken):
            del locks[locktoken]

    def wl_clearLocks(self):
        # Called by lock management machinery to quickly and effectively
        # destroy all locks.
        try:
            locks = self.wl_lockmapping()
            locks.clear()
        except:
            # The locks may be totally messed up, so we'll just delete
            # and replace.
            if hasattr(self, '_dav_writelocks'): del self._dav_writelocks
            if WriteLockInterface.isImplementedBy(self):
                self._dav_writelocks = PersistentMapping()

        # Call into a special hook used by LockNullResources to delete
        # themselves.  Could be used by other objects who want to deal
        # with the state of empty locks.
        if hasattr(Acquisition.aq_base(self), '__no_valid_write_locks__'):
            self.__no_valid_write_locks__()


import Globals
Globals.default__class_init__(LockableItem)


### Utility functions

def wl_isLocked(ob):
    """ Returns true if the object is locked, returns 0 if the object
    is not locked or does not implement the WriteLockInterface """
    return WriteLockInterface.isImplementedBy(ob) and ob.wl_isLocked()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.