-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.py
More file actions
123 lines (94 loc) · 4.28 KB
/
Copy pathfilesystem.py
File metadata and controls
123 lines (94 loc) · 4.28 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
import os
import re
class File:
def __init__(self, name, content=""):
self.name = name
self.content = content
class Directory:
def __init__(self, name):
self.name = name
self.children = {} # Dictionary to store child files and directories
class InMemoryFileSystem:
def __init__(self):
self.root = Directory("/")
self.current_directory = self.root
def mkdir(self, directory_name):
new_directory = Directory(directory_name)
self.current_directory.children[directory_name] = new_directory
def cd(self, path):
if path == "..":
self.current_directory = self._get_parent_directory(self.current_directory)
elif path == "/":
self.current_directory = self.root
else:
target_directory = self._get_absolute_path_from_root(path)
if target_directory:
self.current_directory = target_directory
def ls(self, path="."):
target_directory = self._get_absolute_path(path)
contents = [item.name for item in target_directory.children.values()]
return contents
def grep(self, pattern, file_path):
target_file = self._get_absolute_path(file_path)
if isinstance(target_file, File):
return re.search(pattern, target_file.content)
def cat(self, file_path):
target_file = self._get_absolute_path(file_path)
if isinstance(target_file, File):
return target_file.content
def touch(self, file_path):
new_file_name = os.path.basename(file_path)
new_file = File(new_file_name)
self.current_directory.children[new_file_name] = new_file
def echo(self, text, file_path):
target_file = self._get_absolute_path(file_path)
if isinstance(target_file, File):
target_file.content = text
def mv(self, source_path, destination_path):
source = self._get_absolute_path(source_path)
destination_directory = self._get_absolute_path(destination_path)
if isinstance(source, (File, Directory)):
destination_directory.children[source.name] = source
del self._get_parent_directory(self._get_absolute_path(source_path)).children[source.name]
def cp(self, source_path, destination_path):
source = self._get_absolute_path(source_path)
destination_directory = self._get_absolute_path(destination_path)
if isinstance(source, (File, Directory)):
destination_directory.children[source.name] = source
def rm(self, path):
target = self._get_absolute_path(path)
if isinstance(target, (File, Directory)):
del self._get_parent_directory(target).children[target.name]
def _get_absolute_path(self, path):
if path.startswith("/"):
return self._get_absolute_path_from_root(path)
else:
return self._get_absolute_path_from_current(path)
def _get_absolute_path_from_root(self, path):
current_directory = self.root
components = path.strip("/").split("/")
for component in components:
if component == "..":
current_directory = self._get_parent_directory(current_directory)
else:
current_directory = current_directory.children.get(component, None)
if current_directory is None:
return None
return current_directory
def _get_absolute_path_from_root(self, path):
components = path.strip("/").split("/")
current_directory = self.root
for component in components:
if component == "..":
current_directory = self._get_parent_directory(current_directory)
else:
current_directory = current_directory.children.get(component, None)
if current_directory is None:
return None # Directory not found
return current_directory
def _get_parent_directory(self, node):
if node == self.root:
return self.root
for directory in self.root.children.values():
if node in directory.children.values():
return directory