def get_by_name(self, entry_name, include=None, **kwargs): assert hasattr(self.model_cls, 'name') result = self.list(include=include, filters={'name': entry_name}) if not result: raise exceptions.StorageError( 'Requested {0} with NAME `{1}` was not found'.format( self.model_cls.__name__, entry_name)) elif len(result) > 1: raise exceptions.StorageError( 'Requested {0} with NAME `{1}` returned more than 1 value'. format(self.model_cls.__name__, entry_name)) else: return result[0]
def _safe_commit(self): """Try to commit changes in the session. Roll back if exception raised Excepts SQLAlchemy errors and rollbacks if they're caught """ try: self._session.commit() except (SQLAlchemyError, ValueError) as e: self._session.rollback() raise exceptions.StorageError('SQL Storage error: {0}'.format( str(e)))
def connect(self): """ Establishes a connection and destroys it after use. """ try: self._establish_connection() yield self except BaseException as e: raise exceptions.StorageError(str(e)) finally: self._destroy_connection()
def get(self, entry_id, include=None, **kwargs): """Return a single result based on the model class and element ID """ query = self._get_query(include, {'id': entry_id}) result = query.first() if not result: raise exceptions.StorageError( 'Requested `{0}` with ID `{1}` was not found'.format( self.model_cls.__name__, entry_id)) return result
def read(self, entry_id, path=None, **_): """ Retrieve the content of a file system storage resource. :param str entry_type: the type of the entry. :param str entry_id: the id of the entry. :param str path: a path to a specific resource. :return: the content of the file :rtype: bytes """ resource_relative_path = os.path.join(self.name, entry_id, path or '') resource = os.path.join(self.directory, resource_relative_path) if not os.path.exists(resource): raise exceptions.StorageError( "Resource {0} does not exist".format(resource_relative_path)) if not os.path.isfile(resource): resources = os.listdir(resource) if len(resources) != 1: raise exceptions.StorageError( 'No resource in path: {0}'.format(resource)) resource = os.path.join(resource, resources[0]) with open(resource, 'rb') as resource_file: return resource_file.read()
def read(self, entry_id, path, **_): """ Retrieves the contents of a file. :param entry_id: entry ID :param path: path to resource :return: contents of the file :rtype: bytes """ resource_relative_path = os.path.join(self.name, entry_id, path or '') resource = os.path.join(self.directory, resource_relative_path) if not os.path.exists(resource): raise exceptions.StorageError("Resource {0} does not exist". format(resource_relative_path)) if not os.path.isfile(resource): resources = os.listdir(resource) if len(resources) != 1: raise exceptions.StorageError( 'Failed to read {0}; Reading a directory is ' 'only allowed when it contains a single resource'.format(resource)) resource = os.path.join(resource, resources[0]) with open(resource, 'rb') as resource_file: return resource_file.read()
def download(self, entry_id, destination, path=None, **_): """ Downloads a file or directory. :param entry_id: entry ID :param destination: download destination :param path: path to download relative to the root of the entry (otherwise all) """ resource_relative_path = os.path.join(self.name, entry_id, path or '') resource = os.path.join(self.directory, resource_relative_path) if not os.path.exists(resource): raise exceptions.StorageError( "Resource {0} does not exist".format(resource_relative_path)) if os.path.isfile(resource): shutil.copy2(resource, destination) else: dir_util.copy_tree(resource, destination) # pylint: disable=no-member
def download(self, entry_id, destination, path=None, **_): """ Download a specific file or dir from the file system resource storage. :param str entry_id: the id of the entry. :param str destination: the destination to download to :param str path: the path to download relative to the root of the entry (otherwise all). """ resource_relative_path = os.path.join(self.name, entry_id, path or '') resource = os.path.join(self.directory, resource_relative_path) if not os.path.exists(resource): raise exceptions.StorageError( "Resource {0} does not exist".format(resource_relative_path)) if os.path.isfile(resource): shutil.copy2(resource, destination) else: dir_util.copy_tree(resource, destination) # pylint: disable=no-member