Output¶
OperationResult¶
The atomic result type returned by interface methods. Carries success state, an item identifier, a message, optional data payload, and an error code.
pyclif.OperationResult
dataclass
¶
Outcome of a single interface action.
Interface methods return this instead of rising for expected business failures. Exceptions are reserved for programming errors (broken invariant, missing template, corrupt state).
Attributes:
| Name | Type | Description |
|---|---|---|
success |
bool
|
Whether the action succeeded. |
item |
str
|
Human-readable identifier (file path, resource name, …). |
data |
Any
|
Optional payload attached to the result. |
message |
str
|
Human-readable description of the outcome. |
error_code |
int
|
Non-zero on failure. |
Source code in src/pyclif/core/output/responses.py
ok(item, message='', data=None)
classmethod
¶
Create a successful result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
str
|
Human-readable identifier for the operated resource. |
required |
message
|
str
|
Human-readable description of what happened. |
''
|
data
|
Any
|
Optional domain payload. |
None
|
Returns:
| Type | Description |
|---|---|
OperationResult
|
A successful OperationResult with error_code 0. |
Source code in src/pyclif/core/output/responses.py
error(item, message, error_code=1)
classmethod
¶
Create a failed result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
str
|
Human-readable identifier for the operated resource. |
required |
message
|
str
|
Description of the failure. |
required |
error_code
|
int
|
Exit code for this failure (default 1). |
1
|
Returns:
| Type | Description |
|---|---|
OperationResult
|
A failed OperationResult with the given error_code. |
Source code in src/pyclif/core/output/responses.py
Response¶
The standard return type for all pyclif commands. Carries success state, a human-readable message, optional structured data, and an error code.
pyclif.Response
dataclass
¶
Represents a CLI command response with structured output support.
Attributes:
| Name | Type | Description |
|---|---|---|
success |
bool
|
Indicates whether the response is successful. |
message |
str
|
The message associated with the response. |
data |
Any
|
Additional data associated with the response. |
error_code |
int | None
|
The error code associated with the response. |
renderer |
BaseRenderer | None
|
Renderer controlling all output formats for this response. |
Source code in src/pyclif/core/output/responses.py
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 | |
to_dict()
¶
Return a dictionary representation of the object.
Only includes fields whose values differ from their defaults.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary mapping attributes names to their values. |
Source code in src/pyclif/core/output/responses.py
to_json()
¶
Return a JSON-serializable dictionary of the response.
Non-serializable fields (callbacks) are excluded from the output.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing the serializable attributes. |
Source code in src/pyclif/core/output/responses.py
from_results(results, message='', success_message='', failure_message='', renderer=None)
classmethod
¶
Build a Response from a list of OperationResult.
Aggregates a batch of interface results into a single Response. success is True only if every result succeeded. error_code is taken from the first failed result, or 0 if all passed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
list[OperationResult]
|
Outcomes returned by the interface layer. |
required |
message
|
str
|
Fixed message used regardless of the outcome. When omitted, success_message / failure_message are used, or a default summary is generated from the result counts. |
''
|
success_message
|
str
|
Message used when all results succeeded. |
''
|
failure_message
|
str
|
Message used when at least one result failed. |
''
|
renderer
|
BaseRenderer | None
|
Renderer instance controlling all output formats. |
None
|
Returns:
| Type | Description |
|---|---|
Response
|
An aggregated Response reflecting the overall outcome. |
Source code in src/pyclif/core/output/responses.py
from_stream(stream, renderer)
classmethod
¶
Build a Response from a generator of OperationResult.
The generator is stored without being consumed. The framework materializes it at dispatch time: for rich output the Live context drives iteration via renderer hooks; for all other formats OutputFormatMixin calls _materialise_stream() before dispatch.
success, message, and error_code are left blank — they are re-evaluated by the framework after the stream is consumed, using renderer.get_success_message() / get_failure_message().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
Iterator[OperationResult]
|
Generator yielding OperationResult items one by one. |
required |
renderer
|
BaseRenderer
|
Renderer instance — required, a stream with no renderer has no output contract. |
required |
Returns:
| Type | Description |
|---|---|
Response
|
An incomplete Response carrying the stream and renderer. |
Source code in src/pyclif/core/output/responses.py
CliTable¶
Wrapper around Rich Table for consistent tabular output.
pyclif.CliTable
¶
Create and manage tables for CLI output with custom styling.
This class wraps the Rich library's Table to provide convenient methods for adding columns and rows with automatic formatting.
Source code in src/pyclif/core/output/tables.py
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 | |
__init__(fields, rows, table_style=None)
¶
Initialize the CLI table with columns and rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
dict
|
Dictionary of field names to CliTableColumn objects. |
required |
rows
|
list | dict
|
Single dictionary or list of dictionaries representing rows. |
required |
table_style
|
dict | None
|
Optional dictionary to override default table styling. |
None
|
Source code in src/pyclif/core/output/tables.py
table()
¶
Return the internal table object.
Returns:
| Name | Type | Description |
|---|---|---|
Table |
Table
|
The rich Table instance. |
__rich__()
¶
Return the table for rich rendering.
Returns:
| Type | Description |
|---|---|
Table | str
|
The rich Table object or a message if no data exists. |
update_columns(fields)
¶
Add columns to the table from field definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
dict
|
Dictionary of field names to CliTableColumn objects. |
required |
Source code in src/pyclif/core/output/tables.py
update_rows(fields, rows)
¶
Add rows to the table.
Accepts either a single dictionary or list of dictionaries representing rows. Each row is processed to extract values matching field definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
dict
|
Dictionary of field names to column definitions. |
required |
rows
|
list | dict
|
Single dictionary or list of dictionaries for rows. |
required |
Source code in src/pyclif/core/output/tables.py
__rich_field__(field)
staticmethod
¶
Format a field value for rich table output.
Converts booleans to emoji representations, integers to strings, and None values to "N/A".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
Any
|
The field value to format. |
required |
Returns:
| Type | Description |
|---|---|
str | Any
|
Formatted value suitable for table display. |
Source code in src/pyclif/core/output/tables.py
CliTableColumn¶
Column definition for CliTable.
pyclif.CliTableColumn
¶
Bases: Column
Column definition for CLI tables with serialization support.
Source code in src/pyclif/core/output/tables.py
to_dict()
¶
Convert the column object to a dictionary.
Returns a dictionary of all non-private fields and their values, excluding fields that have default values.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary representation of the column. |
Source code in src/pyclif/core/output/tables.py
ExceptionTable¶
Renders an exception as a Rich table. Used internally by the response formatter but available for direct use in error handlers.
pyclif.ExceptionTable
¶
Bases: CliTable
Specialized table for displaying exception information.
Displays error code, message, and traceback with red styling for exception visualization.
Source code in src/pyclif/core/output/tables.py
__init__(columns)
¶
Initialize the exception table with error data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
dict
|
Dictionary containing error_code, message, and data keys. |
required |
Source code in src/pyclif/core/output/tables.py
Output formats¶
| Format | Output | Filterable |
|---|---|---|
table |
Rich table — default format | no |
rich |
Live / panels / markdown | no |
text |
Plain text: response.message only |
no |
json |
Syntax-highlighted JSON — always valid JSON | yes |
yaml |
Syntax-highlighted YAML — always valid YAML | yes |
raw |
Compact JSON, no highlighting — machine-readable | yes |
table is the default format.
--output-filter accepts a dotted key path (results.0.id, article.title, message).
Numeric segments are treated as list indices. Resolution: data["data"] first, then top-level.
raw— prints the extracted value as-is.running, not"running". Best for scripting.json— re-serializes the extracted value as valid JSON. Always outputs valid JSON.yaml— re-serializes the extracted value as valid YAML. Always outputs valid YAML.
BaseRenderer¶
Declarative base class for all pyclif output renderers. Subclass and set class
attributes (fields, columns, rich_title, success_message, failure_message)
to control every output format without overriding methods.
Key hooks:
text(response)— returnsresponse.messageas plain text (used by--output-format text)raw(response)— returns a serialized dict for machine-readable output (used by--output-format raw)serialize(response)— returns a JSON-serializable dict (used byjson,yaml, andraw)
pyclif.BaseRenderer
¶
Declarative base class for pyclif output renderers.
Subclass and declare class attributes to control every output format. Override individual hooks for custom behavior. Override the full method only as a last resort.
Class attributes
fields: Field names included in JSON/YAML serialization. Empty means all fields via the standard Response.to_json() fallback. columns: Column names for table output. Falls back to fields when empty. rich_title: Panel title used by the default rich() and table() display. success_message: Static success message returned by get_success_message(). failure_message: Static failure message returned by get_failure_message().
Implementation note — class-level lists: fields and columns are ClassVar. Subclasses override them as plain class attributes (never mutated at runtime). get_fields() and get_columns() always return a copy, so callers cannot accidentally mutate the class-level list.
Source code in src/pyclif/core/output/renderer.py
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 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 | |
get_fields()
¶
get_columns()
¶
serialize(response)
¶
Return a JSON-serializable dict filtered to self.fields.
When fields are empty, delegates to response.to_json() for full serialization with standard exclusions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
The command response to serialize. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict suitable for JSON/YAML output. |
Source code in src/pyclif/core/output/renderer.py
table(response)
¶
Build a CliTable from response.data["results"] using self.columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
The command response carrying the result list. |
required |
Returns:
| Type | Description |
|---|---|
CliTable
|
A CliTable instance is ready for console.print(). |
Source code in src/pyclif/core/output/renderer.py
text(response)
¶
Return the response message as plain text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
The command response. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The response message string. |
raw(response)
¶
Return a serialized dict for machine-readable output.
Defaults to serialize() output. Override for a custom raw representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
The command response. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Serialized dict suitable for compact JSON output. |
Source code in src/pyclif/core/output/renderer.py
rich(response, console)
¶
Display a panel with the response message.
Override for panels, rules, markdown, or any static Rich display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
The command response to display. |
required |
console
|
Console
|
The Rich console to print to. |
required |
Source code in src/pyclif/core/output/renderer.py
rich_setup()
¶
Return the initial renderable for the Live context.
Called once before iteration starts. Override to create and store stateful Rich objects (Progress, Layout, etc.) as instance attributes so rich_on_item() can mutate them.
Returns:
| Type | Description |
|---|---|
Any
|
A Rich renderable to wrap in Live(). |
Source code in src/pyclif/core/output/renderer.py
rich_on_item(result, all_so_far)
¶
Called after each streamed item inside the Live context.
Override to mutate the Rich objects created in rich_setup().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OperationResult
|
The latest OperationResult. |
required |
all_so_far
|
list
|
All results received so far, including a result. |
required |
Source code in src/pyclif/core/output/renderer.py
rich_summary(response, console)
¶
Called after all items are processed and the Live context is closed.
Defaults to the static rich() display. Override for a custom summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
The fully materialized response with all results. |
required |
console
|
Console
|
The Rich console to print to. |
required |
Source code in src/pyclif/core/output/renderer.py
get_success_message(results)
¶
Return the success message for a completed batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
list
|
All OperationResult items from the batch. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Human-readable success message. |
Source code in src/pyclif/core/output/renderer.py
get_failure_message(results)
¶
Return the failure message for a partially or fully failed batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
list
|
All OperationResult items from the batch. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Human-readable failure message with failure count. |
Source code in src/pyclif/core/output/renderer.py
ResponseRenderer¶
Protocol that all renderer implementations must satisfy. Implement this Protocol
directly only when inheriting BaseRenderer is not appropriate.
pyclif.ResponseRenderer
¶
Bases: Protocol
Protocol for renderer implementations.
Renderers are the single source of truth for all output formats of a command. Implement this Protocol directly only when inheriting BaseRenderer is not appropriate. All methods called by the framework must be present.