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
from_context(context, default=None)
classmethod
Looks up an instance of this class from the given contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
|
The context the instance should be loaded from. |
required |
default
|
|
The default to use if no instance was found in the context. |
None
|
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:
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
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 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 | |
__init__(*, default_formatter=str, name_formatter=None)
Initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_formatter
|
|
The default property value formatter to use if no formatter could be found for a given value. |
|
name_formatter
|
|
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)
Formats the given name.
Raises:
| Type | Description |
|---|---|
|
If the property should be skipped. |
format_value(value)
Formats the given value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
|
The property value to format. |
required |
Raises:
| Type | Description |
|---|---|
|
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
|
|
The wrapped children. |
()
|
SafeStr
Bases:
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 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:
A simple, static context provider component.
Source code in htmy/core.py
__init__(*children, context)
Initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*children
|
|
The children components to wrap in the given context. |
()
|
context
|
|
The context to make available to children components. |
required |
Source code in htmy/core.py
XBool
Bases:
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.