Styles API¶
Style registry and loading system.
Registry¶
StyleRegistry¶
pyreprint.styles.registry.StyleRegistry()
¶
Registry for named print styles.
The StyleRegistry manages a collection of named style functions that can be applied to print output. Styles can be registered programmatically or loaded from configuration files.
Example
registry = StyleRegistry() @registry.register("highlight") ... def highlight(text, **kwargs): ... return f">>> {text} <<<" styled = registry.apply("highlight", "Important") print(styled) Important <<<
Source code in pyreprint/styles/registry.py
Functions¶
apply(name, text, **kwargs)
¶
Apply a named style to text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the style. |
required |
text
|
str
|
Text to style. |
required |
**kwargs
|
Any
|
Additional arguments for the style function. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The styled text. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the style is not found. |
Source code in pyreprint/styles/registry.py
clear()
¶
get(name)
¶
Get a style function by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the style. |
required |
Returns:
| Type | Description |
|---|---|
Optional[StyleFunction]
|
The style function, or None if not found. |
has_style(name)
¶
Check if a style is registered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the style. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the style exists. |
list_styles()
¶
Get list of registered style names.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of style names. |
register(name, func=None)
¶
Register a style function.
Can be used as a decorator or called directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the style. |
required |
func
|
Optional[StyleFunction]
|
The style function (optional if used as decorator). |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[StyleFunction], StyleFunction]
|
The style function (for decorator usage). |
Example
As decorator:
@registry.register("section") ... def section_style(text, width=60, **kwargs): ... line = "=" * width ... return f"{line}\n{text}\n{line}"
Direct registration:
registry.register("simple", lambda t, **k: f"[{t}]")
Source code in pyreprint/styles/registry.py
unregister(name)
¶
Remove a registered style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the style to remove. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the style was removed, False if not found. |
Source code in pyreprint/styles/registry.py
register_style¶
pyreprint.styles.registry.register_style(name, func=None)
¶
Register a style with the global registry.
Can be used as a decorator or called directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the style. |
required |
func
|
Optional[StyleFunction]
|
The style function (optional if used as decorator). |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[StyleFunction], StyleFunction]
|
The style function (for decorator usage). |
Example
@register_style("warning") ... def warning_style(text, **kwargs): ... return f"[!] {text} [!]"
Source code in pyreprint/styles/registry.py
get_style¶
pyreprint.styles.registry.get_style(name)
¶
Get a style function from the global registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the style. |
required |
Returns:
| Type | Description |
|---|---|
Optional[StyleFunction]
|
The style function, or None if not found. |
list_styles¶
pyreprint.styles.registry.list_styles()
¶
List all registered styles in the global registry.
Returns:
| Type | Description |
|---|---|
List[str]
|
List of style names. |
apply_style¶
pyreprint.styles.registry.apply_style(name, text, **kwargs)
¶
Apply a named style from the global registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the style. |
required |
text
|
str
|
Text to style. |
required |
**kwargs
|
Any
|
Additional arguments for the style function. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The styled text. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the style is not found. |
Source code in pyreprint/styles/registry.py
Loading¶
load_styles_from_file¶
pyreprint.styles.loader.load_styles_from_file(path, register=True)
¶
Load styles from a YAML or TOML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to the configuration file. |
required |
register
|
bool
|
Whether to register styles in the global registry. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, StyleFunction]
|
Dictionary of style name to style function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If file format is not supported. |
FileNotFoundError
|
If the file does not exist. |
Example
styles = load_styles_from_file("my_styles.yaml")
Now you can use: reprint("Hello", style="my_custom_style")¶
Source code in pyreprint/styles/loader.py
load_styles_from_yaml¶
pyreprint.styles.loader.load_styles_from_yaml(yaml_content)
¶
Load styles from YAML content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaml_content
|
str
|
YAML string containing style definitions. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, StyleFunction]
|
Dictionary of style name to style function. |
Example
yaml_content = ''' ... styles: ... divider: ... before: ... char: "-" ... width: 40 ... after: ... char: "-" ... width: 40 ... ''' styles = load_styles_from_yaml(yaml_content)
Source code in pyreprint/styles/loader.py
load_styles_from_dict¶
pyreprint.styles.loader.load_styles_from_dict(config, register=True)
¶
Load styles from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Dict[str, Dict[str, Any]]
|
Dictionary of style name to style configuration. |
required |
register
|
bool
|
Whether to register styles in the global registry. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, StyleFunction]
|
Dictionary of style name to style function. |
Example
config = { ... "highlight": { ... "prefix": ">>> ", ... "suffix": " <<<", ... } ... } styles = load_styles_from_dict(config)
Source code in pyreprint/styles/loader.py
Built-in Styles¶
The following styles are available by default:
| Style | Description |
|---|---|
section |
Lines above and below text |
header |
Text with underline |
divider |
Softer divider with dashes |
box |
Text surrounded by border |
banner |
Full-width centered text |
title |
Prominent title format |
quote |
Blockquote style with prefix |
bullet |
Bullet point format |
numbered |
Numbered item format |
highlight |
Attention markers |
warning |
Warning message format |
error |
Error message format |
success |
Success message format |
info |
Info message format |