htmy.core
ContextAware
Base class with utilities for safe context use.
Features:
- Register subclass instance in a context.
- Load subclass instance from context.
- Wrap components within a subclass instance context.
Subclass instance registration:
Direct subclasses are considered the "base context type". Subclass instances are registered in contexts under their own type and also under their "base context type".
Example:
class ContextDataDefinition(ContextAware):
# This is the "base context type", instances of this class and its subclasses
# will always be registered under this type.
...
class ContextDataImplementation(ContextDataDefinition):
# Instances of this class will be registered under `ContextDataDefinition` (the
# "base context type") and also under this type.
...
class SpecializedContextDataImplementation(ContextDataImplementation):
# Instances of this class will be registered under `ContextDataDefinition` (the
# "base context type") and also under this type, but they will not be registered
# under `ContextDataImplementation`, since that's not the base context type.
...
Source code in htmy/core.py
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 | |
from_context(context, default=_MISSING)
classmethod
Looks up an instance of this class from the given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Context
|
The context the instance should be loaded from. |
required |
default
|
Self | None
|
The default to use if no instance is found in the context. If omitted,
a |
_MISSING
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If no instance was found and no |
TypeError
|
If the value in the context is not an instance of this class. |
Source code in htmy/core.py
in_context(*children)
Creates a context provider component that renders the given children using this instance in its context.
to_context()
Creates a context with this instance in it.
See the context registration rules in the class documentation for more information.
Source code in htmy/core.py
Formatter
Bases: ContextAware
The default, context-aware property name and value formatter.
The formatter supports both primitive and (many) complex values, such as lists, dictionaries, tuples, and sets. Complex values are JSON-serialized by default.
Important: the default implementation looks up the formatter for a given value by checking
its type, but it doesn't do this check with the base classes of the encountered type. For
example the formatter will know how to format datetime object, but it won't know how to
format a MyCustomDatetime(datetime) instance.
One reason for this is efficiency: always checking the base classes of every single value is a
lot of unnecessary calculation. The other reason is customizability: this way you could use
subclassing for formatter selection, e.g. with LocaleDatetime(datetime)-like classes.
Property name and value formatters may raise a SkipProperty error if a property should be skipped.
Source code in htmy/core.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
__init__(*, default_formatter=str, name_formatter=None)
Initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_formatter
|
Callable[[Any], str]
|
The default property value formatter to use if no formatter could be found for a given value. |
str
|
name_formatter
|
Callable[[str], str] | None
|
Optional property name formatter (for replacing the default name formatter). |
None
|
Source code in htmy/core.py
add(key, formatter)
format(name, value)
Formats the given name-value pair.
Returns an empty string if the property name or value should be skipped.
See SkipProperty for more information.
Source code in htmy/core.py
format_name(name)
format_value(value)
Formats the given value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The property value to format. |
required |
Raises:
| Type | Description |
|---|---|
SkipProperty
|
If the property should be skipped. |
Source code in htmy/core.py
Fragment
Fragment utility component that simply wraps some children components.
Source code in htmy/core.py
__init__(*children)
Initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*children
|
ComponentType
|
The wrapped children. |
()
|
SafeStr
Bases: Text
String subclass whose instances shouldn't get escaped during rendering.
Note: any operation on SafeStr instances will result in plain str instances which
will be rendered normally. Make sure the str to SafeStr conversion (SafeStr(my_string))
takes when there's no string operation afterwards.
Source code in htmy/core.py
SkipProperty
Bases: Exception
Exception raised by property formatters if the property should be skipped.
Source code in htmy/core.py
format_property(_)
classmethod
Property formatter that raises a SkipProperty error regardless of the received value.
Text
WithContext
Bases: Fragment
A simple, static context provider component.
Source code in htmy/core.py
__init__(*children, context)
Initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*children
|
ComponentType
|
The children components to wrap in the given context. |
()
|
context
|
Context
|
The context to make available to children components. |
required |
Source code in htmy/core.py
XBool
Bases: Enum
Utility for the valid formatting of boolean XML (and HTML) attributes.
See this article for more information: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#boolean_attributes
Source code in htmy/core.py
format()
xml_format_string(value)
Escapes <, >, and & characters in the given string, unless it's a SafeStr.