evennia.utils.optionhandler¶
-
class
evennia.utils.optionhandler.
InMemorySaveHandler
[源代码]¶ 基类:
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)[源代码]¶ 基类:
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)[源代码]¶ Initialize an OptionHandler.
- 参数
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.
提示
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)[源代码]¶ Retrieves an Option stored in the handler. Will load it if it doesn’t exist.
- 参数
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.
- 返回
option_value (any or Option) – An option value the Option itself.
- 引发
KeyError – If option is not defined.
-
set
(key, value, **kwargs) → BaseOption[源代码]¶ Change an individual option.
- 参数
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.
- 返回
BaseOption –
- The matched object. Its new value can be accessed with
op.value or op.display().
-