Решение на In-memory файлова система от Евгения Евлогиева

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

Към профила на Евгения Евлогиева

Резултати

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

Код

class File():
def __init__(self, name, content):
self.size = len(content) + 1
self.name = name
self.content = content
self.is_directory = False
def append(self, text):
self.content += text
self.size += len(text)
def truncate(self, text):
self.content = text
self.size = len(text) + 1
class Directory():
def __init__(self, name):
self.size = 1
self.name = name
self.content = []
self.is_directory = True
self.is_mount_point = False
def directories(self):
return [obj for obj in self.content if obj.is_directory]
def files(self):
return [obj for obj in self.content if not obj.is_directory]
def nodes(self):
return self.content
def __contains__(self, node):
return node in self.content
def dir_get_node(self, path):
for node in self.content:
if node.name == path:
return node
return False
class FileSystemError(Exception):
def __init__(self):
self.message = "Something in your file system went wrong!"
class FileSystemMountError(FileSystemError):
def __init__(self):
super().__init__()
self.message = "Something in the mounting went wrong!"
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
super().__init__()
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
super().__init__()
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
super().__init__()
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
super().__init__()
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
super().__init__()
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
super().__init__()
class DirectoryHardLinkError(FileSystemError):
def __init__(self, directory):
super().__init__()
self.issuer = directory
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
super().__init__()
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
super().__init__()
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
super().__init__()
class NotAMountPointError(FileSystemMountError):
def __init__(self):
super().__init__()
class FileSystem():
def __init__(self, size_in_bytes):
self.size = size_in_bytes
self.available_size = size_in_bytes - 1
self.root = Directory('/')
def parent_path(self, path):
nodes_in_path = path.split('/')
current_node = self.root
searching_for_path = '/' + nodes_in_path[0]
for node in nodes_in_path[2:]:
next_node = current_node.dir_get_node(searching_for_path)
if next_node:
searching_for_path += ('/' + node)
current_node = next_node
else:
raise NodeDoesNotExistError
return current_node
def get_node(self, path):
node_where_path_is = self.parent_path(path)
return node_where_path_is.dir_get_node(path)
def create(self, path, directory=False, content=''):
try:
node_where_path_is = self.parent_path(path)
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError
if node_where_path_is.dir_get_node(path):
raise DestinationNodeExistsError
if directory:
new = Directory(path)
if self.available_size < 1:
raise NotEnoughSpaceError
else:
new = File(path, content)
if self.available_size < new.size:
raise NotEnoughSpaceError
node_where_path_is.content.append(new)
self.available_size -= new.size
def direct_remove(self, remove_from, node_to_be_removed):
remove_from.content.remove(node_to_be_removed)
if not node_to_be_removed.is_directory:
self.available_size += node_to_be_removed.size
else:
self.available_size += 1
for node in node_to_be_removed.content:
self.direct_remove(node_to_be_removed, node)
def remove(self, path, directory=False, force=True):
node_where_path_is = self.parent_path(path)
node_to_be_removed = node_where_path_is.dir_get_node(path)
if not node_to_be_removed:
raise NodeDoesNotExistError
if not node_to_be_removed.is_directory:
self.direct_remove(node_where_path_is, node_to_be_removed)
elif not directory:
raise NonExplicitDirectoryDeletionError
elif node_to_be_removed.content and not force:
raise NonEmptyDirectoryDeletionError
else:
self.direct_remove(node_where_path_is, node_to_be_removed)
def move(self, source, destination):
try:
source_parent = self.parent_path(source)
except NodeDoesNotExistError:
raise SourceNodeDoesNotExistError
source_node = source_parent.dir_get_node(source)
self.direct_remove(source_parent, source_node)
try:
destination_parent = self.parent_path(destination)
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError
destination_node = destination_parent.dir_get_node(destination)
source_own_name = source_node.name.split('/')[-1]
source_new_name = destination_node.name + ('/' + source_own_name)
if not destination_node.is_directory:
raise DestinationNotADirectoryError(destination)
elif destination_node.dir_get_node(source_new_name):
raise DestinationNodeExistsError
else:
source_node.name = source_new_name
destination_node.content.append(source_node)
def mount(self, file_system, path):
try:
point_parent = self.parent_path(path)
except NodeDoesNotExistError:
raise MountPointDoesNotExistError
mount_point = point_parent.dir_get_node(path)
if not mount_point.is_directory:
raise MountPointNotADirectoryError
elif mount_point.content:
raise MountPointNotEmptyError
mount_point.content = file_system.root.content
mount_point.is_mount_point = True
def unmount(self, path):
mounted_point = self.get_node(path)
if not mounted_point.is_mount_point:
raise NotAMountPointError
mounted_point.is_mount_point = False
mounted_point.content = []

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

