Changeset 7093


Ignore:
Timestamp:
12 Nov 2011, 07:32:42 (13 years ago)
Author:
uli
Message:

Implement deleteFile() and deleteFileByContext() for ExtFileStore?. We
can now also remove files from ExtFileStores?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/imagestorage.py

    r7073 r7093  
    314314        return
    315315
    316     def getFile(self, file_id):
    317         """Get a file stored under file ID `file_id`.
    318 
    319         Returns a file already opened for reading.
    320 
    321         If the file cannot be found ``None`` is returned.
    322 
    323         This methods takes into account registered handlers for any
    324         marker put into the file_id.
    325 
    326         .. seealso:: :class:`DefaultFileStoreHandler`
     316    def _pathFromFileID(self, file_id):
     317        """Helper method to create filesystem path from FileID.
     318
     319        Used class-internally. Do not rely on this method when working
     320        with an :class:`ExtFileStore` instance from other components.
    327321        """
    328322        marker, filename, base, ext = self.extractMarker(file_id)
     
    330324                               default=DefaultFileStoreHandler())
    331325        path = handler.pathFromFileID(self, self.root, file_id)
     326        return path
     327
     328    def getFile(self, file_id):
     329        """Get a file stored under file ID `file_id`.
     330
     331        Returns a file already opened for reading.
     332
     333        If the file cannot be found ``None`` is returned.
     334
     335        This methods takes into account registered handlers for any
     336        marker put into the file_id.
     337
     338        .. seealso:: :class:`DefaultFileStoreHandler`
     339        """
     340        path = self._pathFromFileID(file_id)
    332341        if not os.path.exists(path):
    333342            return None
     
    357366        file_id = IFileStoreNameChooser(context).chooseName(attr=attr)
    358367        return self.getFile(file_id)
     368
     369    def deleteFile(self, file_id):
     370        """Delete file stored under `file_id` in storage.
     371
     372        The file is physically removed from filesystem.
     373        """
     374        path = self._pathFromFileID(file_id)
     375        if not os.path.exists(path) or not os.path.isfile(path):
     376            return
     377        os.unlink(path)
     378        return
     379
     380    def deleteFileByContext(self, context, attr=None):
     381        """Remove file identified by `context` and `attr` if it exists.
     382
     383        This method takes into account registered handlers and file
     384        name choosers for context types to build an intermediate file
     385        id for the context and `attr` given.
     386
     387        Both, `context` and `attr` are used to find (`context`)
     388        and feed (`attr`) an appropriate file name chooser.
     389
     390        This is a convenience method that internally calls
     391        :meth:`getFile`.
     392
     393        .. seealso:: :class:`FileStoreNameChooser`,
     394                     :class:`DefaultFileStoreHandler`.
     395
     396        """
     397        file_id = IFileStoreNameChooser(context).chooseName(attr=attr)
     398        return self.deleteFile(file_id)
    359399
    360400    def createFile(self, filename, f):
Note: See TracChangeset for help on using the changeset viewer.