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

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

class ExtFile(Persistent):

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

    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]

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

*    @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]
        # Handle case when value is a string
        if isinstance(value, unicode):
            value = value.encode('UTF-8')
        if isinstance(value, str):
            value = StringIO(value)
        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]
                    inst.__dict__[self.__name] = newDigest

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"""
        if os.path.exists(path):

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

Zope-Dev maillist  -  Zope-Dev@zope.org
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope )

Reply via email to