Стилиян обнови решението на 30.04.2015 16:51 (преди над 9 години)
+class FileSystemError(Exception):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemError):
+ pass
+
+
+class NotAMountpointError(FileSystemError):
+ pass
+
+
+class Directory:
+
+ def __init__(self, name):
+ self.name = name
+ self.directories, self.files = [], []
+ self.nodes = [self.files, self.directories]
+ self.is_directory = True
+ self.mount_points = {}
+
+ def __str__(self):
+ return str(self.name)
+
+ def add(self, path, directories=False, content=''):
+ path_parts = path.partition('/')
+ if '/' in path:
+ sub_dir = self.get_directory(path_parts[0])
+ sub_dir.add(path_parts[2], directories, content)
+ else:
+ self.create(path_parts[0], directories, content)
+
+ def get_node(self, path):
+ if '/' in path:
+ path_parts = path.partition('/')
+ sub_dir = self.get_directory(path_parts[0])
+ if not sub_dir:
+ raise NodeDoesNotExistError
+ return sub_dir.get_node(path_parts[2])
+ else:
+ node = self.get(path)
+ if not node:
+ raise NodeDoesNotExistError
+ else:
+ return node
+
+ def create(self, file_name, is_dir=False, content=''):
+ if is_dir:
+ self.directories.append(Directory(file_name))
+ else:
+ self.files.append(File(file_name, content))
+
+ def get(self, name):
+ if name in self.mount_points:
+ return self.mount_points[name]
+ check_dirs = self.get_directory(name)
+ if check_dirs:
+ return check_dirs
+ check_files = self.get_file(name)
+ if check_files:
+ return check_files
+ else:
+ return None
+
+ def get_directory(self, name):
+ for directory in self.directories:
+ if directory.name == name:
+ return directory
+
+ def get_file(self, name):
+ for file_ in self.files:
+ if file_.name == name:
+ return file_
+
+ def insert_file(self, node):
+ if node.is_directory:
+ self.directories.append(node)
+ else:
+ self.files.append(node)
+
+ def remove_file(self, file):
+ if file.is_directory:
+ self.directories.remove(file)
+ else:
+ self.files.remove(file)
+
+ def get_file_parent(self, path):
+ path_to_parent = path.split('/')
+ if len(path_to_parent) == 2:
+ return self
+ del(path_to_parent[-1])
+ path_to_parent = ('/').join(path_to_parent)
+ return self.get_node(path_to_parent[1:])
+
+ def is_empty(self):
+ return len(self.directories) == 0 and len(self.files) == 0
+
+ def has_files(self):
+ return not self.is_empty()
+
+
+class File:
+
+ def __init__(self, name, content):
+ self.name = name
+ self.content = content
+ self.content_length = len(content)
+ self.is_directory = False
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.root = Directory('/')
+ self.size, self.available_size = size, size - 1
+
+ def __str__(self):
+ return str(self.name)
+
+ def get_node(self, path):
+ if path == '/':
+ return self.root
+ else:
+ return self.root.get_node(path.partition('/')[2])
+
+ def remove(self, path, directory=False, force=True):
+ try:
+ parent = self.root.get_file_parent(path)
+ to_be_deleted = self.get_node(path)
+ except NodeDoesNotExistError:
+ raise
+ else:
+ if to_be_deleted.is_directory:
+ if not directory:
+ raise NonExplicitDirectoryDeletionError
+ if to_be_deleted.has_files() and not force:
+ raise NonEmptyDirectoryDeletionError
+ parent.remove_file(to_be_deleted)
+ else:
+ parent.remove_file(to_be_deleted)
+
+ def move(self, source, destination):
+ try:
+ src = self.get_node(source)
+ except NodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError
+ try:
+ dst = self.get_node(destination)
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError
+ if not dst.is_directory:
+ raise DestinationNotADirectoryError
+ if dst.get(source.split('/')[-1]):
+ raise DestinationNodeExistsError
+ else:
+ parent = self.root.get_file_parent(source)
+ parent.remove_file(src)
+ dst.insert_file(src)
+
+ def link(self, source, destination, symbolic=True):
+ pass
+
+ def mount(self, file_system, path):
+ try:
+ source = self.get_node(path)
+ except NodeDoesNotExist:
+ raise MountPointDoesNotExistError
+ if not source.is_directory:
+ raise MountPointNotADirectoryError
+ if source.has_files():
+ raise MountPointNotEmptyError
+ parent = self.root.get_file_parent(path)
+ root_of_fs = file_system.root
+ parent.mount_points[source.name] = root_of_fs
+ parent.remove_file(source)
+ parent.insert_file(root_of_fs)
+
+ def unmount(self, path):
+ try:
+ unmoint_point = self.get_node(path)
+ except DestinationNodeExistsError:
+ raise
+ else:
+ parent = self.root.get_file_parent(path)
+ dir_name = path.split('/')[-1]
+ if dir_name not in parent.mount_points:
+ raise NotAMountpointError
+ else:
+ parent.create(dir_name, is_dir=True)
+ del parent.mount_points[dir_name]
+
+ def create(self, path, directory=False, content=''):
+ space_needed = len(content) + 1
+ if self.available_size - space_needed < 0:
+ raise NotEnoughSpaceError
+ try:
+ parent = self.root.get_file_parent(path)
+ if not parent:
+ raise DestinationNodeDoesNotExistError
+ if parent.get(path.split('/')[-1]):
+ raise DestinationNodeExistsError
+ self.root.add(path.partition('/')[2], directory, content)
+ except FileSystemError:
+ raise
+ else:
+ self.available_size -= space_needed