Решение на In-memory файлова система от Николай Велков

Обратно към всички решения

Към профила на Николай Велков

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 6 успешни тест(а)
  • 12 неуспешни тест(а)

Код

class FileSystemError(Exception):
def __init__(self):
self.message = "FileSystemError"
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
super().__init__()
self.massage = "NodeDoesNotExist!"
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
super().__init__()
self.message = "SourceNodeDoesNotExistError"
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
super().__init__()
self.message = "DestinationNodeDoesNotExistError"
class FileSystemMountError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "FileSystemMountError"
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointDoesNotExistError"
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointDoesNotExistError"
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointDoesNotExistError"
class NotAMountpointError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "NotAMountpointError"
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DestinationNodeExistsError"
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NotEnoughSpaceError"
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NonExplicitDirectoryDeletionError"
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NonEmptyDirectoryDeletionError"
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DestinationNotADirectoryError"
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DirectoryHardLinkError"
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DirectoryHardLinkError"
class LinkPathError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "LinkPathError"
class Hard_link:
def __init__(self, content):
self.name = name.split('/')[1]
self.content = content
self.is_link = True
self.is_directory = False
class Symbolic_link:
def __init__(self, file_system, path, name):
self.name = name.split('/')[1]
self.file_system = file_system
self.link_path = path
self.is_link = True
self.is_directory = False
@property
def linked_content(self):
if not file_system.has_node(path):
raise LinkPathError
return self.link_path.content
@property
def linked_files(self):
if not file_system.has_node(path):
raise LinkPathError
return self.link_path.files
@property
def linked_directories(self):
if not file_system.has_node(path):
raise LinkPathError
return self.link_path.directories
@property
def linked_nodes(self):
if not file_system.has_node(path):
raise LinkPathError
return self.link_path.nodes
@property
def links_file(self):
return not self.link_path.is_directory
class File_Common:
def __init__(self, name, content):
self.name = name.split('/')[1]
self.content = content
self.is_link = False
self.is_directory = False
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
def get_name(self):
return self.name
@property
def size(self):
return len(str(self.content)) + 1
class Directory:
def __init__(self, name):
self.name = name.split('/')[1]
self.is_link = False
self.is_directory = True
self.content = []
@property
def files(self):
return [x for x in self.content if not x.is_directory]
@property
def directories(self):
return [x for x in self.content if x.is_directory]
@property
def nodes(self):
return self.content
def get_name(self):
return self.name
def is_empty(self):
return self.content == []
def append_node(self, node):
self.content.append(node)
def remove(self, node):
self.content.remove(node)
class FileSystem:
def __init__(self, size_in_bytes):
self.size = size_in_bytes
self.available_size = self.size - self.__size_of('/', '')
self.root = Directory('/')
self.is_file_system = True
def has_node(self, path, nodes):
path_names = path.split('/')
directory_names = list(map(lambda node: node.get_name(), nodes))
if directory_names == [] or path_names[1] == '':
return False
elif path_names[1] in directory_names:
if len(path_names) == 2:
return True
else:
for node in nodes:
if node.is_directory:
way = path.partition('/{}'.format(path_names[1]))[2]
if self.has_node(way, node.nodes):
return True
return False
def find_node(self, path, nodes):
path_names = path.split('/')
directory_names = list(map(lambda node: node.get_name(), nodes))
while len(path_names) > 2:
nodes = nodes[directory_names.index(path_names[1])].nodes
directory_names = list(map(lambda node: node.get_name(), nodes))
path = path.partition('/{}'.format(path_names[1]))[2]
path_names = path.split('/')
return nodes[directory_names.index(path_names[1])]
def get_node(self, path):
if path == '/' or path == '':
return self.root
elif not self.has_node(path, self.root.nodes):
raise NodeDoesNotExistError
return self.find_node(path, self.root.nodes)
#if not __found(path):
#raise NodeDoesNotExist
def create(self, path, directory=False, content=''):
par_name = path.rpartition('/')[0]
if par_name != '' and not self.has_node(par_name, self.root.nodes):
raise DestinationNodeDoesNotExistError
if self.__size_of(directory, content) > self.available_size:
raise NotEnoughSpaceError
if self.has_node(path, self.root.nodes):
raise DestinationNodeExistsError
parent_directory = self.get_node(par_name)
new_name = '/{}'.format(path.rpartition('/')[2])
if directory:
new_node = Directory(new_name)
else:
new_node = File_Common(new_name, content)
parent_directory.append_node(new_node)
memory_allocated = self.__size_of(directory, content)
self.size += memory_allocated
self.available_size -= memory_allocated
def remove(self, path, directory=False, force=True):
if not self.has_node(path, self.root.nodes):
raise NodeDoesNotExistError
node = self.get_node(path)
if node.is_directory:
if not directory:
raise NonexcplicitDirectoryDeletionError
elif not node.is_empty() and not force:
raise NonEmptyDirectoryDeletionError
parent_name = path.rpartition('/')[0]
if parent_name == '':
self.root.remove(node)
else:
parent_node = self.get_node(parent_name)
parent_node.remove(node)
if node.is_link:
memory_allocated = 1
else:
memory_allocated = self.__size_of(directory, node.content)
self.size -= memory_allocated
self.available_size += memory_allocated
def move(self, source, destination):
if not self.has_node(source, self.root.nodes):
raise SourceNodeDoesNotExistError
if not self.has_node(destination, self.root.nodes):
raise DestinationNodeDoesNotExistError
else:
destination_node = self.get_node(destination)
if not destination_node.is_directory:
DestinationNotADirectoryError
else:
names = map(lambda n: n.get_name(), destination_node.nodes)
source_name = source.rpartition('/')[2]
if source_name in names:
raise DestinationNodeExistsError
source_name = source.rpartition('/')[2]
node_name = '{}/{}'.format(destination, source_name)
source_node = self.get_node(source)
if source_node.is_directory:
self.create(node_name, directory=True)
new_node = self.get_node(node_name)
for x in source_node.nodes:
new_node.append(x)
else:
self.create(
node_name,
directory=False,
content=source_node.content
)
self.remove(source)
def link(self, source, destination, symbolic=True):
if not self.has_node(source):
if symbolic:
raise NodeDoesNotExist
else:
raise SourceNodeDoesNotExistError
pointed_node = self.get_node(source)
if pointed_node.is_directory:
if not symbolic:
raise DirectoryHardLinkError
path = destination.rpartition('/')
new_link_name = '/{}'.format(path[2])
if symbolic:
new_link = Symbolic_link(self, source, new_link_name)
else:
new_link = Hard_link(new_link_name, pointed_node.content)
parent_node = self.get_node(path[0])
parent_node.append(new_link)
memory_allocated = 1
self.size += memory_allocated
self.available_size -= memory_allocated
def mount(self, file_system, path):
if not self.has_node(path):
raise MountPointDoesNotExistError
pointed_node = self.get_node(path)
if pointed_node.is_directory:
if not pointed_node.is_empty:
raise MountPointNotEmptyError
else:
raise MountPointNotADirectoryError
def unmount(self, path):
if not self.has_node(path):
raise NodeDoesNotExistError
pointed_node = self.get_node(path)
has_fs = any(map(lambda x: x.is_file_system, pointed_node.nodes))
if has_fs:
raise NotAMountpointError
def __size_of(self, directory, content):
if directory:
return 1
else:
return 1 + len(str(content))
def __node_name(self, path):
return '/{}'.format(path.rpartition('/')[2])

