serialization
Serializer
class to allow dumping and loading Widget
-s. This class
uses Widget.serialize
for each widget.
Serializer
A class to facilitate loading & dumping widgets.
By default it is only aware of pytermgui objects, however
if needed it can be made aware of custom widgets using
Serializer.register
.
It can dump any widget type, but can only load ones it knows.
All styles (except for char styles) are converted to markup
during the dump process. This is done to make the end-result
more readable, as well as more universally usable. As a result,
all widgets use markup_style
for their affected styles.
Source code in pytermgui/serialization.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 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 107 108 109 110 111 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
__init__()
Sets up known widgets.
Source code in pytermgui/serialization.py
36 37 38 39 40 41 42 43 |
|
bind(name, method)
Binds a name to a method.
These method callables are substituted into all fields that follow
the method:<method_name>
syntax. If method_name
is not bound,
an exception will be raised during loading.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the method, as referenced in the loaded files. |
required |
method |
Callable[..., Any]
|
The callable to bind. |
required |
Source code in pytermgui/serialization.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
dump_to_dict(obj)
staticmethod
Dump widget to a dict.
This is an alias for obj.serialize
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Widget
|
The widget to dump. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
|
Source code in pytermgui/serialization.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
from_dict(data, widget_type=None)
Loads a widget from a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict[str, Any]
|
The data to load from. |
required |
widget_type |
str | None
|
Substitute for when data has no |
None
|
Returns:
Type | Description |
---|---|
Widget
|
A widget from the given data. |
Source code in pytermgui/serialization.py
115 116 117 118 119 120 121 122 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 155 156 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
from_file(file)
Loads widget from a file object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
IO[str]
|
An IO object. |
required |
Returns:
Type | Description |
---|---|
Widget
|
The loaded widget. |
Source code in pytermgui/serialization.py
198 199 200 201 202 203 204 205 206 207 208 |
|
get_widgets()
staticmethod
Gets all widgets from the module.
Source code in pytermgui/serialization.py
45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
register(cls)
Makes object aware of a custom widget class, so it can be serialized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type[Widget]
|
The widget type to register. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
The object is not a type. |
Source code in pytermgui/serialization.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
register_box(name, box)
Registers a new Box type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the box. |
required |
box |
widgets.boxes.Box
|
The box instance. |
required |
Source code in pytermgui/serialization.py
74 75 76 77 78 79 80 81 82 |
|
to_file(obj, file, **json_args)
Dumps widget to a file object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Widget
|
The widget to dump. |
required |
file |
IO[str]
|
The file object it gets written to. |
required |
**json_args |
dict[str, Any]
|
Arguments passed to |
{}
|
Source code in pytermgui/serialization.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|