john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

decorator example ContentServiceUnexpectedExceptionDecoratorExample

CONTENTSERVCE.PY
....
    @service()
    def list_folder( self , path ) :
        """
        enumerate all of the non deleted folders and files for a given path and returns the latest sequence number before enumeration began
        :raises   ParentNotFoundException, PathNotFoundException, PathIsNotAFolderException
        """
        counter = SeqNumberService()
        latestSequenceNumber = counter.get_count()
        if path == '/':
            parentOid = self.ROOT_OID
        else:
            try:
                currentNode = self._get_valid_folder_node( path )       # raises: PathNotFound, InvalidParentType
            except InvalidParentTypeException:
                raise PathIsNotAFolderException( u"list folder can only list a folder, not a file: {}".format( path ) )
            parentOid = currentNode.oid

        nodeListing = self.contentDBService.list_non_deleted_nodes_by_parent_oid( parentOid )
        folderList = list()
        fileList = list()
        for item in nodeListing:
            # ensure that folders are only returning folder attributes and files are only returning file attributes (not node attributes!)
            if item.isFolder:
                folderItem = { 'name': item.name , 'seq': int( item.seq ) }
                folderList.append( folderItem )
            else:
                fileItem = { 'name': item.name , 'seq': int( item.seq ), 'hash': item.hashv , 'size': int( item.size ) , 'modified':  Utility.datetime_to_seconds(item.fileModified) }
                fileList.append( fileItem )
        batchResult = self.FolderAndFileBatchResult( folderList = folderList , fileList = fileList, latestSequenceNumber=latestSequenceNumber )
        return batchResult
...



class _ContentServiceDecorator():

    def __call__( self , func ):

        def wrapper( func_self , *args , **kwargs):
            try:
                return func( func_self , *args , **kwargs )

            except ContentServiceException:
                raise ContentServiceException
            except Exception as e:
                logging.error( traceback.print_exc( ) )
                logging.error( "UNEXPECTED EXCEPTION: {} in {}".format( e.__class__.__name__ , func.__name__) )
                raise UnexpectedContentServiceException

        wrapper.__name__ = func.__name__
        wrapper.__doc__ = func.__doc__
        return wrapper

service = _ContentServiceDecorator







CONTENTSERVICEEXCEPTION.PY

class ContentServiceException( Exception ):
    def __init__( self, message = '' ):
        super( Exception, self ).__init__()
        self.message = u'{}'.format( message )
    def __str__(self):
        return u"[{}]: {}".format( self.__class__.__name__,self.message )


class UnexpectedContentServiceException( ContentServiceException ): pass


class UnableToAcquireSequenceNumberLockException(ContentServiceException):pass    # All Atomic operations
class OutOfSequenceException(ContentServiceException):pass

class PathNotFoundException( ContentServiceException ):pass     # all!
class InvalidPathException( ContentServiceException ): pass     # all!

  • « symlink
  • Webapp2 tests »

Published

Apr 2, 2013

Category

python

~216 words

Tags

  • contentserviceunexpectedexceptiondecoratorexample 1
  • decorator 8
  • example 36
  • python 180