Лог от изпълнението

..EEEEE.EEE..E.EEE
======================================================================
ERROR: test_hard_link_create (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_space_consumption (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_to_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_to_missing_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_link_create (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_mounting (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_move_not_to_a_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_move_overwrite (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_remove_empty_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_remove_nonempty_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_symlink_to_missing_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_valid_move (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 18 tests in 24.163s

FAILED (errors=12)

История (2 версии и 0 коментара)

Николай обнови решението на 30.04.2015 13:37 (преди над 9 години)

+class FileSystemError(Exception):
+ def __init__(self):
+ self.message = "FileSystemError"
+
+
+class NodeDoesNotExistError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.massage = "NodeDoesNotExist!"
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ super().__init__()
+ self.message = "SourceNodeDoesNotExistError"
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ super().__init__()
+ self.message = "DestinationNodeDoesNotExistError"
+
+
+class FileSystemMountError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "FileSystemMountError"
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+ self.message = "MountPointDoesNotExistError"
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+ self.message = "MountPointDoesNotExistError"
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+ self.message = "MountPointDoesNotExistError"
+
+
+class NotAMountpointError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+ self.message = "NotAMountpointError"
+
+
+class DestinationNodeExistsError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "DestinationNodeExistsError"
+
+
+class NotEnoughSpaceError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "NotEnoughSpaceError"
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "NonExplicitDirectoryDeletionError"
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "NonEmptyDirectoryDeletionError"
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "DestinationNotADirectoryError"
+
+
+class DirectoryHardLinkError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "DirectoryHardLinkError"
+
+
+class DirectoryHardLinkError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "DirectoryHardLinkError"
+
+
+class File:
+ def __init__(self, name, content):
+ self.name = name.split('/')[1]
+ self.content = content
+ self.is_directory = False
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+ def get_name(self):
+ return self.name
+
+ @property
+ def size(self):
+ return len(str(self.content)) + 1
+
+
+class Directory:
+ def __init__(self, name):
+ self.name = name.split('/')[1]
+ self.is_directory = True
+ self.content = []
+
+ @property
+ def files(self):
+ return [x for x in self.content if not x.is_directory]
+
+ @property
+ def directories(self):
+ return [x for x in self.content if x.is_directory]
+
+ @property
+ def nodes(self):
+ return self.content
+
+ def get_name(self):
+ return self.name
+
+ def is_empty(self):
+ return self.content == []
+
+ def append_node(self, node):
+ self.content.append(node)
+
+ def remove(self, node):
+ self.content.remove(node)
+
+
+class FileSystem:
+
+ def __init__(self, size_in_bytes):
+ self.size = size_in_bytes
+ self.available_size = self.size - self.__size_of('/', '')
+ self.root = Directory('/')
+
+ def _has_node(self, path, nodes):
+ path_names = path.split('/')
+ directory_names = list(map(lambda node: node.get_name(), nodes))
+
+ if directory_names == [] or path_names[1] == '':
+ return False
+ elif path_names[1] in directory_names:
+ if len(path_names) == 2:
+ return True
+ else:
+ for node in nodes:
+ if node.is_directory:
+ way = path.partition('/{}'.format(path_names[1]))[2]
+ if self._has_node(way, node.nodes):
+ return True
+ return False
+
+ def find_node(self, path, nodes):
+ path_names = path.split('/')
+ directory_names = list(map(lambda node: node.get_name(), nodes))
+
+ while len(path_names) > 2:
+ nodes = nodes[directory_names.index(path_names[1])].nodes
+ directory_names = list(map(lambda node: node.get_name(), nodes))
+ path = path.partition('/{}'.format(path_names[1]))[2]
+ path_names = path.split('/')
+
+ return nodes[directory_names.index(path_names[1])]
+
+ def get_node(self, path):
+ if path == '/' or path == '':
+ return self.root
+ elif not self._has_node(path, self.root.nodes):
+ raise NodeDoesNotExistError
+ return self.find_node(path, self.root.nodes)
+ #if not __found(path):
+ #raise NodeDoesNotExist
+
+ def create(self, path, directory=False, content=''):
+ par_name = path.rpartition('/')[0]
+
+ if par_name != '' and not self._has_node(par_name, self.root.nodes):
+ raise DestinationNodeDoesNotExistError
+ if self.__size_of(directory, content) > self.available_size:
+ raise NotEnoughSpaceError
+ if self._has_node(path, self.root.nodes):
+ raise DestinationNodeExistsError
+
+ parent_directory = self.get_node(par_name)
+ new_name = '/{}'.format(path.rpartition('/')[2])
+
+ if directory:
+ new_node = Directory(new_name)
+ else:
+ new_node = File(new_name, content)
+
+ parent_directory.append_node(new_node)
+
+ memory_allocated = self.__size_of(directory, content)
+ self.size += memory_allocated
+ self.available_size -= memory_allocated
+
+ def remove(self, path, directory=False, force=True):
+ if not self._has_node(path, self.root.nodes):
+ raise NodeDoesNotExistError
+
+ node = self.get_node(path)
+ if node.is_directory:
+ if not directory:
+ raise NonexcplicitDirectoryDeletionError
+ elif not node.is_empty() and not force:
+ raise NonEmptyDirectoryDeletionError
+
+ parent_name = path.rpartition('/')[0]
+ if parent_name == '':
+ self.root.remove(node)
+ else:
+ parent_node = self.get_node(parent_name)
+ parent_node.remove(node)
+
+ memory_allocated = self.__size_of(directory, node.content)
+ self.size -= memory_allocated
+ self.available_size += memory_allocated
+
+ def move(self, source, destination):
+ if not self._has_node(source, self.root.nodes):
+ raise SourceNodeDoesNotExistError
+ if not self._has_node(destination, self.root.nodes):
+ raise DestinationNodeDoesNotExistError
+ else:
+ destination_node = self.get_node(destination)
+ if not destination_node.is_directory:
+ DestinationNotADirectoryError
+ else:
+ names = map(lambda n: n.get_name(), destination_node.nodes)
+ source_name = source.rpartition('/')[2]
+ if source_name in names:
+ raise DestinationNodeExistsError
+
+ source_name = source.rpartition('/')[2]
+ node_name = '{}/{}'.format(destination, source_name)
+ source_node = self.get_node(source)
+
+ if source_node.is_directory:
+ self.create(node_name, directory=True)
+ new_node = self.get_node(node_name)
+
+ for x in source_node.nodes:
+ new_node.append(x)
+ else:
+ self.create(
+ node_name,
+ directory=False,
+ content=source_node.content
+ )
+
+ self.remove(source)
+
+ def link(self, source, destination, symbol=True):
+ return True
+
+ def mount(self, file_system, path):
+ return True
+
+ def unmount(self, path):
+ return True
+
+ def __size_of(self, directory, content):
+ if directory:
+ return 1
+ else:
+ return 1 + len(str(content))
+
+ def __found(self, path):
+ return True

Николай обнови решението на 30.04.2015 16:39 (преди над 9 години)

class FileSystemError(Exception):
def __init__(self):
self.message = "FileSystemError"
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
super().__init__()
self.massage = "NodeDoesNotExist!"
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
super().__init__()
self.message = "SourceNodeDoesNotExistError"
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
super().__init__()
self.message = "DestinationNodeDoesNotExistError"
class FileSystemMountError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "FileSystemMountError"
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointDoesNotExistError"
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointDoesNotExistError"
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "MountPointDoesNotExistError"
class NotAMountpointError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = "NotAMountpointError"
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DestinationNodeExistsError"
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NotEnoughSpaceError"
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NonExplicitDirectoryDeletionError"
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "NonEmptyDirectoryDeletionError"
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DestinationNotADirectoryError"
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DirectoryHardLinkError"
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "DirectoryHardLinkError"
-class File:
+class LinkPathError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "LinkPathError"
+
+
+class Hard_link:
+ def __init__(self, content):
+ self.name = name.split('/')[1]
+ self.content = content
+ self.is_link = True
+ self.is_directory = False
+
+
+class Symbolic_link:
+ def __init__(self, file_system, path, name):
+ self.name = name.split('/')[1]
+ self.file_system = file_system
+ self.link_path = path
+ self.is_link = True
+ self.is_directory = False
+
+ @property
+ def linked_content(self):
+ if not file_system.has_node(path):
+ raise LinkPathError
+ return self.link_path.content
+
+ @property
+ def linked_files(self):
+ if not file_system.has_node(path):
+ raise LinkPathError
+ return self.link_path.files
+
+ @property
+ def linked_directories(self):
+ if not file_system.has_node(path):
+ raise LinkPathError
+ return self.link_path.directories
+
+ @property
+ def linked_nodes(self):
+ if not file_system.has_node(path):
+ raise LinkPathError
+ return self.link_path.nodes
+
+ @property
+ def links_file(self):
+ return not self.link_path.is_directory
+
+
+class File_Common:
def __init__(self, name, content):
self.name = name.split('/')[1]
self.content = content
+ self.is_link = False
self.is_directory = False
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
def get_name(self):
return self.name
@property
def size(self):
return len(str(self.content)) + 1
class Directory:
def __init__(self, name):
self.name = name.split('/')[1]
+ self.is_link = False
self.is_directory = True
self.content = []
@property
def files(self):
return [x for x in self.content if not x.is_directory]
@property
def directories(self):
return [x for x in self.content if x.is_directory]
@property
def nodes(self):
return self.content
def get_name(self):
return self.name
def is_empty(self):
return self.content == []
def append_node(self, node):
self.content.append(node)
def remove(self, node):
self.content.remove(node)
class FileSystem:
def __init__(self, size_in_bytes):
self.size = size_in_bytes
self.available_size = self.size - self.__size_of('/', '')
self.root = Directory('/')
+ self.is_file_system = True
- def _has_node(self, path, nodes):
+ def has_node(self, path, nodes):
path_names = path.split('/')
directory_names = list(map(lambda node: node.get_name(), nodes))
if directory_names == [] or path_names[1] == '':
return False
elif path_names[1] in directory_names:
if len(path_names) == 2:
return True
else:
for node in nodes:
if node.is_directory:
way = path.partition('/{}'.format(path_names[1]))[2]
- if self._has_node(way, node.nodes):
+ if self.has_node(way, node.nodes):
return True
return False
def find_node(self, path, nodes):
path_names = path.split('/')
directory_names = list(map(lambda node: node.get_name(), nodes))
while len(path_names) > 2:
nodes = nodes[directory_names.index(path_names[1])].nodes
directory_names = list(map(lambda node: node.get_name(), nodes))
path = path.partition('/{}'.format(path_names[1]))[2]
path_names = path.split('/')
return nodes[directory_names.index(path_names[1])]
def get_node(self, path):
if path == '/' or path == '':
return self.root
- elif not self._has_node(path, self.root.nodes):
+ elif not self.has_node(path, self.root.nodes):
raise NodeDoesNotExistError
return self.find_node(path, self.root.nodes)
#if not __found(path):
#raise NodeDoesNotExist
def create(self, path, directory=False, content=''):
par_name = path.rpartition('/')[0]
- if par_name != '' and not self._has_node(par_name, self.root.nodes):
+ if par_name != '' and not self.has_node(par_name, self.root.nodes):
raise DestinationNodeDoesNotExistError
if self.__size_of(directory, content) > self.available_size:
raise NotEnoughSpaceError
- if self._has_node(path, self.root.nodes):
+ if self.has_node(path, self.root.nodes):
raise DestinationNodeExistsError
parent_directory = self.get_node(par_name)
new_name = '/{}'.format(path.rpartition('/')[2])
if directory:
new_node = Directory(new_name)
else:
- new_node = File(new_name, content)
+ new_node = File_Common(new_name, content)
parent_directory.append_node(new_node)
memory_allocated = self.__size_of(directory, content)
self.size += memory_allocated
self.available_size -= memory_allocated
def remove(self, path, directory=False, force=True):
- if not self._has_node(path, self.root.nodes):
+ if not self.has_node(path, self.root.nodes):
raise NodeDoesNotExistError
node = self.get_node(path)
if node.is_directory:
if not directory:
raise NonexcplicitDirectoryDeletionError
elif not node.is_empty() and not force:
raise NonEmptyDirectoryDeletionError
parent_name = path.rpartition('/')[0]
if parent_name == '':
self.root.remove(node)
else:
parent_node = self.get_node(parent_name)
parent_node.remove(node)
- memory_allocated = self.__size_of(directory, node.content)
+ if node.is_link:
+ memory_allocated = 1
+ else:
+ memory_allocated = self.__size_of(directory, node.content)
+
self.size -= memory_allocated
self.available_size += memory_allocated
def move(self, source, destination):
- if not self._has_node(source, self.root.nodes):
+ if not self.has_node(source, self.root.nodes):
raise SourceNodeDoesNotExistError
- if not self._has_node(destination, self.root.nodes):
+ if not self.has_node(destination, self.root.nodes):
raise DestinationNodeDoesNotExistError
else:
destination_node = self.get_node(destination)
if not destination_node.is_directory:
DestinationNotADirectoryError
else:
names = map(lambda n: n.get_name(), destination_node.nodes)
source_name = source.rpartition('/')[2]
if source_name in names:
raise DestinationNodeExistsError
source_name = source.rpartition('/')[2]
node_name = '{}/{}'.format(destination, source_name)
source_node = self.get_node(source)
if source_node.is_directory:
self.create(node_name, directory=True)
new_node = self.get_node(node_name)
for x in source_node.nodes:
new_node.append(x)
else:
self.create(
node_name,
directory=False,
content=source_node.content
)
self.remove(source)
- def link(self, source, destination, symbol=True):
- return True
+ def link(self, source, destination, symbolic=True):
+ if not self.has_node(source):
+ if symbolic:
+ raise NodeDoesNotExist
+ else:
+ raise SourceNodeDoesNotExistError
+ pointed_node = self.get_node(source)
+ if pointed_node.is_directory:
+ if not symbolic:
+ raise DirectoryHardLinkError
+
+ path = destination.rpartition('/')
+ new_link_name = '/{}'.format(path[2])
+ if symbolic:
+ new_link = Symbolic_link(self, source, new_link_name)
+ else:
+ new_link = Hard_link(new_link_name, pointed_node.content)
+
+ parent_node = self.get_node(path[0])
+ parent_node.append(new_link)
+
+ memory_allocated = 1
+ self.size += memory_allocated
+ self.available_size -= memory_allocated
+
def mount(self, file_system, path):
- return True
+ if not self.has_node(path):
+ raise MountPointDoesNotExistError
+ pointed_node = self.get_node(path)
+ if pointed_node.is_directory:
+ if not pointed_node.is_empty:
+ raise MountPointNotEmptyError
+ else:
+ raise MountPointNotADirectoryError
+
def unmount(self, path):
- return True
+ if not self.has_node(path):
+ raise NodeDoesNotExistError
+ pointed_node = self.get_node(path)
+ has_fs = any(map(lambda x: x.is_file_system, pointed_node.nodes))
+
+ if has_fs:
+ raise NotAMountpointError
+
def __size_of(self, directory, content):
if directory:
return 1
else:
return 1 + len(str(content))
- def __found(self, path):
- return True
+ def __node_name(self, path):
+ return '/{}'.format(path.rpartition('/')[2])