File System Port¶
file_system_port
¶
File system port for abstract file operations.
Defines the FileSystemPort contract that application code depends on,
decoupling file I/O from any specific implementation.
FileSystemPort
¶
Bases: OutboundPort
Abstract contract for file system operations.
Responsibilities
- Read and write file contents as bytes.
- Delete files and check existence.
- List directory contents.
Non-Responsibilities
- Manage file permissions or ownership.
- Resolve symlinks or handle special file types.
- Provide atomic or transactional file operations.
- Stream large files — the whole content is read into memory.
Example
Source code in src/forging_blocks/application/ports/outbound/file_system_port.py
read(path: Path | str) -> bytes
abstractmethod
async
¶
Read the contents of a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the file. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The file contents as bytes. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
Source code in src/forging_blocks/application/ports/outbound/file_system_port.py
write(path: Path | str, data: bytes) -> None
abstractmethod
async
¶
Write data to a file, creating parent directories as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the file. |
required |
data
|
bytes
|
The bytes to write. |
required |
Source code in src/forging_blocks/application/ports/outbound/file_system_port.py
delete(path: Path | str) -> None
abstractmethod
async
¶
Delete a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the file. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
exists(path: Path | str) -> bool
abstractmethod
async
¶
Check whether a file or directory exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/forging_blocks/application/ports/outbound/file_system_port.py
list_dir(path: Path | str) -> list[Path]
abstractmethod
async
¶
List the contents of a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Path to the directory. |
required |
Returns:
| Type | Description |
|---|---|
list[Path]
|
A list of |
Raises:
| Type | Description |
|---|---|
NotADirectoryError
|
If path is not a directory. |