Николай обнови решението на 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