-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.py
More file actions
48 lines (39 loc) · 1.33 KB
/
Copy pathfile.py
File metadata and controls
48 lines (39 loc) · 1.33 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
from inode import Inode
class File:
def __init__(self, path: str):
self.path = path
self.offset = 0
class OpenedFiles:
def __init__(self) -> None:
self.files: dict[int, File] = {}
self.new_handle = 1
def clear(self) -> None:
self.files.clear()
self.new_handle = 1
def add(self, file: File) -> int:
handle = self.new_handle
self.files[handle] = file
self.new_handle += 1
return handle
def get(self, handle: int) -> File:
if handle not in self.files:
raise FileNotFoundError(f"File handle {handle} not found")
return self.files[handle]
def find(self, path: str) -> int:
for handle, file in self.files.items():
if file.path == path:
return handle
raise FileNotFoundError(f"File {path} not found")
def pop(self, item: int | str) -> None:
if isinstance(item, int):
self.files.pop(item)
else:
handle = self.find(item)
self.files.pop(handle)
def __contains__(self, wanted: int | str) -> bool:
if isinstance(wanted, int):
return wanted in self.files
for file in self.files.values():
if file.path == wanted:
return True
return False