[Zope-dev] delete feature to z3c.extfile
Jürgen kartnaller
juergen.kartnaller at gmail.com
Wed Sep 24 11:28:57 EDT 2008
On Wed, Sep 24, 2008 at 9:10 AM, Jayarajan Jn <jayarajan at musmo.com> wrote:
> Hi Jürgen,
>
> First of all thank you for your reply....
> hmm you are right too... And i am comfortable with the explicit delete. But
> in my prototype for i have also added webDAV interface for managing
> extfile.... Then when a user deletes the extfile object through webDAV
> interface, the file remains there.... coz i don't know where to put my codes
> to call delete() in that case... And in my project there wont be more than
> one extfiles refering to same file! so in my case its ok to go for implicit
> delete! but its now working!
>
You can never be sure if a file is only used once. If two user upload the
same file only one copy is stored in extfile because both files have the
same hash.
>
> But form a general perspective you are correct.... may be HashDir will have
> to evolve to add reference counting feture just like python do!
>
> On Wed, Sep 24, 2008 at 11:34 AM, Jürgen kartnaller <
> juergen.kartnaller at gmail.com> wrote:
>
>> Hi jayaraj,
>> it is definitely not a good idea to implicitily delete files.
>>
>> What if two ExtFile objects reference the same file ?
>>
>> Jürgen
>>
>> On Wed, Sep 24, 2008 at 4:37 AM, Jayarajan Jn <jayarajan at musmo.com>wrote:
>>
>>> 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
>>>
>>> _______________________________________________
>>> Zope-Dev maillist - Zope-Dev at zope.org
>>> http://mail.zope.org/mailman/listinfo/zope-dev
>>> ** No cross posts or HTML encoding! **
>>> (Related lists -
>>> http://mail.zope.org/mailman/listinfo/zope-announce
>>> http://mail.zope.org/mailman/listinfo/zope )
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zope-dev/attachments/20080924/6f4f348d/attachment-0001.html
More information about the Zope-Dev
mailing list