Skip to content

Commit 6217718

Browse files
authored
Add FS utility. (#175)
1 parent 52402b2 commit 6217718

File tree

1 file changed

+112
-0
lines changed
  • aws_doc_sdk_examples_tools

1 file changed

+112
-0
lines changed

aws_doc_sdk_examples_tools/fs.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from abc import ABC, abstractmethod
2+
from dataclasses import dataclass
3+
from fnmatch import fnmatch
4+
from os import listdir
5+
from pathlib import Path
6+
from stat import S_ISREG
7+
from typing import Dict, Generator, List
8+
9+
10+
@dataclass(frozen=True)
11+
class Stat:
12+
path: Path
13+
exists: bool
14+
is_file: bool
15+
16+
@property
17+
def is_dir(self):
18+
return self.exists and not self.is_file
19+
20+
21+
class Fs(ABC):
22+
@abstractmethod
23+
def glob(self, path: Path, glob: str) -> Generator[Path, None, None]:
24+
pass
25+
26+
@abstractmethod
27+
def read(self, path: Path) -> str:
28+
pass
29+
30+
@abstractmethod
31+
def write(self, path: Path, content: str):
32+
pass
33+
34+
@abstractmethod
35+
def stat(self, path: Path) -> Stat:
36+
pass
37+
38+
@abstractmethod
39+
def mkdir(self, path: Path):
40+
pass
41+
42+
@abstractmethod
43+
def list(self, path: Path) -> List[Path]:
44+
pass
45+
46+
47+
class PathFs(Fs):
48+
def glob(self, path: Path, glob: str) -> Generator[Path, None, None]:
49+
return path.glob(glob)
50+
51+
def read(self, path: Path) -> str:
52+
with path.open("r") as file:
53+
return file.read()
54+
55+
def write(self, path: Path, content: str):
56+
with path.open("w") as file:
57+
file.write(content)
58+
59+
def stat(self, path: Path) -> Stat:
60+
if path.exists():
61+
stat = path.stat()
62+
return Stat(path, True, S_ISREG(stat.st_mode))
63+
else:
64+
return Stat(path, False, False)
65+
66+
def mkdir(self, path: Path):
67+
path.mkdir(parents=True, exist_ok=True)
68+
69+
def list(self, path: Path) -> List[Path]:
70+
if self.stat(path).is_file:
71+
return []
72+
return [path / name for name in listdir(path)]
73+
74+
75+
class RecordFs(Fs):
76+
def __init__(self, fs: Dict[Path, str]):
77+
self.fs = fs
78+
79+
def glob(self, path: Path, glob: str) -> Generator[Path, None, None]:
80+
path_s = str(path)
81+
for key in self.fs.keys():
82+
key_s = str(key)
83+
if key_s.startswith(path_s):
84+
if fnmatch(key_s, glob):
85+
yield key
86+
87+
def read(self, path: Path) -> str:
88+
return self.fs[path]
89+
90+
def write(self, path: Path, content: str):
91+
base = str(path.parent)
92+
assert any(
93+
[str(key).startswith(base) for key in self.fs]
94+
), "No parent folder, this will probably fail without a call to mkdir in a real file system!"
95+
self.fs[path] = content
96+
97+
def stat(self, path: Path):
98+
if path in self.fs:
99+
return Stat(path, True, True)
100+
for item in self.fs.keys():
101+
if str(item).startswith(str(path)):
102+
return Stat(path, True, False)
103+
return Stat(path, False, False)
104+
105+
def mkdir(self, path: Path):
106+
self.fs.setdefault(path, "")
107+
108+
def list(self, path: Path) -> List[Path]:
109+
return [item for item in self.fs.keys() if item.parent == path]
110+
111+
112+
fs = PathFs()

0 commit comments

Comments
 (0)