guillotina.utils

guillotina.utils.get_current_request()

Return the current request by heuristically looking it up from stack

Return type:guillotina.interfaces.misc.IRequest
guillotina.utils.get_content_path(content)

Generate path of resource object from the container

Parameters:content (guillotina.interfaces.content.IResource) – object to get path from
Return type:str
guillotina.utils.get_full_content_path(ob)

Generate full path of resource object from root

Parameters:content – object to get path from
Return type:str
guillotina.utils.iter_parents(content)

Iterate through all the parents of a content object

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

Get a sub-object.

Parameters:
  • obj (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 (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 (guillotina.interfaces.content.IResource) – object to get url for
  • request (Optional[guillotina.interfaces.misc.IRequest]) – relative path to object you want to retrieve
Return type:

Optional[str]

guillotina.utils.get_object_by_uid(uid, txn=None)

Get an object from an uid

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

guillotina.db.orm.interfaces.IBaseObject

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_database(db_id, root=None)

Get configured database

Parameters:db_id – configured database id
Return type:guillotina.interfaces.content.IDatabase
guillotina.utils.get_current_db()

Return the current db by heuristically looking it up from stack

Return type:guillotina.interfaces.content.IDatabase
guillotina.utils.get_current_container()

Return the current container by heuristically looking it up from stack

Return type:guillotina.interfaces.content.IContainer
guillotina.utils.find_container(context=None)

Find container based on contextvar or by looking up the container from the provided context parameter

Return type:Optional[guillotina.interfaces.content.IContainer]
guillotina.utils.get_current_transaction()

Return the current request by heuristically looking it up from stack

Return type:guillotina.db.interfaces.ITransaction
guillotina.utils.get_url(req, path)

Return calculated url from a request object taking into account X-VirtualHost-Monster header and X-Forwarded header

guillotina.utils.get_schema_validator(schema_name)

Get a json schema validator by the definition name

Parameters:schema_name (str) – Name of the json schema type
guillotina.utils.get_registry(context=None)
Return type:Optional[guillotina.interfaces.content.IRegistry]
guillotina.utils.get_authenticated_user()

Get the currently authenticated user

Return type:Optional[guillotina.interfaces.security.IPrincipal]
guillotina.utils.get_authenticated_user_id()

Get the currently authenticated user id

Return type:Optional[str]
guillotina.utils.get_security_policy(user=None)

Get the current security policy

Parameters:user (Optional[guillotina.interfaces.security.IPrincipal]) – user to use for security policy
Return type:guillotina.interfaces.security.ISecurityPolicy
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:Any
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/user/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 (Union[str, bytes]) – bytes to convert
Return type:str
guillotina.utils.run_async(func, *args, **kwargs)

Run a non-async function in an executor

>>> async def foobar(): return 'hi'
>>> await run_async(foobar)
'hi'
Parameters:
  • func – function to run as coroutiune if one
  • *args

    args to call function with

  • **kwargs

    kwargs to call function with

Return type:

object

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, _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)
  • *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, **kwargs)

Execute after the request has failed or errored.

Parameters:
  • func (Callable[…, Coroutine[Any, Any, Any]]) – function to be queued
  • *args – arguments to call the func with
  • **kwargs – keyword arguments to call the func with
guillotina.utils.execute.after_commit(func, *args, **kwargs)

Execute a commit to the database.

Parameters:
  • func (Callable) – function to be queued
  • *args – arguments to call the func with
  • **kwargs – keyword arguments to call the func with
guillotina.utils.execute.before_commit(func, *args, **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, **kwargs)

Execute function in the async pool.

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

ExecuteContext

guillotina.utils.execute.in_queue(func, *args, **kwargs)

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

Parameters:view – view to be queued
Return type:ExecuteContext
guillotina.utils.execute.in_queue_with_func(func, *args, **kwargs)

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

Parameters:view – view to be queued
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()

Execute after we commit to the database.

after_request(_name=None)

Execute after the request has successfully finished.

Parameters:_name – unique identifier to give in case you want to prevent duplicates
after_request_failed(_name=None)

Execute after the request has failed or errored.

Parameters:_name – unique identifier to give in case you want to prevent duplicates
before_commit()

Execute just before we commit to the database.