Диана обнови решението на 29.04.2015 16:33 (преди над 9 години)
+class FileSystemError(RuntimeError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NotDirectoryError(FileSystemError):
+ pass
+
+
+class Node:
+ def __init__(self, name, content=''):
+ self.content = content
+ self.name = name
+
+
+class File(Node):
+ is_directory = False
+
+ def __init__(self, name, content=''):
+ Node.__init__(self, name, content)
+
+
+class Directory(Node):
+ is_directory = True
+
+ def __init__(self, name):
+ Node.__init__(self, name)
+ self.directories = []
+ self.files = []
+
+ def find_by_path(self, path):
+ if '/' in path:
+ parent_dir = path.split('/', 1)[0]
+ basename = path.split('/', 1)[1]
+ for dir in self.directories:
+ if dir.name == parent_dir:
+ return dir.find_by_path(basename)
+ else:
+ for node in self.nodes:
+ if node.name == path:
+ return node
+ raise NodeDoesNotExistError
+
+ def find_by_name(self, name):
+ for node in self.nodes:
+ if node.name == name:
+ return node
+ return None
+
+ def delete(self, name):
+ node = self.find_by_name(name)
+ if node.is_directory:
+ self.directories.remove(node)
+ else:
+ self.files.remove(node)
+
+ @property
+ def nodes(self):
+ return self.directories + self.files
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.size, self.available_size = size, size - 1
+ self.root = Directory('/')
+
+ def get_node(self, path):
+ if path:
+ return self.root.find_by_path(path[1:])
+ else:
+ return self.root
+
+ def create(self, path, directory=False, content=''):
+ parent_dir = path.rsplit('/', 1)[0]
+ name = path.rsplit('/', 1)[1]
+ size = len(content) + 1
+ try:
+ if(parent_dir):
+ node = self.get_node(parent_dir)
+ else:
+ node = self.root
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError
+ if node.is_directory:
+ if node.find_by_name(name):
+ raise DestinationNodeExistsError
+ else:
+ if self.available_size < size:
+ raise NotEnoughSpaceError
+ else:
+ if(directory):
+ node.directories.append(Directory(name))
+ else:
+ node.files.append(File(name, content))
+ self.available_size -= size
+ else:
+ raise NotDirectoryError
+
+ def remove(self, path, directory=False, force=True):
+ node = self.get_node(path)
+ parent_dir = path.rsplit('/', 1)[0]
+ name = path.rsplit('/', 1)[1]
+ if(node.is_directory):
+ if not directory:
+ raise NonExplicitDirectoryDeletionError
+ else:
+ if node.nodes:
+ if force:
+ self.get_node(parent_dir).delete(name)
+ else:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ self.get_node(parent_dir).delete(name)
+
+ else:
+ self.get_node(parent_dir).delete(name)