Решение на In-memory файлова система от Цветелина Александрова

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

Към профила на Цветелина Александрова

Резултати

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

Код

class FileSystemError(Exception):
def __init__(self, message=''):
Exception.__init__(self, "A FileSystem error occured! " + message)
class NotEnoughSpaceError(FileSystemError):
def __init__(self, message=''):
Exception.__init__(self, "There is not enough space! " + message)
class NodeDoesNotExistError(FileSystemError):
def __init__(self, message=''):
Exception.__init__(
self, "Node not found in the file system! " + message)
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self, message=''):
Exception.__init__(self, "Source node does not exist! " + message)
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self, message=''):
Exception.__init__(self, "Destination node does not exist! " + message)
class DestinationNodeExistsError(NodeDoesNotExistError):
def __init__(self, message=''):
Exception.__init__(self, "Destination node already exists! " + message)
class FileSystemMountError(FileSystemError):
def __init__(self):
Exception.__init__(self, "A FileSystem error occured while mounting! ")
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self, message=''):
Exception.__init__(self, "Mount point node does not exist! " + message)
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self, message=''):
Exception.__init__(
self, "Mount point should be a directory! " + message)
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self, message=''):
Exception.__init__(self, "Mount point should be empty! " + message)
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.root = Dir('/')
def get_node(self, path):
if self.root.path == path:
return self.root
return self.root.get_child(path)
def create(self, path, directory=False, content=''):
if directory:
content = ''
try:
src = self.get_node(path)
raise DestinationNodeExistsError()
except DestinationNodeExistsError:
raise
except NodeDoesNotExistError:
print("")
parent_dir_path = self.get_parent_path(path)
try:
parent_dir = self.get_node(parent_dir_path)
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError()
if directory:
new_node = Dir(path)
else:
new_node = File(path, content)
if self.available_size - new_node.size() < 0:
raise NotEnoughSpaceError()
self.available_size -= new_node.size()
if directory:
parent_dir.add_sub_dir(new_node)
else:
parent_dir.add_sub_file(new_node)
def remove(self, path, directory=False, force=True):
node = self.get_node(path)
if node.is_directory:
if not directory:
raise NonExplicitDirectoryDeletionError()
if node.get_nodes():
if not force:
raise NonEmptyDirectoryDeletionError()
self.nodes.remove(node)
def move(self, source, destination):
try:
source_node = self.get_node(source)
except NodeDoesNotExistError:
raise SourceNodeDoesNotExistError()
try:
dest_node = self.get_node(destination)
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError()
if not dest_node.is_directory:
raise DestinationNotADirectoryError()
new_dest_path = destination + '/' + source.split("/")[-1]
try:
self.get_node(new_dest_path)
raise DestinationNodeExistsError()
except NodeDoesNotExistError:
print("")
source_node.set_path(new_dest_path)
parent_dir_path = self.get_parent_path(source)
source_old_parent = self.get_node(parent_dir_path)
source_old_parent.nodes.remove(source_node)
if source_node.is_directory:
dest_node.add_sub_dir(source_node)
else:
dest_node.add_sub_file(source_node)
def get_parent_path(self, path):
parent_dir_path = "/".join(path.split("/")[:-1])
if len(parent_dir_path) == 0 or not parent_dir_path[0] == '/':
parent_dir_path = "/" + parent_dir_path
return parent_dir_path
def link(self, source, destination, symbolic=True):
try:
source_node = self.get_node(source)
except NodeDoesNotExistError as e:
if not symbolic:
raise SourceNodeDoesNotExistError()
raise e
if source_node.is_directory and not symbolic:
raise DirectoryHardLinkError()
def mount(self, file_system, path):
try:
point = self.get_node(path)
except NodeDoesNotExistError:
raise MountPointDoesNotExistError()
if not point.is_directory:
raise MountPointNotADirectoryError()
if point.nodes:
raise MountPointNotEmptyError()
def unmount(self, path):
point = self.get_node(path)
if not point.has_mounted:
raise NotAMountpointError()
class Node:
def __init__(self, path, directory=True):
self.path = path
self.is_directory = directory
def set_path(self, path):
self.path = path
class File(Node):
def __init__(self, path, content=''):
Node.__init__(self, path, False)
self.content = content
def append(self, text):
self.content += text
def truncate(self, text):
self.content = text
def size(self):
return len(self.content) + 1
class Dir(Node):
def __init__(self, path):
Node.__init__(self, path)
self.directories = []
self.files = []
self.nodes = []
self.has_mounted = False
def add_sub_dir(self, dir):
self.nodes.append(dir)
self.directories.append(dir)
def add_sub_file(self, file):
self.files.append(file)
self.nodes.append(file)
def get_child(self, path):
for n in self.nodes:
if n.path == path:
return n
if n.is_directory:
sub_ch = n.get_child(path)
if sub_ch:
return sub_ch
raise NodeDoesNotExistError()
def size(self):
return 1

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

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

======================================================================
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_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_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 26.152s

FAILED (errors=13)

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

Цветелина обнови решението на 30.04.2015 14:29 (преди почти 9 години)

