Fork me on GitHub

sqlalchemy_imageattach.store — Image storage backend interface

This module declares a common interface for physically agnostic storage backends. Whatever a way to implement a storage, it needs only common operations of the interface. This consists of some basic operations like writing, reading, deletion, and finding urls.

Modules that implement the storage interface inside sqlalchemy_imageattach.storages package might help to implement a new storage backend.

class sqlalchemy_imageattach.store.Store

The interface of image storage backends. Every image storage backend implementation has to implement this.

delete(image)

Delete the file of the given image.

Parameters:image (sqlalchemy_imageattach.entity.Image) – the image to delete
delete_file(object_type, object_id, width, height, mimetype)

Deletes all reproducible files related to the image. It doesn’t raise any exception even if there’s no such file.

Parameters:
  • object_type (basestring) – the object type of the image to put e.g. 'comics.cover'
  • object_id (numbers.Integral) – the object identifier number of the image to put
  • width (numbers.Integral) – the width of the image to delete
  • height (numbers.Integral) – the height of the image to delete
  • mimetype (basestring) – the mimetype of the image to delete e.g. 'image/jpeg'
get_file(object_type, object_id, width, height, mimetype)

Gets the file-like object of the given criteria.

Parameters:
  • object_type (basestring) – the object type of the image to find e.g. 'comics.cover'
  • object_id (numbers.Integral) – the object identifier number of the image to find
  • width (numbers.Integral) – the width of the image to find
  • height (numbers.Integral) – the height of the image to find
  • mimetype (basestring) – the mimetype of the image to find e.g. 'image/jpeg'
Returns:

the file of the image

Return type:

file-like object, file

Raises exceptions.IOError:
 

when such file doesn’t exist

Note

This is an abstract method which has to be implemented (overridden) by subclasses.

It’s not for consumers but implementations, so consumers should use open() method instead of this.

get_url(object_type, object_id, width, height, mimetype)

Gets the file-like object of the given criteria.

Parameters:
  • object_type (basestring) – the object type of the image to find e.g. 'comics.cover'
  • object_id (numbers.Integral) – the object identifier number of the image to find
  • width (numbers.Integral) – the width of the image to find
  • height (numbers.Integral) – the height of the image to find
  • mimetype (basestring) – the mimetype of the image to find e.g. 'image/jpeg'
Returns:

the url locating the image

Return type:

basestring

Note

This is an abstract method which has to be implemented (overridden) by subclasses.

It’s not for consumers but implementations, so consumers should use locate() method instead of this.

locate(image)

Gets the URL of the given image.

Parameters:image (sqlalchemy_imageattach.entity.Image) – the image to get its url
Returns:the url of the image
Return type:basestring
open(image, use_seek=False)

Opens the file-like object of the given image. Returned file-like object guarantees:

To sum up: you definitely can read the file, in with statement and for loop.

Plus, if use_seek option is True:

For example, if you want to make a local copy of the image:

import shutil

with store.open(image) as src:
    with open(filename, 'wb') as dst:
        shutil.copyfileobj(src, dst)
Parameters:
  • image (sqlalchemy_imageattach.entity.Image) – the image to get its file
  • use_seek (bool) – whether the file should seekable. if True it maybe buffered in the memory. default is False
Returns:

the file-like object of the image, which is a context manager (plus, also seekable only if use_seek is True)

Return type:

file, FileProxy, file-like object

Raises exceptions.IOError:
 

when such file doesn’t exist

put_file(file, object_type, object_id, width, height, mimetype, reproducible)

Puts the file of the image.

Parameters:
  • file (file-like object, file) – the image file to put
  • object_type (basestring) – the object type of the image to put e.g. 'comics.cover'
  • object_id (numbers.Integral) – the object identifier number of the image to put
  • width (numbers.Integral) – the width of the image to put
  • height (numbers.Integral) – the height of the image to put
  • mimetype (basestring) – the mimetype of the image to put e.g. 'image/jpeg'
  • reproducible (bool) – True only if it’s reproducible by computing e.g. resized thumbnails. False if it cannot be reproduced e.g. original images

Note

This is an abstract method which has to be implemented (overridden) by subclasses.

It’s not for consumers but implementations, so consumers should use store() method instead of this.

store(image, file)

Stores the actual data file of the given image.

with open(imagefile, 'rb') as f:
    store.store(image, f)
Parameters: