Transparent filesystem interception via monkey-patching.
Project description
monkeyfs 🐒
Filesystem interception via monkey-patching.
Patches open(), os.listdir(), os.stat(), and 30+ other stdlib functions to route through a virtual or isolated filesystem. Patches are applied lazily on first patch() call and are inert outside the context. Uses contextvars for async-safe isolation between concurrent tasks. Zero dependencies.
Install
pip install monkeyfs
Quick example
from monkeyfs import VirtualFS, patch
vfs = VirtualFS({})
with patch(vfs):
with open("data.csv", "w") as f:
f.write("name,score\nalice,98\nbob,87\n")
import os
print(os.listdir("/")) # ['data.csv']
print(os.path.getsize("data.csv")) # 30
with open("data.csv") as f:
print(f.read()) # name,score\nalice,98\nbob,87\n
IsolatedFS
Restricts file operations to a root directory on the real filesystem:
from monkeyfs import IsolatedFS, patch
isolated = IsolatedFS(root="/tmp/sandbox")
with patch(isolated):
with open("notes.txt", "w") as f:
f.write("hello") # Written to /tmp/sandbox/notes.txt
open("/etc/passwd") # PermissionError -- outside root
Documentation
- API Reference -- public API, FileSystem protocol, patched functions
Development
uv sync --extra dev
uv run pytest
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file monkeyfs-0.1.1.tar.gz.
File metadata
- Download URL: monkeyfs-0.1.1.tar.gz
- Upload date:
- Size: 51.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48c938714455eee317bcb1f44c9899a06cc0eb231ce5e2b4c090096690886027
|
|
| MD5 |
e2b5ef158f79e3798d5f2dbfd745db5e
|
|
| BLAKE2b-256 |
d864c7eff21a759007331c15b9f2540ec7b9e1c2d2dabcea2957bf1d137e0317
|
File details
Details for the file monkeyfs-0.1.1-py3-none-any.whl.
File metadata
- Download URL: monkeyfs-0.1.1-py3-none-any.whl
- Upload date:
- Size: 32.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9067f0d89d49a83606137aeef8a33c4eeb499c0e37e2bf55b7f934139c7ac05
|
|
| MD5 |
fe27dff483cde5c8e488846a7d6b3551
|
|
| BLAKE2b-256 |
5ba2a6d71e2b6ed53c2cfd58e3328400f585bb5c34997d87875c72bdcd5934ca
|