granola.hooks.hooks module

Hooks provide a way to inject customizable behavior at preditermined locations.

All hooks are subclasses of BaseHook. There are two ways to construct a hook. Directly as a subclass of BaseHook or as a function decorated with the register_hook() decorator that converts a simple function into the more full featured hook class.

For a detailed comparison of constructing a hook class versus a hook by decorator

See also

Module Hook API

class granola.hooks.hooks.ApproachHook(attributes=None, include_or_exclude=SetRelationship.exclude, transition_asc_scaling=40, transition_dsc_scaling=100)

Bases: BaseHook

Hook that will on applicable attributes, when a new value is set, it will approach that value over a period time (dictated by transition_asc_scaling and transition_dsc_scaling). It uses a hyperbolic tangent function to allow smooth transition from start and end values.

Parameters
  • attributes (set, optional) – getter and setter attributes to include or exclude from this hook.

  • include_or_exclude (SetRelationship or str, optional) – Whether to include attributes in the hook or exclude attributes from the hook.

  • transition_asc_scaling (float) – An increase of delta (change) of 1 unit takes this many seconds longer to transition. Must be positive.

  • transition_dsc_scaling (float) – A decrease of delta (change) of 1 unit takes this many seconds longer to transition. Must be negative.

hooked_classes = [<class 'granola.command_readers.GettersAndSetters'>]
post_reading(hooked, result, data, **kwargs)

On applicable attributes, retrieve meta data class information set from pre_reading hook, and based on that information (how long it has been since the set time, how long the transition is supposed to take, etc), calculate the new attribute value and return it.

Parameters
  • hooked (GettersAndSetters) – instance of hooked class

  • result (str) – Returned result from serial command.

  • data (str) – Serial command

pre_reading(hooked, data, **kwargs)

On applicable attributes, record meta data about the pre_reading value, the end_value, how long it should take to complete the transition, and when the transition started. Store all of this information in a meta data class.

Parameters
  • hooked (GettersAndSetters) – instance of hooked class

  • data (str) – Serial command

validate_attribute_type(hooked, attribute)
class granola.hooks.hooks.LoopCannedQueries(attributes=None, include_or_exclude=SetRelationship.exclude, *args, **kwargs)

Bases: BaseHook

hooked_classes = [<class 'granola.command_readers.CannedQueries'>]
post_reading(**kwargs)

Loop through canned queries to run when the returned query is SENTINEL, meaning the CannedQuery generator reached the end.

Parameters
  • hooked (CannedQueries) – CannedQueries instance to run the hook on.

  • result (str) – Passed in result to potentially modify.

  • data (str) – Serial command.

Returns

the modified or unmodified serial command result

SENTINEL (object): if the hook is passed in object, and it not acting on this serial command, it will just return SENTINEL unmodified.

Return type

str

class granola.hooks.hooks.StickCannedQueries(attributes=None, include_or_exclude=SetRelationship.exclude, *args, **kwargs)

Bases: BaseHook

hooked_classes = [<class 'granola.command_readers.CannedQueries'>]
post_reading(**kwargs)

Stick on the last return query when the returned query is SENTINEL, meaning the CannedQuery generator reached the end.

Parameters
  • hooked (CannedQueries) – CannedQueries instance to run the hook on.

  • result (str) – Passed in result to potentially modify.

Returns

the modified or unmodified serial command result

Return type

str