[Zope-dev] delete feature to z3c.extfile

Jayarajan Jn jayarajan at musmo.com
Tue Sep 23 22:37:51 EDT 2008


Hi,
i am now doing some prototypes for my projects which will be dealing with
tones of files. After a little scouting i decided to try z3c.extfile. Every
thing works fine. But i found it strange that there is no delete feature in
z3c.extfile. ie, even if i can delete a ExtFile object, the file in the hash
directory is not getting deleted! and it keeps on accumulating...

So i  thought i 'll add a delete feature... but  my  __del__() approach
doesn't  work for me. but i added an additional delete() function too which
can be invoked explicitly to delete the file before trying to delete ExtFile
object.

i made following  changes to the source...
inside  z3c.extfile.file.file.ExtFile,

----------------------------------------------------------------------------------------------
class ExtFile(Persistent):

    """A zope file implementation based on z3c.extfile"""

    interface.implements(IExtFile)
    data = ExtBytesProperty('data')

    def __init__(self, data='', contentType=''):
        self.data = data
        self.contentType = contentType

    # added the following lines#

*    def __del__(self):   # <- this is not being invoked when i try to
delete an extfile object
        del self.data
        #print "deleted data via destructor"

    def delete(self):     # <- added this to be able to manually able to
delete files
        del self.data
        #print "deleted data via delete()"

    # # # # # # #  # # # # # # #  #
*
    def getSize(self):
        return len(self.data)

----------------------------------------------------------------------------------------------

and the 'data' is a 'property'  (ExtBytesProperty)
so i made following changes to z3c.extfile.property.ExtBytesProperty

----------------------------------------------------------------------------------------------
class ExtBytesProperty(object):

    """a property which's values are stored as external files"""

    def __init__(self, name):
        self.__name = name



    # added the following lines#

*    def __delete__(self,inst):
        digest = inst.__dict__[self.__name]
        self.hd.delete(digest)

*    *# # # # # # #  # # # # # # #  #



*    @property
    def hd(self):
        return component.getUtility(interfaces.IHashDir)

    def __get__(self, inst, klass):

        if inst is None:
            return self
        digest = inst.__dict__.get(self.__name, _marker)
        if digest is _marker:
            return None
        return getFile(digest)

    def __set__(self, inst, value):
        # ignore if value is None
        if value is None:
            if inst.__dict__.has_key(self.__name):
                del inst.__dict__[self.__name]
            return
        # Handle case when value is a string
        if isinstance(value, unicode):
            value = value.encode('UTF-8')
        if isinstance(value, str):
            value = StringIO(value)
        value.seek(0)
        f = self.hd.new()
        while True:
            chunk = value.read(BLOCK_SIZE)
            if not chunk:
                newDigest = f.commit()
                oldDigest = inst.__dict__.get(self.__name, _marker)
                if newDigest == oldDigest:
                    # we have no change, so we have to seek to zero
                    # because this is normal behaviour when setting a
                    # new value
                    if hasattr(_storage, 'dataManager'):
                        if newDigest in _storage.dataManager.files:
                            f = _storage.dataManager.files[newDigest]
                            f.seek(0)
                else:
                    inst.__dict__[self.__name] = newDigest
                break
            f.write(chunk)
----------------------------------------------------------------------------------------------------

and at last added the real code which delete the file in hash directory too

i added following codes inside z3c.extfile.hashdir.HashDir class
---------------------------------------------------
def delete(self,digest):
        """delete the file"""
        path=self.getPath(digest)
        if os.path.exists(path):
            os.remove(path)
        return
----------------------------------------------------

Now, everything works fine when i try to delete an ExtFile object in ZODB,
__del__() is not being invoked!!!!!

can anyone tell me how can i fix this???

thanks in advance

jayaraj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zope-dev/attachments/20080924/a9e6b7cc/attachment.html 


More information about the Zope-Dev mailing list