shellbot.context module¶
-
class
shellbot.context.
Context
(settings=None, filter=None)[source]¶ Bases:
object
Stores settings across multiple independent processing units
This is a key-value store, that supports concurrency across multiple processes.
-
apply
(settings={})[source]¶ Applies multiple settings at once
Parameters: settings (dict) – variables to be added to this context
-
check
(key, default=None, is_mandatory=False, validate=None, filter=False)[source]¶ Checks some settings
Parameters: - key – the key that has to be checked
- default (str) – the default value if no statement can be found
- is_mandatory (bool) – raise an exception if keys are not found
- validate (callable) – a function called to validate values before the import
- filter (bool) – look at the content, and change it eventually
Example:
context = Context({ 'spark': { 'room': 'My preferred room', 'participants': ['alan.droit@azerty.org', 'bob.nard@support.tv'], 'team': 'Anchor team', 'token': 'hkNWEtMJNkODk3ZDZLOGQ0OVGlZWU1NmYtyY>', 'webhook': "http://73a1e282.ngrok.io", 'weird_token', '$WEIRD_TOKEN', } }) context.check('spark.room', is_mandatory=True) context.check('spark.team') context.check('spark.weird_token', filter=True)
When a default value is provided, it is used to initialize properly a missing key:
context.check('general.switch', 'on')
Another usage is to ensure that a key has been set:
context.check('spark.room', is_mandatory=True)
Additional control can be added with the validation function:
context.check('general.switch', validate=lambda x: x in ('on', 'off'))
When filter is True, if the value is a string starting with ‘$’, then a variable with the same name is loaded from the environment:
>>>token=context.check('spark.weird_token', filter=True) >>>assert token == os.environ.get('WEIRD_TOKEN') True
The default filter can be changed at the creation of a context:
>>>context=Context(filter=lambda x : x + '...')
This function raises
KeyError
if a mandatory key is absent. If a validation function is provided, then aValueError
can be raised as well in some situations.
-
get
(key, default=None)[source]¶ Retrieves the value of one configurationkey
Parameters: - key (str) – name of the value
- default (any serializable type is accepted) – default value
Returns: the actual value, or the default value, or None
Example:
message = context.get('bot.on_start')
This function is safe on multiprocessing and multithreading.
-
has
(prefix)[source]¶ Checks the presence of some prefix
Parameters: prefix (str) – key prefix to be checked Returns: True if one or more key start with the prefix, else False This function looks at keys actually used in this context, and return True if prefix is found. Else it returns False.
Example:
context = Context(settings={'space': {'title', 'a title'}}) >>>context.has('space') True >>>context.has('space.title') True >>>context.has('spark') False
-
is_empty
¶ Does the context store something?
Returns: True if there at least one value, False otherwise
-