..EEEEE.EEEE.EEEEE
======================================================================
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_out_of_space (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_file (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 28.152s

FAILED (errors=14)

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

Евгения обнови решението на 30.04.2015 16:56 (преди над 9 години)

+class File():
+ def __init__(self, name, content):
+ self.size = len(content) + 1
+ self.name = name
+ self.content = content
+ self.is_directory = False
+
+ def append(self, text):
+ self.content += text
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(text) + 1
+
+
+class Directory():
+ def __init__(self, name):
+ self.size = 1
+ self.name = name
+ self.content = []
+ self.is_directory = True
+ self.is_mount_point = False
+
+ def directories(self):
+ return [obj for obj in self.content if obj.is_directory]
+
+ def files(self):
+ return [obj for obj in self.content if not obj.is_directory]
+
+ def nodes(self):
+ return self.content
+
+ def __contains__(self, node):
+ return node in self.content
+
+ def dir_get_node(self, path):
+ for node in self.content:
+ if node.name == path:
+ return node
+ return False
+
+
+class FileSystemError(Exception):
+ def __init__(self):
+ self.message = "Something in your file system went wrong!"
+
+
+class FileSystemMountError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+ self.message = "Something in the mounting went wrong!"
+
+
+class NodeDoesNotExistError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ super().__init__()
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ super().__init__()
+
+
+class NotEnoughSpaceError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+
+
+class DestinationNodeExistsError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ def __init__(self):
+ super().__init__()
+
+
+class DirectoryHardLinkError(FileSystemError):
+ def __init__(self, directory):
+ super().__init__()
+ self.issuer = directory
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+
+
+class NotAMountPointError(FileSystemMountError):
+ def __init__(self):
+ super().__init__()
+
+
+class FileSystem():
+ def __init__(self, size_in_bytes):
+ self.size = size_in_bytes
+ self.available_size = size_in_bytes - 1
+ self.root = Directory('/')
+
+ def parent_path(self, path):
+ nodes_in_path = path.split('/')
+ current_node = self.root
+ searching_for_path = '/' + nodes_in_path[0]
+ for node in nodes_in_path[2:]:
+ next_node = current_node.dir_get_node(searching_for_path)
+ if next_node:
+ searching_for_path += ('/' + node)
+ current_node = next_node
+ else:
+ raise NodeDoesNotExistError
+ return current_node
+
+ def get_node(self, path):
+ node_where_path_is = self.parent_path(path)
+ return node_where_path_is.dir_get_node(path)
+
+ def create(self, path, directory=False, content=''):
+ try:
+ node_where_path_is = self.parent_path(path)
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError
+
+ if node_where_path_is.dir_get_node(path):
+ raise DestinationNodeExistsError
+
+ if directory:
+ new = Directory(path)
+ if self.available_size < 1:
+ raise NotEnoughSpaceError
+ else:
+ new = File(path, content)
+ if self.available_size < new.size:
+ raise NotEnoughSpaceError
+
+ node_where_path_is.content.append(new)
+ self.available_size -= new.size
+
+ def direct_remove(self, remove_from, node_to_be_removed):
+ remove_from.content.remove(node_to_be_removed)
+ if not node_to_be_removed.is_directory:
+ self.available_size += node_to_be_removed.size
+ else:
+ self.available_size += 1
+ for node in node_to_be_removed.content:
+ self.direct_remove(node_to_be_removed, node)
+
+ def remove(self, path, directory=False, force=True):
+ node_where_path_is = self.parent_path(path)
+ node_to_be_removed = node_where_path_is.dir_get_node(path)
+
+ if not node_to_be_removed:
+ raise NodeDoesNotExistError
+
+ if not node_to_be_removed.is_directory:
+ self.direct_remove(node_where_path_is, node_to_be_removed)
+ elif not directory:
+ raise NonExplicitDirectoryDeletionError
+ elif node_to_be_removed.content and not force:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ self.direct_remove(node_where_path_is, node_to_be_removed)
+
+ def move(self, source, destination):
+ try:
+ source_parent = self.parent_path(source)
+ except NodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError
+
+ source_node = source_parent.dir_get_node(source)
+ self.direct_remove(source_parent, source_node)
+
+ try:
+ destination_parent = self.parent_path(destination)
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError
+
+ destination_node = destination_parent.dir_get_node(destination)
+
+ source_own_name = source_node.name.split('/')[-1]
+ source_new_name = destination_node.name + ('/' + source_own_name)
+
+ if not destination_node.is_directory:
+ raise DestinationNotADirectoryError(destination)
+ elif destination_node.dir_get_node(source_new_name):
+ raise DestinationNodeExistsError
+ else:
+ source_node.name = source_new_name
+ destination_node.content.append(source_node)
+
+ def mount(self, file_system, path):
+ try:
+ point_parent = self.parent_path(path)
+ except NodeDoesNotExistError:
+ raise MountPointDoesNotExistError
+
+ mount_point = point_parent.dir_get_node(path)
+
+ if not mount_point.is_directory:
+ raise MountPointNotADirectoryError
+ elif mount_point.content:
+ raise MountPointNotEmptyError
+
+ mount_point.content = file_system.root.content
+ mount_point.is_mount_point = True
+
+ def unmount(self, path):
+ mounted_point = self.get_node(path)
+ if not mounted_point.is_mount_point:
+ raise NotAMountPointError
+
+ mounted_point.is_mount_point = False
+ mounted_point.content = []