+class FileSystemError(Exception):
+
+ def __init__(self, message=''):
+ Exception.__init__(self, "A FileSystem error occured! " + message)
+
+
+class NotEnoughSpaceError(FileSystemError):
+
+ def __init__(self, message=''):
+ Exception.__init__(self, "There is not enough space! " + message)
+
+
+class NodeDoesNotExistError(FileSystemError):
+
+ def __init__(self, message=''):
+ Exception.__init__(
+ self, "Node not found in the file system! " + message)
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self, message=''):
+ Exception.__init__(self, "Source node does not exist! " + message)
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self, message=''):
+ Exception.__init__(self, "Destination node does not exist! " + message)
+
+
+class DestinationNodeExistsError(NodeDoesNotExistError):
+
+ def __init__(self, message=''):
+ Exception.__init__(self, "Destination node already exists! " + message)
+
+
+class FileSystemMountError(FileSystemError):
+
+ def __init__(self):
+ Exception.__init__(self, "A FileSystem error occured while mounting! ")
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+
+ def __init__(self, message=''):
+ Exception.__init__(self, "Mount point node does not exist! " + message)
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+
+ def __init__(self, message=''):
+ Exception.__init__(
+ self, "Mount point should be a directory! " + message)
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+
+ def __init__(self, message=''):
+ Exception.__init__(self, "Mount point should be empty! " + message)
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.root = Dir('/')
+
+ def get_node(self, path):
+ if self.root.path == path:
+ return self.root
+ return self.root.get_child(path)
+
+ def create(self, path, directory=False, content=''):
+ if directory:
+ content = ''
+ try:
+ src = self.get_node(path)
+ raise DestinationNodeExistsError()
+ except DestinationNodeExistsError:
+ raise
+ except NodeDoesNotExistError:
+ print("")
+ parent_dir_path = self.get_parent_path(path)
+ try:
+ parent_dir = self.get_node(parent_dir_path)
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError()
+ if directory:
+ new_node = Dir(path)
+ else:
+ new_node = File(path, content)
+ if self.available_size - new_node.size() < 0:
+ raise NotEnoughSpaceError()
+ self.available_size -= new_node.size()
+ if directory:
+ parent_dir.add_sub_dir(new_node)
+ else:
+ parent_dir.add_sub_file(new_node)
+
+ def remove(self, path, directory=False, force=True):
+ node = self.get_node(path)
+ if node.is_directory:
+ if not directory:
+ raise NonExplicitDirectoryDeletionError()
+ if node.get_nodes():
+ if not force:
+ raise NonEmptyDirectoryDeletionError()
+ self.nodes.remove(node)
+
+ def move(self, source, destination):
+ try:
+ source_node = self.get_node(source)
+ except NodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError()
+ try:
+ dest_node = self.get_node(destination)
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError()
+ if not dest_node.is_directory:
+ raise DestinationNotADirectoryError()
+ new_dest_path = destination + '/' + source.split("/")[-1]
+ try:
+ self.get_node(new_dest_path)
+ raise DestinationNodeExistsError()
+ except NodeDoesNotExistError:
+ print("")
+ source_node.set_path(new_dest_path)
+ parent_dir_path = self.get_parent_path(source)
+ source_old_parent = self.get_node(parent_dir_path)
+ source_old_parent.nodes.remove(source_node)
+ if source_node.is_directory:
+ dest_node.add_sub_dir(source_node)
+ else:
+ dest_node.add_sub_file(source_node)
+
+ def get_parent_path(self, path):
+ parent_dir_path = "/".join(path.split("/")[:-1])
+ if len(parent_dir_path) == 0 or not parent_dir_path[0] == '/':
+ parent_dir_path = "/" + parent_dir_path
+ return parent_dir_path
+
+ def link(self, source, destination, symbolic=True):
+ try:
+ source_node = self.get_node(source)
+ except NodeDoesNotExistError as e:
+ if not symbolic:
+ raise SourceNodeDoesNotExistError()
+ raise e
+ if source_node.is_directory and not symbolic:
+ raise DirectoryHardLinkError()
+
+ def mount(self, file_system, path):
+ try:
+ point = self.get_node(path)
+ except NodeDoesNotExistError:
+ raise MountPointDoesNotExistError()
+ if not point.is_directory:
+ raise MountPointNotADirectoryError()
+ if point.nodes:
+ raise MountPointNotEmptyError()
+
+ def unmount(self, path):
+ point = self.get_node(path)
+ if not point.has_mounted:
+ raise NotAMountpointError()
+
+
+class Node:
+
+ def __init__(self, path, directory=True):
+ self.path = path
+ self.is_directory = directory
+
+ def set_path(self, path):
+ self.path = path
+
+
+class File(Node):
+
+ def __init__(self, path, content=''):
+ Node.__init__(self, path, False)
+ self.content = content
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+ def size(self):
+ return len(self.content) + 1
+
+
+class Dir(Node):
+
+ def __init__(self, path):
+ Node.__init__(self, path)
+ self.directories = []
+ self.files = []
+ self.nodes = []
+ self.has_mounted = False
+
+ def add_sub_dir(self, dir):
+ self.nodes.append(dir)
+ self.directories.append(dir)
+
+ def add_sub_file(self, file):
+ self.files.append(file)
+ self.nodes.append(file)
+
+ def get_child(self, path):
+ for n in self.nodes:
+ if n.path == path:
+ return n
+ if n.is_directory:
+ sub_ch = n.get_child(path)
+ if sub_ch:
+ return sub_ch
+ raise NodeDoesNotExistError()
+
+ def size(self):
+ return 1