cement.core.handler
Cement core handler module.
- class cement.core.handler.Handler(**kw: Any)[source]
-
Base handler class that all Cement Handlers should subclass from.
- class Meta[source]
Bases:
objectHandler meta-data (can also be passed as keyword arguments to the parent class).
- config_defaults: Dict[str, Any] | None = None
A config dictionary that is merged into the applications config in the
[<config_section>]block. These are defaults and do not override any existing defaults under that section.
- class cement.core.handler.HandlerManager(app: App)[source]
Bases:
objectManages the handler system to define, get, resolve, etc handlers with the Cement Framework.
- get(interface: str, handler_label: str, fallback: Type[Handler] | None = None, **kwargs: Any) Handler | Type[Handler][source]
Get a handler object.
- Parameters:
- Keyword Arguments:
setup (bool) – Whether or not to call
setup()on the handler before returning. This will not be called on thefallbackif no the handler given does not exist.- Returns:
An uninstantiated handler object
- Return type:
- Raises:
cement.core.exc.InterfaceError – If the
interfacedoes not exist, or if the handler itself does not exist.
Example
_handler = app.handler.get('output', 'json') output = _handler() output._setup(app) output.render(dict(foo='bar'))
- list(interface: str) List[Type[Handler]][source]
Return a list of handlers for a given
interface.- Parameters:
interface (str) – The interface of the handler (i.e.
output)- Returns:
Handler labels (str) that match
interface.- Return type:
- Raises:
cement.core.exc.InterfaceError – If the
interfacedoes not exist.
Example
app.handler.list('log')
- register(handler_class: Type[Handler], force: bool = False) None[source]
Register a handler class to an interface. If the same object is already registered then no exception is raised, however if a different object attempts to be registered to the same name a
InterfaceErroris raised.- Parameters:
handler_class (Handler) – The uninstantiated handler class to register.
- Keyword Arguments:
force (bool) – Whether to allow replacement if an existing
registered. (handler of the same label is already)
- Raises:
cement.core.exc.InterfaceError – If the
handler_classdoes not implementHandler, or ifhandler_classdoes not properly sub-class it’s interface.cement.core.exc.InterfaceError – If the
handler_class.Meta.interfacedoes not exist
Usage:
class MyDatabaseHandler(object): class Meta: interface = IDatabase label = 'mysql' def connect(self): # ... app.handler.register(MyDatabaseHandler)
- registered(interface: str, handler_label: str) bool[source]
Check if a handler is registered.
- Parameters:
- Returns:
Trueif the handler is registered,Falseotherwise- Return type:
Example
app.handler.registered('log', 'colorlog')
- resolve(interface: str, handler_def: str | Handler | Type[Handler], **kwargs: Any) Handler | None[source]
Resolves the actual handler, as it can be either a string identifying the handler to load from
self.__handlers__, or it can be an instantiated or non-instantiated handler class.- Parameters:
- Keyword Arguments:
raise_error (bool) – Whether or not to raise an exception if unable to resolve the handler.
meta_defaults (dict) – Optional meta-data dictionary used as defaults to pass when instantiating uninstantiated handlers. Use
App.Meta.meta_defaultsby default.setup (bool) – Whether or not to call
.setup()before return. Default:False
- Returns:
The instantiated handler object.
- Return type:
instance
Example
# via label (str) log = app.handler.resolve('log', 'colorlog') # via uninstantiated handler class log = app.handler.resolve('log', ColorLogHanddler) # via instantiated handler instance log = app.handler.resolve('log', ColorLogHandler())