Fork me on GitHub

sqlalchemy_imageattach.context — Scoped context of image storage

Scoped context makes other modules able to vertically take an image store object without explicit parameter for it. It’s similar to Flask‘s design decision and Werkzeug‘s context locals. Context locals are workaround to use dynamic scoping in programming languages that doesn’t provide it (like Python).

For example, a function can take an image store to use as its parameter:

def func(store):
    url = store.locate(image)
    # ...

func(fs_store)

But, what if for various reasions it can’t take an image store as parameter? You should vertically take it using scoped context:

def func():
    current_store.locate(image)

with store_context(fs_store):
    func()

What if you have to pass the another store to other subroutine?:

def func(store):
    decorated_store = DecoratedStore(store)
    func2(decorated_store)

def func2(store):
    url = store.locate(image)
    # ...

func(fs_store)

The above code can be rewritten using scoped context:

def func():
    decorated_store = DecoratedStore(current_store)
    with store_context(decorated_store):
        func2()

def func2():
    url = current_store.locate(image)
    # ...

with store_context(fs_store):
    func()
exception sqlalchemy_imageattach.context.ContextError

The exception which rises when the current_store is required but there’s no currently set store context.

class sqlalchemy_imageattach.context.LocalProxyStore(get_current_object, repr_string=None)

Proxy of another image storage.

Parameters:
  • get_current_object (collections.Callable) – a function that returns “current” store
  • repr_string (str) – an optional string for repr()
sqlalchemy_imageattach.context.context_stacks = {}

(dict) The dictionary of concurrent contexts to their stacks.

sqlalchemy_imageattach.context.current_store = sqlalchemy_imageattach.context.current_store

(LocalProxyStore) The currently set context of the image store backend. It can be set using store_context().

sqlalchemy_imageattach.context.get_current_context_id()

Identifis which context it is (greenlet, stackless, or thread).

Returns:the identifier of the current context.
sqlalchemy_imageattach.context.get_current_store()

The lower-level function of current_store. It returns the actual store instance while current_store is a just proxy of it.

Returns:the actual object of the currently set image store
Return type:Store
sqlalchemy_imageattach.context.pop_store_context()

Manually pops the current store from the stack.

Although store_context() and with keyword are preferred than using it, it’s useful when you have to push and pop the current stack on different hook functions like setup/teardown.

Returns:the current image store
Return type:Store
sqlalchemy_imageattach.context.push_store_context(store)

Manually pushes a store to the current stack.

Although store_context() and with keyword are preferred than using it, it’s useful when you have to push and pop the current stack on different hook functions like setup/teardown.

Parameters:store (Store) – the image store to set to the current_store
sqlalchemy_imageattach.context.store_context(*args, **kwds)

Sets the new (nested) context of the current image storage:

with store_context(store):
    print current_store

It could be set nestedly as well:

with store_context(store1):
    print current_store  # store1
    with store_context(store2):
        print current_store  # store2
    print current_store  # store1 back
Parameters:store (Store) – the image store to set to the current_store