Repository navigation
Expand file tree
/
Copy path05_advanced_vfs_features.py
More file actions
347 lines (280 loc) Β· 11.9 KB
/
Copy path05_advanced_vfs_features.py
File metadata and controls
347 lines (280 loc) Β· 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
"""
Example 5: Advanced VFS Features
This example demonstrates advanced VFS functionality available in all namespaces:
- Batch operations (create, read, write, delete)
- Metadata management
- Directory operations (cd, rmdir, is_dir, is_file)
- File search and find
- Text/binary operations
- Touch and stat operations
- Storage stats
Everything shown here works for BOTH blob and workspace namespaces!
"""
import asyncio
import os
os.environ.setdefault("ARTIFACT_PROVIDER", "memory")
os.environ.setdefault("SESSION_PROVIDER", "memory")
from chuk_artifacts import ArtifactStore, NamespaceType, StorageScope
async def main():
store = ArtifactStore()
print("=" * 70)
print("ADVANCED VFS FEATURES")
print("=" * 70)
# Create a workspace for demonstrations
workspace = await store.create_namespace(
type=NamespaceType.WORKSPACE,
name="advanced-demo",
scope=StorageScope.SESSION,
)
vfs = store.get_namespace_vfs(workspace.namespace_id)
# ========================================================================
# Part 1: Batch Operations
# ========================================================================
print("\nπ¦ PART 1: BATCH OPERATIONS")
print("-" * 70)
# Batch create files (creates nodes + writes content)
file_specs = [
{"path": "/batch1.txt", "content": b"First batch file"},
{"path": "/batch2.txt", "content": b"Second batch file"},
{"path": "/batch3.txt", "content": b"Third batch file"},
]
await vfs.batch_create_files(file_specs)
print(f"β Batch created {len(file_specs)} files")
# Batch read files
paths = ["/batch1.txt", "/batch2.txt", "/batch3.txt"]
batch_data = await vfs.batch_read_files(paths)
print(f"β Batch read {len(batch_data)} files:")
for path, data in batch_data.items():
print(f" - {path}: {data.decode()}")
# Batch write files (updates existing files)
update_data = {
"/batch1.txt": b"Updated first file",
"/batch2.txt": b"Updated second file",
}
await vfs.batch_write_files(update_data)
print(f"β Batch updated {len(update_data)} files")
# Batch delete
await vfs.batch_delete_paths(["/batch1.txt", "/batch2.txt"])
print("β Batch deleted 2 files")
remaining = await vfs.ls("/")
print(f"β Remaining files: {remaining}")
# ========================================================================
# Part 2: Metadata Management
# ========================================================================
print("\nπ·οΈ PART 2: METADATA MANAGEMENT")
print("-" * 70)
# Write file with metadata
await vfs.write_file("/meta_test.txt", b"File with metadata")
# Set metadata
metadata = {
"author": "Alice",
"version": "1.0",
"tags": ["important", "demo"],
"custom": {"key": "value"},
}
await vfs.set_metadata("/meta_test.txt", metadata)
print("β Set metadata on /meta_test.txt")
# Get metadata
retrieved_meta = await vfs.get_metadata("/meta_test.txt")
print("β Retrieved metadata:")
print(f" Author: {retrieved_meta.get('author')}")
print(f" Version: {retrieved_meta.get('version')}")
print(f" Tags: {retrieved_meta.get('tags')}")
# Get full node info (includes metadata + stats)
node_info = await vfs.get_node_info("/meta_test.txt")
print("\nβ Node info:")
print(f" Size: {node_info.size} bytes")
print(f" Is directory: {node_info.is_dir}")
print(f" Created: {node_info.created_at}")
print(f" MIME type: {node_info.mime_type}")
# ========================================================================
# Part 3: Directory Operations
# ========================================================================
print("\nπ PART 3: DIRECTORY OPERATIONS")
print("-" * 70)
# Create nested directories
await vfs.mkdir("/level1")
await vfs.mkdir("/level1/level2")
await vfs.mkdir("/level1/level2/level3")
print("β Created nested directory structure")
# Change directory
await vfs.cd("/level1/level2")
print("β Changed directory to /level1/level2")
# Create file in current directory
await vfs.write_file("file_in_level2.txt", b"Content")
print("β Created file in current directory")
# Check if directory/file
is_dir = await vfs.is_dir("/level1")
is_file = await vfs.is_file("/level1/level2/file_in_level2.txt")
print(f"\nβ /level1 is directory: {is_dir}")
print(f"β file_in_level2.txt is file: {is_file}")
# Remove directory
await vfs.rmdir("/level1/level2/level3")
print("β Removed /level1/level2/level3 directory")
# ========================================================================
# Part 4: Text vs Binary Operations
# ========================================================================
print("\nπ PART 4: TEXT VS BINARY OPERATIONS")
print("-" * 70)
# Write text file
await vfs.write_text(
"/text_file.txt", "Hello, World!\nThis is text.", encoding="utf-8"
)
print("β Wrote text file with write_text()")
# Read as text
text_content = await vfs.read_text("/text_file.txt", encoding="utf-8")
print(f"β Read as text: {repr(text_content)}")
# Write binary file
binary_data = bytes([0x89, 0x50, 0x4E, 0x47]) # PNG header
await vfs.write_binary("/binary_file.bin", binary_data)
print(f"\nβ Wrote binary file ({len(binary_data)} bytes)")
# Read as binary
binary_content = await vfs.read_binary("/binary_file.bin")
print(f"β Read as binary: {binary_content.hex()}")
# Generic read_file (auto-detects)
generic_text = await vfs.read_file("/text_file.txt", as_text=True)
generic_binary = await vfs.read_file("/binary_file.bin", as_text=False)
print(f"\nβ Generic read (text): {type(generic_text).__name__}")
print(f"β Generic read (binary): {type(generic_binary).__name__}")
# ========================================================================
# Part 5: Touch and File Checks
# ========================================================================
print("\nπ PART 5: TOUCH AND FILE CHECKS")
print("-" * 70)
# Touch creates empty file or updates timestamp
await vfs.touch("/touched.txt")
print("β Touched /touched.txt (created empty file)")
# Check existence
exists = await vfs.exists("/touched.txt")
print(f"β /touched.txt exists: {exists}")
# Touch with custom metadata (use custom_meta dict)
await vfs.touch(
"/meta_touch.txt", custom_meta={"project": "demo", "status": "active"}
)
node_info2 = await vfs.get_node_info("/meta_touch.txt")
print(f"β Touched with custom metadata: {node_info2.custom_meta}")
# ========================================================================
# Part 6: Search and Find
# ========================================================================
print("\nπ PART 6: SEARCH AND FIND")
print("-" * 70)
# Create test files for searching
await vfs.mkdir("/search_test")
await vfs.write_file("/search_test/file1.txt", b"content one")
await vfs.write_file("/search_test/file2.log", b"content two")
await vfs.write_file("/search_test/file3.txt", b"content three")
await vfs.mkdir("/search_test/subdir")
await vfs.write_file("/search_test/subdir/file4.txt", b"content four")
# Find files by pattern
txt_files = await vfs.find(pattern="*.txt", path="/search_test")
print(f"\nβ Found .txt files: {txt_files}")
# Find by name
file1_results = await vfs.find(pattern="file1.txt", path="/search_test")
print(f"β Found file1.txt: {file1_results}")
# Find all files recursively
all_files = await vfs.find(pattern="*", path="/search_test", recursive=True)
print(f"β Found all files: {all_files}")
# ========================================================================
# Part 7: Storage Stats
# ========================================================================
print("\nπ PART 7: STORAGE STATS")
print("-" * 70)
# Get storage statistics
stats = await vfs.get_storage_stats()
print("\nβ Storage statistics:")
print(f" Total files: {stats.get('total_files', 'N/A')}")
print(f" Total size: {stats.get('total_size', 'N/A')} bytes")
print(f" Provider: {await vfs.get_provider_name()}")
# ========================================================================
# Part 8: Advanced File Operations
# ========================================================================
print("\nβοΈ PART 8: ADVANCED FILE OPERATIONS")
print("-" * 70)
# Create test structure
await vfs.write_file("/source.txt", b"Source content")
# Copy preserves original
await vfs.cp("/source.txt", "/copy.txt")
print("β Copied /source.txt to /copy.txt")
# Move removes original
await vfs.mv("/copy.txt", "/moved.txt")
print("β Moved /copy.txt to /moved.txt")
# Verify
source_exists = await vfs.exists("/source.txt")
copy_exists = await vfs.exists("/copy.txt")
moved_exists = await vfs.exists("/moved.txt")
print(f"\nβ /source.txt exists: {source_exists}")
print(f"β /copy.txt exists: {copy_exists}")
print(f"β /moved.txt exists: {moved_exists}")
# ========================================================================
# Part 9: Batch Create with Specs
# ========================================================================
print("\nπ PART 9: BATCH CREATE WITH SPECS")
print("-" * 70)
# Batch create files with full specifications
file_specs = [
{
"path": "/batch_spec1.txt",
"content": b"Spec file 1",
"metadata": {"type": "spec", "index": 1},
},
{
"path": "/batch_spec2.txt",
"content": b"Spec file 2",
"metadata": {"type": "spec", "index": 2},
},
{
"path": "/batch_spec3.txt",
"content": b"Spec file 3",
"metadata": {"type": "spec", "index": 3},
},
]
await vfs.batch_create_files(file_specs)
print(f"β Batch created {len(file_specs)} files with metadata")
# Verify metadata was set
spec1_meta = await vfs.get_metadata("/batch_spec1.txt")
print(f"β Batch spec1 metadata: {spec1_meta}")
# ========================================================================
# Summary
# ========================================================================
print("\n" + "=" * 70)
print("β¨ ADVANCED VFS FEATURES - SUMMARY")
print("=" * 70)
print("""
BATCH OPERATIONS:
β batch_write_files() - Write multiple files at once
β batch_read_files() - Read multiple files at once
β batch_create_files() - Create with metadata specs
β batch_delete_paths() - Delete multiple paths
METADATA:
β set_metadata() - Attach custom metadata
β get_metadata() - Retrieve metadata
β get_node_info() - Full file/directory info
DIRECTORY OPERATIONS:
β mkdir() / rmdir() - Create/remove directories
β cd() - Change current directory
β is_dir() / is_file() - Type checking
TEXT & BINARY:
β write_text() / read_text() - Text operations
β write_binary() / read_binary() - Binary operations
β read_file(as_text=True/False) - Generic read
FILE OPERATIONS:
β touch() - Create/update file
β exists() - Check existence
β cp() / mv() / rm() - Copy/move/delete
SEARCH:
β find(pattern=, path=, recursive=) - Find files by pattern
STATS:
β get_storage_stats() - Storage metrics
β get_provider_name() - Provider info
ALL WORK FOR BOTH BLOB AND WORKSPACE NAMESPACES!
""")
# Cleanup
print("\nπ§Ή Cleaning up...")
await store.destroy_namespace(workspace.namespace_id)
print("β Cleaned up workspace")
print("\n" + "=" * 70)
print("β ADVANCED VFS FEATURES DEMO COMPLETE")
print("=" * 70)
if __name__ == "__main__":
asyncio.run(main())