Гален обнови решението на 30.04.2015 02:46 (преди над 9 години)
+class Directory:
+ def __init__(self, name_):
+ self.name = name_
+ self.directories = []
+ self.files = []
+ self.nodes = []
+ self.size = 1
+ self.is_directory = True
+
+ def add_directory(self, dir_):
+ self.directories.append(dir_)
+
+ def add_file(self, file_):
+ self.files.append(file_)
+
+ def add_node(self, object_):
+ self.nodes.append(object_)
+
+
+class File:
+ def __init__(self, name_, content_):
+ self.content = content_
+ self.name = name_
+ self.size = len(content_) + 1
+ self.is_directory = False
+
+ def append(self, text):
+ self.content.append(text)
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(text) + 1
+
+
+class DestinationNodeDoesNotExistError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class NotEnoughSpaceError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class NodeDoesNotExistError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class SourceNodeDoesNotExistError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class DestinationNodeExistsError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class NonExplicitDirectoryDeletionError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class NonEmptyDirectoryDeletionError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class FileSystemMountError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class DirectoryHardLinkError(Exception):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class NodeDoesNotExistError(FileSystemMountError):
+ def __init__(self, message_):
+ self.message = message_
+
+
+class FileSystem:
+ def __init__(self, size_):
+ if size_ < 1:
+ raise NotEnoughSpaceError("Exception")
+ self.size = size_
+ self.available_size = size_ - 1
+ self.home = Directory('')
+
+ def get_node(self, path):
+ cur_directory = self.home
+ if path == "/":
+ return cur_directory
+
+ paths = path.split("/")
+
+ pos = 1
+ cur_directory = self.home
+
+ while pos < len(paths):
+ find = False
+ for directory in cur_directory.nodes:
+ if paths[pos] == directory.name:
+ cur_directory = directory
+ find = True
+ break
+ if find is False:
+ raise DestinationNodeDoesNotExistError("Exception")
+
+ pos += 1
+
+ return cur_directory
+
+ def create(self, path, directory=False, content=''):
+ paths = path.split("/")
+
+ if directory is False:
+ if len(content) + 1 > self.available_size:
+ raise NotEnoughSpaceError("Exception")
+ else:
+ self.available_size -= len(content) + 1
+ else:
+ if self.available_size < 1:
+ raise NotEnoughSpaceError("Exception")
+ else:
+ self.available_size -= 1
+
+ if self.path_exist(path):
+ raise DestinationNodeExistsError("Exception")
+
+ try:
+ cur_directory = self.get_directory(path)
+
+ if directory is False:
+ file_ = File(paths[-1], content)
+ cur_directory.add_file(file_)
+ cur_directory.add_node(file_)
+ else:
+ directory_ = Directory(paths[-1])
+ cur_directory.add_directory(directory_)
+ cur_directory.add_node(directory_)
+ except DestinationNodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError("Exception")
+
+ def remove(self, path, directory=False, force=True):
+ try:
+ cur_directory = self.get_node(path)
+ last_directory = self.get_directory(path)
+
+ if cur_directory is True and directory is False:
+ raise NonExplicitDirectoryDeletionError("Exception")
+
+ if cur_directory.is_directory is True and force is False and len(cur_directory.nodes) > 0:
+ raise NonEmptyDirectoryDeletionError("Exception")
+
+ self.available_size += cur_directory.size
+
+ if directory is True:
+ self.remove_element_with_name
+ (last_directory.directories, cur_directory.name)
+ else:
+ self.remove_element_with_name
+ (last_directory.files, cur_directory.name)
+ self.remove_element_with_name
+ (last_directory.nodes, cur_directory.name)
+ del source_
+
+ except DestinationNodeDoesNotExistError:
+ raise NodeDoesNotExistError("Exception")
+
+ def move(self, source, destination):
+ source_ = {}
+ destination_ = {}
+ last_directory = {}
+
+ try:
+ source_ = self.get_node(source)
+ except DestinationNodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError("Exception")
+
+ try:
+ destination_ = self.get_node(destination)
+ if destination_.is_directory is False:
+ raise DestinationNotADirectoryError("Exception")
+ elif self.directory_exist(destination_.directories, source_.name) is True:
+ raise DestinationNodeExistsError("Exception")
+ except DestinationNodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError("Exception")
+
+ try:
+ last_directory = self.get_directory(source)
+
+ if source_.is_directory is True:
+ destination_.add_directory(source_)
+ self.remove_element_with_name
+ (last_directory.directories, source_.name)
+ else:
+ destination_.add_file(source_)
+ self.remove_element_with_name
+ (last_directory.files, source_.name)
+ destination_.add_node(source_)
+ self.remove_element_with_name(last_directory.nodes, source_.name)
+ del source_
+ except DestinationNodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError("Exception")
+
+ def link(self, source, destination, symbolic=True):
+ source_ = {}
+ destination_ = self.create_path(destination)
+
+ if self.path_exist(source) is False:
+ if symbolic is False:
+ raise NodeDoesNotExistError("Exception")
+ else:
+ destination_.add_node(source_)
+
+ if symbolic is False and source_.is_directory is True:
+ raise DirectoryHardLinkError("Exception")
+
+ if symbolic is True:
+ if source_.is_directory is True:
+ destination_.add_directory(source_)
+ destination_.add_node(source_)
+ else:
+ destination_.add_file(source_)
+ destination_.add_node(source_)
+ else:
+ if source_.is_directory is False:
+ self.available_size -= 1
+ new_file = File(source.split("/")[-1], source_.content)
+ destination_.add_file(new_file)
+ destination_.add_node(new_file)
+ else:
+ new_directory = Directory(source.split("/")[-1])
+ destination_.add_directory(new_directory)
+ destination_.add_node(new_directory)
+
+ def mount(self, file_system, path):
+ source_ = {}
+ last_directory = {}
+
+ try:
+ source_ = self.get_node(source)
+ last_directory = self.get_directory(source)
+ except DestinationNodeDoesNotExistError:
+ raise MountPointDoesNotExistError("Exception")
+
+ if source_.is_directory is False:
+ raise MountPointNotADirectoryError("Exception")
+
+ if len(source_.nodes) > len:
+ raise MountPointNotEmptyError("Exception")
+
+ last_directory.add_node(file_system)
+
+ def unmount(self, path):
+ source_ = {}
+ last_directory = {}
+
+ try:
+ source_ = self.get_node(source)
+ last_directory = self.get_directory(source)
+ except DestinationNodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError("Exception")
+
+ for node in last_directory.nodes:
+ if isinstance(node, FileSystem):
+ last_directory.nodes.remove(node)
+ del source
+
+ def remove_element_with_name(self, array, name):
+ for node in array:
+ if node.name == name:
+ array.remove(node)
+ break
+
+ def directory_exist(self, array, name):
+ for node in array:
+ if node.name == name:
+ return True
+ return False
+
+ def path_exist(self, path):
+ paths = path.split("/")
+
+ pos = 1
+ cur_directory = self.home
+
+ while pos < len(paths):
+ find = False
+ for node in cur_directory.nodes:
+ if paths[pos] == node.name:
+ cur_directory = node
+ find = True
+ break
+ if find is False:
+ return False
+ pos += 1
+ return True
+
+ def get_directory(self, path):
+ paths = path.split("/")
+
+ pos = 1
+ cur_directory = self.home
+
+ while pos < len(paths) - 1:
+ find = False
+ for directory in cur_directory.directories:
+ if paths[pos] == directory.name:
+ cur_directory = directory
+ find = True
+ break
+
+ if find is False:
+ raise DestinationNodeDoesNotExistError("Exception")
+ pos += 1
+
+ return cur_directory
+
+ def create_path(self, path):
+ if path == "/":
+ return self.home
+
+ paths = path.split("/")
+
+ pos = 1
+ cur_directory = self.home
+
+ while pos < len(paths):
+ find = False
+ for node in cur_directory.directories:
+ if paths[pos] == node.name:
+ cur_directory = node
+ find = True
+ break
+
+ if find is False:
+ new_directory = Directory(paths[pos])
+ cur_directory.add_directory(new_directory)
+ cur_directory.add_node(new_directory)
+ cur_directory = new_directory
+
+ pos += 1
+
+ return cur_directory