Любослав обнови решението на 30.04.2015 15:45 (преди над 9 години)
+class FileSystemError(Exception):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class FileSystem:
+ def __init__(self, size_in_bytes):
+ self.__SIZE_OF_DIR = 1
+ self.__size = size_in_bytes
+ self.__available_size = size_in_bytes - self.__SIZE_OF_DIR
+ self.__root = Directory("/")
+
+ def get_node(self, path):
+ if len(path) > 0 and path[0] is not "/":
+ raise NodeDoesNotExistError()
+ elif path == "/":
+ return self.__root
+ else:
+ node = self.__root
+ current_path = path[1:]
+ while "/" in current_path:
+ index = current_path.find("/")
+ current_dir = current_path[:index]
+ current_path = current_path[index + 1:]
+
+ node = FileSystem.__find_node(current_dir, node)
+
+ return self.__find_node(current_path, node)
+
+ def create(self, path, directory=False, content=""):
+ parent_path = path[:path.rfind("/")]
+
+ try:
+ parent = self.get_node(parent_path if parent_path != "" else "/")
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError("The path to the file "
+ "does not exist.")
+ if parent.is_directory:
+ file_size = (len(content) if not directory else 0) + 1
+ if self.__available_size < file_size:
+ raise NotEnoughSpaceError
+
+ new_file_name = path[path.rfind("/") + 1:]
+
+ if new_file_name in parent.nodes:
+ raise Exception
+ try:
+ self.__find_node(new_file_name, parent)
+ except NodeDoesNotExistError:
+ if directory:
+ parent.add_directory(new_file_name)
+ else:
+ parent.add_file(new_file_name, content)
+
+ self.__available_size -= file_size # check if not negative
+ else:
+ raise DestinationNodeExistsError
+
+ else:
+ raise DestinationNodeDoesNotExistError("The path to the file "
+ "does not exist.")
+
+ def remove(self, path, directory=False, force=True):
+ node = self.get_node(FileSystem.__get_parent(path))
+ name = FileSystem.__get_filename(node)
+
+ if not directory:
+ if not node.is_directory:
+ node.remove_file(name)
+ else:
+ pass # the node is said not to be directory but it is a
+ # directory
+ else:
+ try:
+ if self.__is_empty_dir(node):
+ pass # delete dir
+ else:
+ if force:
+ pass # delete recursively
+ else:
+ raise NonEmptyDirectoryDeletionError
+
+ except FileSystemError as e:
+ pass # the node is said to be a directory but it is a file
+
+ def move(self, source, destination):
+ pass
+
+ def link(self, source, destination, symbolic=True):
+ pass
+
+ def mount(self, file_system, path):
+ pass
+
+ def unmount(self, path):
+ pass
+
+ @staticmethod
+ def __find_node(node_name, directory):
+ if node_name in directory.nodes:
+ return directory.nodes[node_name]
+
+ raise NodeDoesNotExistError
+
+ def __is_empty_dir(self, path):
+ node = self.get_node(path)
+
+ if node.is_directory:
+ return not node.nodes
+ else:
+ raise FileSystemError
+
+ @staticmethod
+ def __get_parent(path):
+ return path[:path.rfind("/")]
+
+ @staticmethod
+ def __get_filename(path):
+ return path[path.rfing("/") + 1:]
+
+
+class Directory:
+ def __init__(self, name):
+ self.name = name
+ self.files = {}
+ self.directories = {}
+ self.nodes = {}
+ self.is_directory = True
+
+ def add_directory(self, name):
+ if self.is_available(name):
+ self.directories.update({name: Directory(name)})
+ self.__update_nodes()
+ else:
+ raise Exception
+
+ def add_file(self, name, content):
+ if self.is_available(name):
+ self.files.update({name: File(name, content)})
+ self.__update_nodes()
+ else:
+ raise Exception
+
+ def remove_file(self, name):
+ self.files.pop(name, None)
+
+ def __update_nodes(self):
+ self.nodes.update(self.directories)
+ self.nodes.update(self.files)
+
+ def is_available(self, name):
+ return name not in self.nodes.keys()
+
+
+class File:
+ def __init__(self, name, content):
+ self.name = name
+ self.content = content
+ self.size = len(content) + 1
+ self.is_directory = False
+
+ def append(self, text):
+ self.__set_content(self.content + text)
+
+ def truncate(self, text):
+ self.__set_content(text)
+
+ def __set_content(self, text):
+ self.content = text
+ self.__update_size()
+
+ def __update_size(self):
+ self.size = len(self.content) + 1