Mixins¶
Feature mixins used internally by pyclifer's group and context classes. Exposed publicly for advanced subclassing.
EventsMixin¶
Lets a group attach handlers to named lifecycle events with on() and call them with emit().
Handlers run in registration order, a plain string that is not a framework event raises
ValueError from both on() and emit(), and an exception raised by a handler propagates. See the
Lifecycle events guide.
pyclifer.EventsMixin
¶
Mixin that lets a group attach handlers to named lifecycle events.
Handlers run in registration order. An exception raised by a handler propagates to the caller of emit(); nothing is swallowed.
Source code in src/pyclifer/core/mixins/events.py
__init__(*args, **kwargs)
¶
Initialize the handler registry then chain to the next class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments for the parent class. |
()
|
**kwargs
|
Any
|
Keyword arguments for the parent class. |
{}
|
Source code in src/pyclifer/core/mixins/events.py
on(event, handler=None)
¶
Attach a handler to an event, or return a decorator when handler is omitted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
str
|
An Event object, or the plain name of a framework event. |
required |
handler
|
EventHandler | None
|
The function to call when the event is emitted. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The handler when given, otherwise a decorator that registers the |
Any
|
decorated function and returns it unchanged. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When event is a plain string that is not a framework event. |
Source code in src/pyclifer/core/mixins/events.py
emit(event, ctx)
¶
Call every handler attached to an event, in registration order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
str
|
An Event object, or the plain name of a framework event. |
required |
ctx
|
Context
|
The Click context passed to each handler. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
The values returned by the handlers, in the order they ran. |
Raises:
| Type | Description |
|---|---|
ValueError
|
When event is a plain string that is not a framework event. |
Source code in src/pyclifer/core/mixins/events.py
Event¶
A named lifecycle event. It is a string, so it compares equal to its name. The framework defines its
own events; plugins and applications create theirs with Event("myplugin.ready"), which needs no
registration.
pyclifer.Event
¶
Bases: str
A named lifecycle event.
An Event is a string, so it compares equal to its name and works as a dict key. The framework defines its own events; plugins and applications create theirs with Event("myplugin.ready") and share the object, which needs no prior registration.
Source code in src/pyclifer/core/events.py
GlobalOptionsMixin¶
Propagates options from a parent group to all child commands and subgroups at registration time.
- Options marked
is_global=Trueare added as real subcommand parameters — visible in help and forwarded to each callback. - The help follows the application. A group that prints plain help (
use_rich_help=False) switches the rich-click commands it receives to Click's help, along with everything they hold. - A group takes a propagated option only if it wants it. The callback of a subgroup receives a
propagated global option when it declares the parameter (or
**kwargs);def ops():works and simply does not see it. An option the group declares itself is always passed, and a leaf command must accept every global option, since it is forwarded to its callback. - A global option has one environment variable: the one of the root group. For an application
named
myappwith--region, every command readsMYAPP_REGION; the variable built from the path of the command (MYAPP_LEAF_REGION) is not read. The help of each command shows the variable that is read, withuse_rich_helpon or off. Anenvvar=given to the option is used instead. - A global option is parsed where it is written.
myapp --region eu leafgiveseuto the group that declares the option, not toleaf, which parses its own copy and keeps its default. Writemyapp leaf --region eufor the subcommand to receive it. This holds for arequired=Trueoption too:myapp --token T leaffails with a missing option onleaf, whilemyapp leaf --token Tworks. - Options marked
context=True(non-global) withshow_in_subcommand_help=Trueare injected as display-only copies into subcommand help under a dedicated Context Options panel. These copies haveexpose_value=Falseandrequired=Falseso they never interfere with subcommand argument parsing or callbacks. They reach commands at any depth, whatever the order in which groups and commands are registered. The panel title is drawn by the Rich help; withuse_rich_help=Falsethe option is listed with the other options of the command.
pyclifer.GlobalOptionsMixin
¶
Mixin that propagates global options to subcommands.
Source code in src/pyclifer/core/mixins/cli.py
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 | |
add_command(cmd, name=None, **kwargs)
¶
Register a subcommand and inject global and context options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
Command
|
The command to add. |
required |
name
|
str | None
|
The name to register the command with. |
None
|
**kwargs
|
Any
|
Additional arguments passed to the parent method. |
{}
|
Source code in src/pyclifer/core/mixins/cli.py
HandleResponseMixin¶
Auto-wraps commands added via command() or add_command() with returns_response
by default (opt out with handle_response=False).
handle_response has three values, on a command as well as on a group:
| Value | Meaning |
|---|---|
None (default) |
Inherit. A command takes its group's setting; a group takes its parent's once attached. |
True |
Enable. |
False |
Opt out. A group set to False protects its whole sub-tree from a parent that handles responses. |
Whichever of the sub-group and the application is declared first, a command with no explicit choice is wrapped as soon as its group is attached to an application that handles responses.
pyclifer.HandleResponseMixin
¶
Mixin that adds automatic Response dispatch to Click groups.
When handle_response_by_default is True (set via @app_group(handle_response=True)), every command registered through @group.command() or group.add_command() automatically wraps its return value: if the function returns a Response, it is printed using the output format stored in ctx.meta['pyclifer.output_format'].
The per-command handle_response kwarg always takes precedence over the group-level default.
Source code in src/pyclifer/core/mixins/response.py
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 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 | |
parse_args(ctx, args)
¶
Parse the arguments, answering a Click error in a structured format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
The context of this group. |
required |
args
|
list[str]
|
The arguments to parse. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The remaining arguments, as returned by Click. |
Source code in src/pyclifer/core/mixins/response.py
invoke(ctx)
¶
Invoke the group, answering a Click error in a structured format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
The context of this group. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The value returned by the invoked command. |
Source code in src/pyclifer/core/mixins/response.py
command(*args, handle_response=None, **kwargs)
¶
Override Click.Group.command to automatically handle Response objects.
Source code in src/pyclifer/core/mixins/response.py
add_command(cmd, name=None, **kwargs)
¶
Override Click.Group.add_command to propagate response handling.
Extra keyword arguments, such as a cloup section, are passed on to the next class in the chain.
When handle_response_by_default is True: - Sub-groups: handle_response_by_default is propagated recursively so that all already-registered leaf commands are wrapped and future registrations on those groups are also covered. - Leaf commands: the callback is wrapped with returns_response directly.
Commands whose callback carries the _PYCLIFER_RESPONSE_DECIDED flag (set by the @group.command() factory path before Click registers the command) are left untouched so that explicit handle_response=False overrides and already-wrapped functions are both respected.
Source code in src/pyclifer/core/mixins/response.py
OutputFormatMixin¶
Dispatches Response objects to the appropriate formatter (JSON / YAML / Table / Rich / Raw)
based on --output-format stored in ctx.meta.
pyclifer.OutputFormatMixin
¶
Provide methods for printing error messages and results based on a specified format.
This mixin expects the inheriting class to have 'console' and 'output_format' attributes. Every result must be a Response with a renderer attached.
Source code in src/pyclifer/core/mixins/output.py
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 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 | |
print_error_based_on_format(exception)
¶
Format and print an unhandled exception as a structured Response.
Creates a single-result Response from the exception and dispatches it through the normal renderer path using _ExceptionRenderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
Exception
|
The exception to display. |
required |
Source code in src/pyclifer/core/mixins/output.py
print_result_based_on_format(result, options=None)
¶
Print a Response using its renderer.
Streaming responses (data["stream"] present) are handled via the Live context for rich output, or materialized first for all other formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Response
|
The Response to print. Must have a renderer attached. |
required |
options
|
dict | None
|
Optional dict with the filter_value key for --output-filter support. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When result.renderer is None — a programming error. |
Source code in src/pyclifer/core/mixins/output.py
RichHelpersMixin¶
Rich console helpers available on the context: panels, rules, status spinners, prompts.
pyclifer.RichHelpersMixin
¶
Provide rich console helper methods for CLI contexts.
This mixin expects the inheriting class to have a 'console' attribute instantiated with a rich.console.Console object.
Source code in src/pyclifer/core/mixins/rich.py
rich_panel(text, title=None, border_style=None, padding=(0, 1), console_print=False, fit=False)
¶
Create a rich panel with the given formatting options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
Any
|
The text content or renderable of the panel. |
required |
title
|
str | None
|
The title of the panel. |
None
|
border_style
|
str | None
|
The border style of the panel. |
None
|
padding
|
tuple
|
The padding of the panel (top/bottom, left/right). |
(0, 1)
|
console_print
|
bool
|
If True, prints the panel directly to the console. |
False
|
fit
|
bool
|
If True, the panel fits its contents. |
False
|
Returns:
| Type | Description |
|---|---|
Panel
|
The rendered rich panel object. |
Source code in src/pyclifer/core/mixins/rich.py
display_rule(title='', style='blue')
¶
Display a horizontal rule with an optional title.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The text to display in the middle of the rule. |
''
|
style
|
str
|
The color and style of the rule. |
'blue'
|
Source code in src/pyclifer/core/mixins/rich.py
show_status(message='Processing...', spinner='dots')
¶
Return a rich status context manager for long-running tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to display next to the spinner. |
'Processing...'
|
spinner
|
str
|
The type of spinner animation to use. |
'dots'
|
Returns:
| Name | Type | Description |
|---|---|---|
Status |
Status
|
A context manager for the status. |
Source code in src/pyclifer/core/mixins/rich.py
ask_user(question, default=None, choices=None, password=False)
¶
Prompt the user for input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The prompt text to display. |
required |
default
|
str | None
|
The default value if the user presses enter. |
None
|
choices
|
list[str] | None
|
A list of valid choices to restrict input. |
None
|
password
|
bool
|
If True, hides the user's input. |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
The user's input. |
Source code in src/pyclifer/core/mixins/rich.py
ask_confirmation(question, default=False)
¶
Ask the user a yes or no confirmation question.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The prompt text to display. |
required |
default
|
bool
|
The default answer if the user presses enter. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the user confirmed, False otherwise. |