Цветан обнови решението на 30.04.2015 14:04 (преди над 9 години)
+class Node():
+
+ def __init__(self, is_directory, parent=""):
+ self.is_directory = is_directory
+ self.parent = parent
+
+
+class File(Node):
+
+ def __init__(self, path, content, parent=""):
+ Node.__init__(self, False, parent)
+ self.content = content
+ self.path = path
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+ @property
+ def size(self):
+ return len(self.content) + 1
+
+
+class Directory(Node):
+
+ def __init__(self, path, parent=""):
+ Node.__init__(self, True, parent)
+ self.path = path
+ self.directories = {}
+ self.files = {}
+
+ @property
+ def nodes(self):
+ return dict(self.files, **self.directories)
+
+ @property
+ def size(self):
+ return 1
+
+class FileSystemError(Exception):
+
+ def __init__(self):
+ self.message = 'FileSystemError'
+
+class NodeDoesNotExistError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NodeDoesNotExistError'
+
+
+class DestinationNotADirectoryError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'DestinationNotADirectoryError'
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'DestinationNodeDoesNotExistError'
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'SourceNodeDoesNotExistError'
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NonEmptyDirectoryDeletionError'
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NonExplicitDirectoryDeletionError'
+
+
+class NotEnoughSpaceError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'Not Enough Space Error'
+
+
+class DestinationNodeExistsError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'Node exists'
+
+
+class DestinationNodeDoesNotExistError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'Destination node does not exist'
+
+
+class FileSystem():
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.nodes = {}
+ self.nodes["/"] = Directory("/")
+
+ if self.available_size < 0:
+ raise NotEnoughSpaceError
+
+ def get_node(self, path):
+ if path not in self.nodes:
+ raise NodeDoesNotExistError
+
+ return self.nodes[path]
+
+ def create(self, path, directory=False, content=''):
+ if path in self.nodes:
+ raise DestinationNodeExistsError
+ if self.available_size < len(content) + 1:
+ raise NotEnoughSpaceError
+
+ parent_path = path[0: path.rindex('/')]
+ if len(parent_path) == 0:
+ parent_path = '/'
+
+ if parent_path not in self.nodes:
+ raise DestinationNodeDoesNotExistError
+
+ if directory:
+ self.nodes[path] = Directory(path, self.nodes[parent_path])
+ self.nodes[parent_path].directories[path] = self.nodes[path]
+ else:
+ self.nodes[path] = File(path, content, self.nodes[parent_path])
+ self.nodes[parent_path].files[path] = self.nodes[path]
+
+ self.available_size -= len(content) + 1
+
+ def remove(self, path, directory=False, force=True):
+ if path not in self.nodes:
+ raise NodeDoesNotExistError
+ if not directory and self.nodes[path].is_directory:
+ raise NonExplicitDirectoryDeletionError
+ if directory and len(self.nodes[path].nodes) > 0 and not force:
+ raise NonEmptyDirectoryDeletionError
+
+ self.available_size += self.nodes[path].size
+
+ if directory:
+ del self.nodes[path].parent.directories[path]
+ else:
+ del self.nodes[path].parent.files[path]
+
+ del self.nodes[path]
+
+ def move(self, source, destination):
+ if source not in self.nodes:
+ raise SourceNodeDoesNotExistError
+
+ if destination not in self.nodes:
+ raise DestinationNodeDoesNotExistError
+
+ if not self.nodes[destination].is_directory:
+ raise DestinationNotADirectoryError
+
+
+ def link(self, source, destination, symbolic=True):
+ return
+
+ def mount(self, file_system, path):
+ return
+
+ def unmount(self, path):
+ return