guillotina.utils

guillotina.utils.get_current_request()

Return the current request by heuristically looking it up from stack

Return type:<InterfaceClass guillotina.interfaces.IRequest>
guillotina.utils.get_content_path(content)

Generate full path of resource object

Parameters:content (<InterfaceClass guillotina.interfaces.content.IResource>) – object to get path from
Return type:str
guillotina.utils.iter_parents(content)

Iterate through all the parents of a content object

Parameters:content (<InterfaceClass guillotina.interfaces.content.IResource>) – object to get path from
Return type:Iterator[<InterfaceClass guillotina.interfaces.content.IResource>]
guillotina.utils.navigate_to(obj, path)

Get a sub-object.

Parameters:
  • obj (<InterfaceClass guillotina.interfaces.content.IAsyncContainer>) – object to get path from
  • path (str) – relative path to object you want to retrieve
guillotina.utils.get_owners(obj)

Return owners of an object

Parameters:obj (<InterfaceClass guillotina.interfaces.content.IResource>) – object to get path from
Return type:list
guillotina.utils.get_object_url(ob, request=None, **kwargs)

Generate full url of object.

Parameters:
  • ob (<InterfaceClass guillotina.interfaces.content.IResource>) – object to get url for
  • request (Optional[<InterfaceClass guillotina.interfaces.IRequest>]) – relative path to object you want to retrieve
Return type:

Optional[str]

guillotina.utils.get_object_by_oid(oid, txn=None)

Get an object from an oid

Parameters:
  • oid (str) – Object id of object you need to retreive
  • txn – Database transaction object. Will get current transaction is not provided
Return type:

Optional[<InterfaceClass guillotina.interfaces.content.IResource>]

guillotina.utils.get_behavior(ob, iface, create=False)

Generate behavior of object.

Parameters:
  • ob – object to get behavior for
  • interface – interface registered for behavior
  • create – if behavior data empty, should we create it?
guillotina.utils.get_authenticated_user(request=None)

Get the currently authenticated user

Parameters:request (Optional[<InterfaceClass guillotina.interfaces.IRequest>]) – request the user is authenticated against
Return type:Optional[<InterfaceClass guillotina.interfaces.security.IPrincipal>]
guillotina.utils.get_authenticated_user_id(request=None)

Get the currently authenticated user id

Parameters:request (Optional[<InterfaceClass guillotina.interfaces.IRequest>]) – request the user is authenticated against
Return type:Optional[str]
guillotina.utils.strings_differ(string1, string2)

Check whether two strings differ while avoiding timing attacks.

This function returns True if the given strings differ and False if they are equal. It’s careful not to leak information about where they differ as a result of its running time, which can be very important to avoid certain timing-related crypto attacks:

>>> strings_differ('one', 'one')
False
>>> strings_differ('one', 'two')
True
Parameters:
  • string1 (str) –
  • string2 (str) –
Return type:

bool

