evennia.utils.optionhandler¶
-
class
evennia.utils.optionhandler.
InMemorySaveHandler
[source]¶ Bases:
object
Fallback SaveHandler, implementing a minimum of the required save mechanism and storing data in memory.
-
class
evennia.utils.optionhandler.
OptionHandler
(obj, options_dict=None, savefunc=None, loadfunc=None, save_kwargs=None, load_kwargs=None)[source]¶ Bases:
object
This is a generic Option handler. Retrieve options either as properties on this handler or by using the .get method.
This is used for Account.options but it could be used by Scripts or Objects just as easily. All it needs to be provided is an options_dict.
-
__init__
(obj, options_dict=None, savefunc=None, loadfunc=None, save_kwargs=None, load_kwargs=None)[source]¶ Initialize an OptionHandler.
- Parameters
obj (object) – The object this handler sits on. This is usually a TypedObject.
options_dict (dict) – A dictionary of option keys, where the values are options. The format of those tuples is: (‘key’, “Description to show”, ‘option_type’, <default value>)
savefunc (callable) – A callable for all options to call when saving itself. It will be called as savefunc(key, value, **save_kwargs). A common one to pass would be AttributeHandler.add.
loadfunc (callable) – A callable for all options to call when loading data into itself. It will be called as loadfunc(key, default=default, **load_kwargs). A common one to pass would be AttributeHandler.get.
save_kwargs (any) – Optional extra kwargs to pass into savefunc above.
load_kwargs (any) – Optional extra kwargs to pass into loadfunc above.
Notes
Both loadfunc and savefunc must be specified. If only one is given, the other will be ignored and in-memory storage will be used.
-
get
(key, default=None, return_obj=False, raise_error=False)[source]¶ Retrieves an Option stored in the handler. Will load it if it doesn’t exist.
- Parameters
key (str) – The option key to retrieve.
default (any) – What to return if the option is defined.
return_obj (bool, optional) – If True, returns the actual option object instead of its value.
raise_error (bool, optional) – Raise Exception if key is not found in options.
- Returns
option_value (any or Option) – An option value the Option itself.
- Raises
KeyError – If option is not defined.
-
set
(key, value, **kwargs)[source]¶ Change an individual option.
- Parameters
key (str) – The key of an option that can be changed. Allows partial matching.
value (str) – The value that should be checked, coerced, and stored.:
kwargs (any, optional) – These are passed into the Option’s validation function, save function and display function and allows to customize either.
- Returns
value (any) – Value stored in option, after validation.
-