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
ReadOnlyFS
Wraps any filesystem and blocks all write operations:
from monkeyfs import VirtualFS, ReadOnlyFS, patch
vfs = VirtualFS({})
vfs.write("data.csv", b"a,b,c")
ro = ReadOnlyFS(vfs)
with patch(ro):
print(open("data.csv").read()) # a,b,c
open("new.txt", "w") # PermissionError
MountFS
Routes operations to different filesystems by path prefix:
from monkeyfs import VirtualFS, MountFS, ReadOnlyFS, patch
base = VirtualFS({})
overlay = VirtualFS({})
overlay.write("summary.md", b"# Chapter 1")
fs = MountFS(base, {"/chapters": ReadOnlyFS(overlay)})
with patch(fs):
# Writes go to base
with open("app.py", "w") as f:
f.write("print('hi')")
# Reads from /chapters go to the overlay
print(open("/chapters/summary.md").read()) # # Chapter 1
# Writes to /chapters are blocked (read-only)
open("/chapters/new.md", "w") # PermissionError
Part of the agex stack
monkeyfs provides filesystem interception for sandtrap and agex, giving sandboxed agent code an isolated virtual filesystem. VirtualFS accepts any dict-like backing store -- including kvgit Staged instances for a versioned filesystem with commit/rollback.
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.3.tar.gz.
File metadata
- Download URL: monkeyfs-0.1.3.tar.gz
- Upload date:
- Size: 58.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5497a4629c185a82f0086517e307c74580c8f5e5ad607f74d8fbc2c717fe67f
|
|
| MD5 |
e5cdcef1e74ce3878d4871681f88aef7
|
|
| BLAKE2b-256 |
0ae4fe3e51daf38c588b7c143831ee7a21b5aa3716c7b7929de7aeec8a4618fa
|
File details
Details for the file monkeyfs-0.1.3-py3-none-any.whl.
File metadata
- Download URL: monkeyfs-0.1.3-py3-none-any.whl
- Upload date:
- Size: 37.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 |
6c8c6abf4dff52e747a4a10a8da9f7d6fad7470f9a5bfbadc27cf787ab9f5e79
|
|
| MD5 |
ae5997dcec2bfda79ea1745143572fa3
|
|
| BLAKE2b-256 |
1f2b950c6dd6d1a8296628e8fe7a6811d878a51e5227e8afa35a9cc6114851da
|