typerdrive Files modules
Listing files
Use FilesManager.list_items to list files from directories below the configured files directory:
from typerdrive.files.manager import FilesManager
manager = FilesManager()
manager.list_items("documents")
manager.list_items("documents", recursive=True)
manager.list_items("documents", recursive=True, missing_ok=True)
manager.list_items("documents", recursive=True, pattern="*.md")
manager.list_items("documents", recursive=True, pattern="**/*.md")
Listing is nonrecursive by default and returns immediate file names. Recursive listing returns storage-root-relative
keys, including files nested below the requested directory. A single optional glob pattern filters files relative to the
requested directory. Patterns support *, ?, and bracket expressions within path components; / separates path
components, and ** as a path component matches zero or more directories. Thus, *.md.j2 matches immediate files only,
while **/*.md.j2 also matches nested files. recursive=False remains nonrecursive regardless of the pattern.
Wildcards can match dot-prefixed names. missing_ok=True returns an empty list only when the requested directory is
missing. Non-directories, unreadable directories, and explicit paths through symlinks raise FilesError. Listings omit
symlink entries and never follow symlinked directories. The configured files root cannot be listed, including through
equivalent paths such as . or subdirectory/...
typerdrive.files.attach
Provide a decorator that attaches the typerdrive files to a typer command function.
Attributes
Classes
Functions:
attach_files
Attach the typerdrive files to the decorated typer command function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show
|
bool
|
If set, show the files directory after the function runs. |
False
|
Source code in src/typerdrive/files/attach.py
get_files_manager
Retrieve the FilesManager from the TyperdriveContext.
Source code in src/typerdrive/files/attach.py
typerdrive.files.commands
Provide commands that can be added to a typer app to interact with files.
Classes
Functions:
add_files_subcommand
Add all files subcommands to the given app.
Source code in src/typerdrive/files/commands.py
add_show
typerdrive.files.exceptions
Provide exceptions specific to the files feature of typerdrive.
Classes
FilesClearError
Bases: FilesError
Indicate that there was a problem deleting a file.
Source code in src/typerdrive/files/exceptions.py
FilesError
Bases: TyperdriveError
The base exception for files errors.
Source code in src/typerdrive/files/exceptions.py
FilesInitError
Bases: FilesError
Indicate that there was a problem initializing the files storage.
Source code in src/typerdrive/files/exceptions.py
FilesLoadError
Bases: FilesError
Indicate that there was a problem loading a file.
Source code in src/typerdrive/files/exceptions.py
FilesStoreError
Bases: FilesError
Indicate that there was a problem storing a file.
Source code in src/typerdrive/files/exceptions.py
typerdrive.files.manager
Provide a class for managing the typerdrive files feature.
Classes
FilesManager
Manage the typerdrive files feature.
This manager provides a simple key-value file storage system where files are stored in a directory structure. Files can be stored as bytes, text, or JSON and retrieved the same way.
Source code in src/typerdrive/files/manager.py
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 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 | |
Attributes
files_dir
instance-attribute
The directory where files are stored.
Methods:
__init__
delete
Delete a file at the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The file key where the file should be deleted |
required |
Source code in src/typerdrive/files/manager.py
list_items
list_items(
path: Path | str,
recursive: bool = False,
missing_ok: bool = False,
pattern: str | None = None,
) -> list[str]
List files in a directory below the configured files directory.
Nonrecursive listing returns immediate file names. Recursive listing returns keys relative to the configured files directory, including files nested below the requested directory. Symlink entries are omitted and never traversed. The configured files directory itself cannot be listed.
A glob pattern filters files relative to the requested directory. Patterns support *, ?, and bracket
expressions within path components, with / as the path separator. A ** path component matches zero or more
directories, so *.md.j2 matches only immediate files while **/*.md.j2 also matches nested files. Wildcards
can match dot-prefixed names. Only one pattern is supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The directory key to list |
required |
recursive
|
bool
|
If set, include files from nested directories |
False
|
missing_ok
|
bool
|
If set, return an empty list when the requested directory is missing |
False
|
pattern
|
str | None
|
If set, include only files matching this glob pattern |
None
|
Raises:
| Type | Description |
|---|---|
FilesError
|
If the requested directory is missing and missing_ok is false, is not a directory, is unreadable, is the configured root, or contains a symlink component |
Source code in src/typerdrive/files/manager.py
load_bytes
Load binary data from a file at the given key.
If there is no file at the given key, an exception will be raised. If there is an error reading the file, an exception will be raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The file key where the data should be loaded from |
required |
Source code in src/typerdrive/files/manager.py
load_json
Load a dictionary from a JSON file at the given key.
The file at the given key must be valid JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The file key where the JSON should be loaded from |
required |
Source code in src/typerdrive/files/manager.py
load_text
Load text from a file at the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The file key where the text should be loaded from |
required |
matches_glob
staticmethod
Checks if a given path matches a supplied glob pattern.
Note
Replace with glob.translate once Python 3.12 support is dropped; it is unavailable in Python 3.12.
Source code in src/typerdrive/files/manager.py
resolve_path
Resolve a given file key path to an absolute path within the files directory.
If the resolved path is outside the files directory, an exception will be raised. If the resolved path is the same as the files directory, an exception will be raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
The file key |
required |
mkdir
|
bool
|
If set, create the directory for the file key if it does not exist. |
False
|
Source code in src/typerdrive/files/manager.py
store_bytes
Store binary data as a file at the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The binary data to store |
required |
path
|
Path | str
|
The file key where the data should be stored |
required |
mode
|
int | None
|
The file mode to use when creating the file |
None
|
Source code in src/typerdrive/files/manager.py
store_json
Store a dictionary as a JSON file at the given key.
The dictionary must be json serializable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The dict to store as JSON |
required |
path
|
Path | str
|
The file key where the JSON should be stored |
required |
mode
|
int | None
|
The file mode to use when creating the file |
None
|
Source code in src/typerdrive/files/manager.py
store_text
Store text as a file at the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to store |
required |
path
|
Path | str
|
The file key where the text should be stored |
required |
mode
|
int | None
|
The file mode to use when creating the file |
None
|