guillotina.utils.get_random_string(length=30, allowed_chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')

Heavily inspired by Plone/Django Returns a securely generated random string.

>>> get_random_string(length=10)
Parameters:
  • length (int) –
  • allowed_chars (str) –
Return type:

str

guillotina.utils.resolve_dotted_name(name)

import the provided dotted name

>>> resolve_dotted_name('guillotina.interfaces.IRequest')
<InterfaceClass guillotina.interfaces.IRequest>
Parameters:name (str) – dotted name
Return type:~ResolvableType
guillotina.utils.get_caller_module(level=2, sys=<module 'sys' (built-in)>)

Pulled out of pyramid

Return type:module
guillotina.utils.resolve_module_path(path)
Return type:str
guillotina.utils.get_module_dotted_name(ob)
Return type:str
guillotina.utils.get_dotted_name(ob)

Convert a module/class/function to dotted path string

Parameters:ob (~ResolvableType) – the object you’d like to convert
Return type:str
guillotina.utils.import_class(import_string)

Import class from string

Parameters:import_string (str) – dotted class name
Return type:module
guillotina.utils.resolve_path(file_path)

Resolve path to file inside python module

>>> resolve_path('guillotina:__init__.py')
PosixPath('/Users/vangheem/onna/onna-canonical/libsrc/guillotina/guillotina/__init__.py')
Parameters:file_path (str) – module:path string
Return type:Path
guillotina.utils.merge_dicts(d1, d2)

Update two dicts of dicts recursively, if either mapping has leaves that are non-dicts, the second’s leaf overwrites the first’s.

Return type:dict
guillotina.utils.apply_coroutine(func, *args, **kwargs)

Call a function with the supplied arguments. If the result is a coroutine, await it.

>>> async def foobar(): return 'hi'
>>> async def async_foobar(): return 'hi'
>>> await apply_coroutine(foobar)
'hi'
>>> await apply_coroutine(async_foobar)
'hi'
Parameters:
  • func (function) – function to run as coroutiune if one
  • *args – args to call function with
  • **kwargs – kwargs to call function with
Return type:

object

guillotina.utils.lazy_apply(func, *call_args, **call_kwargs)

apply arguments in the order that they come in the function signature and do not apply if argument not provided

call_args will be applied in order if func signature has args. otherwise, call_kwargs is the magic here…

guillotina.utils.safe_unidecode(val)

Convert bytes to a string in a safe way

>>> safe_unidecode(b'foobar')
'foobar'
Parameters:val (bytes) – bytes to convert
Return type:str
class guillotina.utils.Navigator(txn, container)

Provides a consistent view of the loaded objects, ensuring that only a single instance of each resource path is loaded.

It is particularly useful when objects that may have been modified or added have to be found by path (something that the content api cannot do).

To keep the Navigator index consistent, all object loadings must be done through its API, meaning the content api should not be used anymore. In case it cannot be avoided, make sure to use the sync() function before re-using the Navigator.

Parameters:
  • txn (Transaction) – A Transaction instance
  • container (Container) – A Container
delete(obj)

Delete an object from the tree

When using Navigator, this method should be used so that the Navigator can update its index and not return it anymore.

get(path)

Returns an object from its path.

If this path was already modified or loaded by Navigator, the same object instance will be returned.

sync()

Resync the index with the transaction

If some operations may have added, modified or deleted objects that the Navigator does not know, sync() should be called so that the index is up-to-date with the transaction.

guillotina.utils.execute

guillotina.utils.execute.after_request(func, *args, _name=None, _request=None, _scope='', **kwargs)

Execute after the request has successfully finished.

Parameters:
  • func (Callable[…, Coroutine[Any, Any, Any]]) – function to be queued
  • _name – unique identifier to give in case you want to prevent duplicates
  • _scope – customize scope of after commit to run for instead of default(successful request)
  • _request – provide request object to prevent request lookup
  • *args – arguments to call the func with
  • **kwargs – keyword arguments to call the func with
guillotina.utils.execute.after_request_failed(func, *args, _name=None, _request=None, **kwargs)

Execute after the request has failed or errored.

Parameters:
  • func (Callable[…, Coroutine[Any, Any, Any]]) – function to be queued
  • _request – provide request object to prevent request lookup
  • *args – arguments to call the func with
  • **kwargs – keyword arguments to call the func with
guillotina.utils.execute.after_commit(func, *args, _request=None, **kwargs)

Execute a commit to the database.

Parameters:
  • func (Callable) – function to be queued
  • _request – provide request object to prevent request lookup
  • *args – arguments to call the func with
  • **kwargs – keyword arguments to call the func with
guillotina.utils.execute.before_commit(func, *args, _request=None, **kwargs)

Execute before a commit to the database.

Parameters:
  • func (Callable[…, Coroutine[Any, Any, Any]]) – function to be queued
  • _request – provide request object to prevent request lookup
  • *args – arguments to call the func with
  • **kwargs – keyword arguments to call the func with
guillotina.utils.execute.in_pool(func, *args, request=None, **kwargs)

Execute function in the async pool.

Parameters:
  • func (Callable[…, Coroutine[Any, Any, Any]]) – function to be queued
  • _request – provide request object to prevent request lookup. Provide if function be wrapped in database transaction.
  • *args – arguments to call the func with
  • **kwargs – keyword arguments to call the func with
Return type:

ExecuteContext

guillotina.utils.execute.in_queue(view)

Execute view-type object(context, request) in the async queue.

Parameters:view (Union[<InterfaceClass guillotina.interfaces.views.IView>, GenerateQueueView]) – view to be queued
Return type:ExecuteContext
guillotina.utils.execute.in_queue_with_func(func, *args, _request=None, **kwargs)

Execute function in the async queue.

Parameters:
  • func (Callable[…, Coroutine[Any, Any, Any]]) – function to be queued
  • _request – provide request object to prevent request lookup
  • *args – arguments to call the func with
  • **kwargs – keyword arguments to call the func with
Return type:

ExecuteContext

class guillotina.utils.execute.ExecuteContext(func, *args, **kwargs)

Execution context object to allow you to run the function in different contexts.

after_commit(_request=None)

Execute after we commit to the database.

Parameters:_request – provide request object to prevent request lookup
after_request(_name=None, _request=None)

Execute after the request has successfully finished.

Parameters:
  • _name – unique identifier to give in case you want to prevent duplicates
  • _request – provide request object to prevent request lookup
after_request_failed(_name=None, _request=None)

Execute after the request has failed or errored.

Parameters:
  • _name – unique identifier to give in case you want to prevent duplicates
  • _request – provide request object to prevent request lookup
before_commit(_request=None)

Execute just before we commit to the database.

Parameters:_request – provide request object to prevent request lookup