typerdrive Base Modules
typerdrive.config
Provide controls for the configuration of typerdrive
itself.
Note
This is distinct from the functionality provided in the settings
module.
The settings
feature is for configuring the app built with typerdrive
. Usually, settings reflect behavior
specific to the app.
The config
feature is for configuring typerdrive
internal behavior.
The config
features should never be exposed to end-users or used to manage behavior of the app built on top of
typerdrive
.
Attributes
Classes
TyperdriveConfig
Bases: BaseModel
Define the configurable attributes of typerdrive
.
Source code in src/typerdrive/config.py
Attributes
app_name
class-attribute
instance-attribute
The name of the app built on typerdrive
. Use this setting if you want to override the default behavior of using
sys.argv[0]
.
console_ascii_only
class-attribute
instance-attribute
If set, only use ascii characters in the console.
console_width
class-attribute
instance-attribute
Set the width of the console for rich.console
. Will override automatic resizing behavior.
log_file_compression
class-attribute
instance-attribute
Define whether rotated logs should be compressed. See the
loguru
docs
for more information.
log_file_name
class-attribute
instance-attribute
The name of the file where logs will be written. This file will be created in the log_dir
directory.
log_file_retention
class-attribute
instance-attribute
Define the how soon logs should be deleted. See the
loguru
docs
for more information.
log_file_rotation
class-attribute
instance-attribute
Define the how often logs should be rotated. See the
loguru
docs
for more information.
Functions
get_typerdrive_config
set_typerdrive_config
Update the TyperdriveConfig
with the provided key/values.
Source code in src/typerdrive/config.py
typerdrive.constants
Provide some constants for the project.
Classes
ExitCode
Bases: IntEnum
Maps exit codes for the application.
Source code in src/typerdrive/constants.py
Validation
Bases: Flag
Defines when validation of settings should happen in the @attach_settings
context manager.
Source code in src/typerdrive/constants.py
typerdrive.context
Provide tools for working with the TyperContext
and the TyperdriveContext
.
Classes
TyperdriveContext
dataclass
Define the typerdrive
context that is attached to the Typer.Context
as obj
.
Source code in src/typerdrive/context.py
Attributes
Functions
Functions
from_context
Retrieve a TyperdriveManager
from the TyperdriveContext
at the given name
.
Source code in src/typerdrive/context.py
get_user_context
Retrieve the user context from the typer.Context
.
If a user context has not been established yet, it will be initialized.
Read the 'click' docs to learn more.
Source code in src/typerdrive/context.py
to_context
Attach a TyperdriveManager
object to the TyperdriveContext
at the given name
.
Source code in src/typerdrive/context.py
typerdrive.dirs
Provide methods for working with directories.
Classes
DirInfo
dataclass
Describes directory info needed for the render_directory()
function.
Source code in src/typerdrive/dirs.py
Functions
clear_directory
Delete all files and directories in the directory at the given path.
If the target path is not a directory, an exception will be raised. If the target path does not exist, an exception will be raised.
Source code in src/typerdrive/dirs.py
is_child
Return true if the given path is a child of the given parent.
Source code in src/typerdrive/dirs.py
render_directory
Render a visualization of the directory at the given path using rich.tree
.
This is a recursive function. If calling this function, you should call it with is_root=True
.
Source code in src/typerdrive/dirs.py
show_directory
Print the visualization of a target directory retrieved with render_directory()
using terminal_message
.
Source code in src/typerdrive/dirs.py
typerdrive.env
Provide utility functions for working with the environment.
Functions
tweak_env
Temporarily alter environment variables for the lifetime of the context manager.
All items in kwargs
will be set in the environment.
If there was exiting value in the given key, it will be restored after the context manager exits.
If there was no existing value in the given key, it will have no value after the context manager exits.
Source code in src/typerdrive/env.py
typerdrive.exceptions
Provide exception types for typerdrive
.
All exception types derived from TyperdriveError
will, by default, be handled by the @handle_errors
decorator.
Classes
BuildCommandError
Bases: TyperdriveError
Indicates that there was a problem building a command with typer-repyt
.
Source code in src/typerdrive/exceptions.py
ContextError
Bases: TyperdriveError
Indicates that there was a problem interacting with the Typer.Context
.
Source code in src/typerdrive/exceptions.py
DisplayError
Bases: TyperdriveError
Indicates that there was a problem displaying data to the user.
Source code in src/typerdrive/exceptions.py
TyperdriveError
Bases: Buzz
Base exception class for all typerdrive
errors.
Derives from the buzz.Buzz
base class. See the
py-buzz
docs to learn more.
Source code in src/typerdrive/exceptions.py
Attributes
details
class-attribute
instance-attribute
Any additional details that should be included in the error as well
exit_code
class-attribute
instance-attribute
The exit code that should be used after handling the error and terminating the app
footer
class-attribute
instance-attribute
The footer to show on user-facing messages when this error is handled by @handle_errors()
subject
class-attribute
instance-attribute
The subject to show on user-facing messages when this error is handled by @handle_errors()
Functions
__init__
__init__(
*args: Any,
subject: str | None = None,
footer: str | None = None,
details: Any | None = None,
exit_code: ExitCode | None = None,
**kwargs: Any,
)
Source code in src/typerdrive/exceptions.py
typerdrive.format
Provide methods for formatted output for the user.
Classes
Functions
pretty_field_info
Format a pydantic FieldInfo.
Source code in src/typerdrive/format.py
pretty_model
Format a pydantic model class.
Source code in src/typerdrive/format.py
simple_message
simple_message(
message: str,
indent: bool = False,
markdown: bool = False,
error: bool = False,
to_clipboard: bool = False,
)
Show a simple, non-decorated message to the user.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to show to the user; May not be a |
required |
indent
|
bool
|
If |
False
|
markdown
|
bool
|
If |
False
|
error
|
bool
|
If |
False
|
to_clipboard
|
bool
|
If |
False
|
Source code in src/typerdrive/format.py
strip_rich_style
Strip all rich styling (color, emphasis, etc.) from the text.
terminal_message
terminal_message(
message: RenderableType,
subject: str | None = None,
subject_align: Literal[
"left", "right", "center"
] = "left",
subject_color: str = "green",
footer: str | None = None,
footer_align: Literal[
"left", "right", "center"
] = "left",
indent: bool = True,
markdown: bool = False,
error: bool = False,
to_clipboard: bool = False,
)
Show a formatted message to the user in a rich
Panel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
RenderableType
|
The message to show to the user. May be a string or any other |
required |
subject
|
str | None
|
An optional heading for the panel to show to the user |
None
|
subject_align
|
Literal['left', 'right', 'center']
|
The alignment for the |
'left'
|
subject_color
|
str
|
The color to render the |
'green'
|
footer
|
str | None
|
An optional footer to show to the user. If |
None
|
footer_align
|
Literal['left', 'right', 'center']
|
The alignment for the |
'left'
|
indent
|
bool
|
If |
True
|
markdown
|
bool
|
If |
False
|
error
|
bool
|
If |
False
|
to_clipboard
|
bool
|
If |
False
|
Source code in src/typerdrive/format.py
typerdrive.handle_errors
Provide an error handler that can be attached to a command through a decorator.
Classes
Functions
handle_errors
handle_errors(
base_message: str,
*,
handle_exc_class: type[Exception]
| tuple[type[Exception], ...] = TyperdriveError,
ignore_exc_class: type[Exception]
| tuple[type[Exception], ...]
| None = None,
do_except: Callable[[DoExceptParams], None]
| None = None,
do_else: Callable[[], None] | None = None,
do_finally: Callable[[], None] | None = None,
unwrap_message: bool = True,
debug: bool = False,
) -> Callable[
[WrappedFunction[P, T]], WrappedFunction[P, T]
]
Handle errors raised by the decorated command function and show a user-friendly message in the terminal.
The behavior of this function is very similar to the py-buzz
handle_errors()
context manager. See the
py-buzz docs for more context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_message
|
str
|
The "base" message to be used for the error. This will be the "subject" of the message shown to the user when an error is handled. |
required |
handle_exc_class
|
type[Exception] | tuple[type[Exception], ...]
|
The exception class that will be handled. Exception types that inherit from this will also be handled as well. |
TyperdriveError
|
ignore_exc_class
|
type[Exception] | tuple[type[Exception], ...] | None
|
An exception class that will not be handled even if it inherits from the |
None
|
do_except
|
Callable[[DoExceptParams], None] | None
|
A function that will be called if an exception is handled. This is most useful for logging the details of the error. |
None
|
do_else
|
Callable[[], None] | None
|
A function that will be called if no exceptions were handled. |
None
|
do_finally
|
Callable[[], None] | None
|
A function that will always be called, even if an exception was handled. |
None
|
unwrap_message
|
bool
|
If true, "unwrap" the message so that newline characters are removed. |
True
|
debug
|
bool
|
If true, the message shown in the |
False
|
Source code in src/typerdrive/handle_errors.py
23 24 25 26 27 28 29 30 31 32 33 34 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 |
|
typerdrive.types
Provide some useful type definitions for the project.
The File*
types align with loguru file sink rotation and retention spec.
Refer to the Loguru documentation for more information:
Attributes
FileRetentionSpec
module-attribute
typerdrive.version
Provide methods for getting the project version.
Attributes
Functions
get_version
Try to get version from metadata, then fallback to pyproject.toml
, then fallback to "unknown".