utils
Core utility functions for codablellm.
Command = List[str]
module-attribute
A CLI command.
CommandErrorHandler = Literal['interactive', 'ignore', 'none']
module-attribute
Defines the strategies for handling errors encountered during the execution of a CLI command.
Supported Error Handlers
ignore: The CLI command error is ignored, and execution continues without interruption.none: An exception is raised immediately upon encountering the CLI error.interactive: The user is prompted to resolve the error manually, allowing for interactive handling of the issue.
JSONObject = Dict[str, JSONValue]
module-attribute
Represents a JSON object.
JSONValue = Optional[Union[str, int, float, bool, List['JSONValue'], 'JSONObject']]
module-attribute
Represents a valid JSON value
PathLike = Union[Path, str]
module-attribute
An object representing a file system path.
REBASED_DIR_ENVIRON_KEY = 'CODABLELLM_REBASED_DIR'
module-attribute
Environment variable key used to expose the rebased directory path to subprocesses.
This is especially useful when running custom build or clean commands that need to
reference the rebased project root dynamically (e.g., using shell expansion like $CODABLELLM_REBASED_DIR).
Set automatically when using the temp generation mode.
ASTEditor
A Tree-sitter AST editor.
Source code in src/codablellm/core/utils.py
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 | |
__init__(parser, source_code, ensure_parsable=True)
Initializes the AST editor with a parser and source code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
Parser
|
The |
required |
source_code
|
str
|
The source code to be edited. |
required |
ensure_parsable
|
bool
|
If |
True
|
Source code in src/codablellm/core/utils.py
edit_code(node, new_code)
Edits the source code at the specified AST node and updates the AST.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node
|
The |
required |
new_code
|
str
|
The new code to insert in place of the node's source code. |
required |
Raises:
| Type | Description |
|---|---|
TSParsingError
|
If |
Source code in src/codablellm/core/utils.py
match_and_edit(query, groups_and_replacement)
Searches the AST using a Tree-sitter query and applies code edits to matching nodes.
For each match group, replaces the matched node's code with a provided string or the result of a callable that returns the replacement string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The Tree-sitter query string to use for finding matching nodes. |
required |
groups_and_replacement
|
Dict[str, Union[str, Callable[[Node], str]]]
|
A mapping from query group names to either replacement strings
or callables that take a |
required |
Raises:
| Type | Description |
|---|---|
TSParsingError
|
If an edit introduces parsing errors and |
Source code in src/codablellm/core/utils.py
SupportsJSON
Bases: Protocol
A class that supports JSON serialization/deserialization.
Source code in src/codablellm/core/utils.py
from_json(json_obj)
classmethod
Deserializes a JSON object to this object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_obj
|
JSONObject_T
|
The JSON representation of this object. |
required |
Returns:
| Type | Description |
|---|---|
SupportsJSON_T
|
This object loaded from the JSON object. |
Source code in src/codablellm/core/utils.py
to_json()
Serializes this object to a JSON object.
Returns:
| Type | Description |
|---|---|
JSONObject_T
|
A JSON representation of the object. |
add_command_args(command, *args)
Appends additional arguments to a CLI command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Command
|
The CLI command to append. |
required |
args
|
Any
|
Additional arguments to append to the command. |
()
|
Returns:
| Type | Description |
|---|---|
Command
|
The updated command with the appended arguments. |
Source code in src/codablellm/core/utils.py
execute_command(command, error_handler='none', task=None, ctx=nullcontext(), log_level='info', print_errors=True, cwd=None)
Executes a CLI command with optional interactive error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Command
|
The CLI command to be executed. |
required |
error_handler
|
CommandErrorHandler
|
'none' | 'interactive' |
'none'
|
task
|
Optional[str]
|
Optional description for logging. |
None
|
ctx
|
AbstractContextManager[Any]
|
Context manager used to wrap the execution. |
nullcontext()
|
log_level
|
Literal['debug', 'info']
|
Log level for the task description. |
'info'
|
print_errors
|
bool
|
If True, prints output on error. |
True
|
cwd
|
Optional[PathLike]
|
Working directory to execute the command in. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The output of the command. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If the command fails and error_handler is 'none'. |
Source code in src/codablellm/core/utils.py
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
get_checkpoint_file(prefix)
Returns the checkpoint file path for the current process based on the given prefix.
The checkpoint file is stored in the system temporary directory and named using
the format: {prefix}_{pid}.json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The filename prefix for the checkpoint file. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
A |
Source code in src/codablellm/core/utils.py
get_checkpoint_files(prefix)
Retrieves all checkpoint files matching the given prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The filename prefix used to locate checkpoint files. |
required |
Returns:
| Type | Description |
|---|---|
List[Path]
|
A list of |
Source code in src/codablellm/core/utils.py
get_readable_file_size(size)
Converts number of bytes to a human readable output (i.e. bytes, KB, MB, GB, TB.)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
The number of bytes. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A human readable output of the number of bytes. |
Source code in src/codablellm/core/utils.py
is_binary(file_path)
Checks if a file is a binary file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
PathLike
|
Path to a potential binary file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the file is a binary. |
Source code in src/codablellm/core/utils.py
iter_queue(queue)
Iterates over all items in a queue until it is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queue
|
Queue[T]
|
A |
required |
Returns:
| Type | Description |
|---|---|
None
|
A generator that yields each item from the queue. |
Source code in src/codablellm/core/utils.py
load_checkpoint_data(prefix, delete_on_load=False)
Loads checkpoint data from all checkpoint files matching the given prefix.
The function reads and aggregates JSON data from each checkpoint file and optionally deletes the checkpoint files after loading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The filename prefix used to locate checkpoint files. |
required |
delete_on_load
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
List[JSONObject]
|
A list of JSON objects aggregated from all matching checkpoint files. |
Source code in src/codablellm/core/utils.py
requires_extra(extra, feature, module)
Decorator that enforces the presence of an optional dependency (extra) before executing a function.
If the required module is not installed, raises an ExtraNotInstalled error with instructions
on how to install the missing extra.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
extra
|
str
|
The name of the extra (e.g., "excel") required for the feature. |
required |
feature
|
str
|
A description of the feature that requires the extra. |
required |
module
|
str
|
The module name to attempt to import. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., Any]], Callable[..., Any]]
|
A decorator that checks for the required extra before calling the function. |
Raises:
| Type | Description |
|---|---|
ExtraNotInstalled
|
If the required module is not installed. |
Source code in src/codablellm/core/utils.py
resolve_kwargs(**kwargs)
Filters out keyword arguments with None values.
Returns a dictionary containing only key-value pairs where the value is not None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Arbitrary keyword arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary of keyword arguments with |
Source code in src/codablellm/core/utils.py
save_checkpoint_file(prefix, contents)
Saves checkpoint data to a file based on the given prefix.
The contents are converted to JSON and written to a checkpoint file named
{prefix}_{pid}.json in the system temporary directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The filename prefix for the checkpoint file. |
required |
contents
|
Iterable[SupportsJSON]
|
An iterable of objects that support JSON serialization via |
required |