-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefs.py
More file actions
73 lines (57 loc) · 1.51 KB
/
makefs.py
File metadata and controls
73 lines (57 loc) · 1.51 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
"""
corelib module that makes it 10000000000000000000000000x percent easier
to make filesystem structures,
here's an example:
```python
from makefs import mkfs
mkfs({
"name": "test_dir",
"items": [
{
"name": "test.py",
"text": "print('hello, world!')\"
},
{
"name": "content_dir",
"items": [
{
"name": "info.txt",
"text": "password: *******\n"
}
]
}
]
})
```
this will make a filesystem tree that looks like this:
[test_dir]
- test.py
[content_dir]
- info.txt
"""
import os
from typing import TypedDict
class _FileBlueprint(TypedDict):
name: str
text: str
class _DirectoryBlueprint(TypedDict):
name: str
items: list["_DirectoryBlueprint|_FileBlueprint"]
def file(map: _FileBlueprint, parent: str | None = None):
fullpath = map["name"]
if parent:
fullpath = os.path.join(parent, map["name"])
with open(fullpath, "w") as _FH:
_FH.write(map["text"])
_FH.close()
def mkfs(map: _DirectoryBlueprint, parent: str | None = None):
fullpath = map["name"]
if parent:
fullpath = os.path.join(parent, map["name"])
if not os.path.exists(fullpath):
os.mkdir(fullpath)
for item in map["items"]:
if item.get("text", None) is not None:
file(item, parent=fullpath) # type: ignore
continue
mkfs(item, parent=fullpath) # type: ignore