Kayaku🔗
kayaku.initialize 🔗
kayaku.initialize(specs: Dict[str, str], prettifier: Optional[Prettifier] = None, schema_generator_cls: Type[SchemaGenerator] = SchemaGenerator) -> None
初始化 Kayaku.
本函数需要最先被调用.
Parameters:
-
specs
(
Dict[str, str]
) –domain 样式 -> 路径样式
的映射. -
prettifier
(
Prettifier
) –格式化器.
-
schema_generator_cls
(
Type[SchemaGenerator]
) –JSON Schema 生成器的类.
Example
from kayaku import bootstrap, config, initialize
initialize({"{**}.connection": "./config/connection.jsonc::{**}})
@config("my_mod.config.connection")
class Connection:
account: int | None = None
"Account"
password: str | None = None
"password"
bootstrap()
以上代码将会将 Connection
类的数据存储在 ./config/connection.jsonc
文件的 ["my_mod"]["config"]
里.
Source code in kayaku/domain.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
Note
Please note that config
is routed to real config implementation at runtime.
将类型转换为 kayaku 承认的设置类。
domain
以外的参数将被直接传入 dataclasses.dataclass
进行转换。
Parameters:
-
domain
(
str
) –唯一的 用于标识类的结构位置与类别的字符串,用
.
分开。 -
init
(
bool
) –If true (the default), a
__init__()
method will be generated. -
repr
(
bool
) –If true (the default), a
__repr__()
method will be generated. -
eq
(
bool
) –If true (the default), an
__eq__()
method will be generated. -
order
(
bool
) –If specified,
__lt__()
,__le__()
,__gt__()
, and__ge__()
methods will be generated. -
unsafe_hash
(
bool
) –Force dataclass() to create a
__hash__()
method. Otherwise a__hash__()
method is generated according to how eq and frozen are set. -
frozen
(
bool
) –If specified, assigning to fields will generate an exception. This emulates read-only frozen instances.
-
match_args
(
bool
) –If true (the default is True), the
__match_args__
tuple will be created from the list of parameters to the generated__init__()
. -
kw_only
(
bool
) –If specified, then all fields will be marked as keyword-only.
-
slots
(
bool
) –If specified,
__slots__
attribute will be generated and new class will be returned instead of the original one.
Returns:
Source code in kayaku/model.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
自动 deepcopy 默认值的 dataclass.field
.
Parameters:
-
default
(
T
) –The default value for this field, which will be deepcopied on each call.
-
init
(
bool
) –If true (the default), this field is included in
__init__()
. -
repr
(
bool
) –If true (the default), this field is included in
repr()
. -
hash
(
bool | None
) –If true, this field is included in the generated
__hash__()
method. If None (the default), use the value of compare. -
compare
(
bool
) –If true (the default), this field is included in comparison methods.
-
metadata
(
Mapping[Any, Any] | None
) –A mapping which is stored but not otherwise examined by dataclass.
-
kw_only
(
bool
) –If true, this field will be marked as keyword-only. (3.10+)
Returns:
-
T(
T
) –The field object, faked to be the same type of
default
in typ checkers' eyes.
kayaku.bootstrap 🔗
kayaku.bootstrap() -> None
预处理所有 ConfigModel
并写入默认值和 JSON Schema.
建议在加载完外部模块后调用.
Source code in kayaku/domain.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
kayaku.save_all 🔗
kayaku.save_all() -> None
保存所有容器,并格式化。
可以在退出前调用本函数。
Source code in kayaku/domain.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
kayaku.create 🔗
kayaku.create(cls: Type[T], flush: bool = False) -> T
创建对应的模型。
给 flush
传入真值会强制重载。
Note
为了安全重载,我们推荐你让返回的实例存活时间尽量短。
Source code in kayaku/model.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
kayaku.save 🔗
kayaku.save(model: Union[T, Type[T]]) -> None
保存某个类的设置。 对应的 JSON Schema 也会被更新。
Source code in kayaku/model.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
kayaku.pretty.Prettifier 🔗
kayaku.pretty.Prettifier(indent: int = 4, trail_comma: bool = False, key_quote: Quote | None | Constant[False] = Quote.DOUBLE, string_quote: Quote | None = Quote.DOUBLE, unfold_single: bool = False)
容器格式化工具
Parameters:
-
indent
(
int
) –缩进数量. Defaults to 4.
-
trail_comma
(
bool
) –是否要为容器增加尾随逗号. Defaults to False.
-
key_quote
(
Quote | None | Constant[False]
) –键使用的引号风格, None 为保留, Quote.DOUBLE 为双引号, Quote.SINGLE 为单引号, False 为尽可能去除引号. Defaults to Quote.DOUBLE.
-
string_quote
(
Quote | None
) –值中字符串使用的引号风格, 解释同上. Defaults to Quote.DOUBLE.
-
unfold_single
(
bool
) –单个元素的容器是否展开. Defaults to False.
Source code in kayaku/pretty.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|