Виктор обнови решението на 30.04.2015 16:10 (преди над 9 години)
+class FileSystem:
+
+ """docstring for FileSystem"""
+
+ def __init__(self, input_size):
+ self.__size = input_size
+ self.__available_size = input_size - 1
+ self.__nodes = []
+
+ def size(self):
+ return self.__size
+
+ def available_size(self):
+ return self.__available_size
+
+ def get_node(self, path):
+ searched_path = self.__get_split_path(path)
+ if searched_path == []:
+ return self.__nodes
+
+ searched_nodes = self.__nodes
+ node_found = -1
+ while len(searched_path) > 0:
+ name = searched_path[0]
+ node_found = self.__find_node_by_name(searched_nodes, name)
+ if node_found == -1:
+ raise NodeDoesNotExistError
+
+ searched_path = searched_path[1:]
+ if not node_found.is_directory():
+ if len(searched_path) == 0:
+ return node_found
+ else:
+ # File can't contain any nodes!
+ # Example: '/home/file.txt/dir1' is invalid path!
+ raise NodeDoesNotExistError
+
+ searched_nodes = node_found._nodes
+
+ if node_found == -1:
+ raise NodeDoesNotExistError
+
+ return node_found
+
+ def create(self, path, directory=False, content=''):
+ try:
+ self.get_node(path)
+ raise DestinationNodeExistsError
+ print('YMU')
+ except NodeDoesNotExistError:
+
+ searched_path = self.__get_split_path(path)
+ name = searched_path[-1]
+ new_node = self.__create_new_node(name, directory, content)
+ if directory:
+ new_node_size = 1
+ else:
+ new_node_size = new_node.size()
+
+ if self.__available_size - new_node_size < 0:
+ raise NotEnoughSpaceError
+ else:
+ self.__available_size -= new_node_size # Assigning 'memory'
+
+ if len(searched_path) == 1:
+ # Created directly in the file system nodes.
+ self.__nodes.append(new_node)
+ else:
+ path_to_create = path[0:path.rfind('/')]
+ if self.get_node(path_to_create).is_directory():
+ self.get_node(path_to_create)._nodes.append(new_node)
+ else:
+ raise AttemptingToCreateInFileError
+ # Must raise an error if attempting to create in file.
+
+ def remove(self, path, directory=False, force=True):
+ node_to_remove = self.get_node(path)
+ if node_to_remove.is_directory():
+ if not directory:
+ raise NonExplicitDirectoryDeletionError
+ elif not (node_to_remove._nodes == [] or force):
+ # If directory is not empty and force is False.
+ raise NonEmptyDirectoryDeletionError
+ else:
+ self.__available_size += 1
+ # Detach the node from upper directory.
+ self.get_node(self.__upper_path(path)).remove(node_to_remove)
+ else:
+ self.__available_size += node_to_remove.size()
+ self.get_node(self.__upper_path(path)).remove(node_to_remove)
+
+ # REALLOCATE MEMORY!!! -> Done!
+
+ def move(self, source, destination):
+ try:
+ node_to_move = self.get_node(source)
+ except NodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError
+ except:
+ print('Unknown error! Please keep calm & code more Python!')
+
+ try:
+ destination_node = self.get_node(destination)
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError
+
+ if not destination_node.is_directory():
+ raise DestinationNotADirectoryError
+
+ if not (self.__find_node_by_name(destination_node._nodes,
+ node_to_move.name()) == -1):
+ # If there is already a node with such name in the destination.
+ raise DestinationNodeExistsError
+
+ destination_node._nodes.append(node_to_move)
+ self.get_node(__upper_path(source)).remove(node_to_move)
+
+ def link(self, source, destination, symbolic=True):
+ pass
+ # TO IMPLEMENT
+
+ def mount(self, file_system, path):
+ pass
+ # TO IMPLEMENT
+
+ def unmount(self, path):
+ pass
+ # TO IMPLEMENT
+
+ def __create_new_node(self, name, directory=False, content=''):
+ if directory:
+ new_node = Directory(name)
+ else:
+ new_node = File(name, content)
+
+ new_node._owner = self
+ return new_node
+
+ def __find_node_by_name(self, nodes, name):
+ # Searches for a node in given collection.
+ # Returns -1 if there is none with such name.
+ for node in nodes:
+ if node.name() == name:
+ return node
+ return -1
+
+ def __get_split_path(self, path):
+ # Returns a list of all directories of the given argument.
+ # Since the split gives one '' element in the list, it's not included.
+ # __get_split_path('/home/dir1/dir2') returns [home, dir1, dir2]
+ return path.split('/')[1:]
+
+ def __upper_path(self, path):
+ # Returns the path one level/floor before the given argument.
+ # __upper_path('/home/dir1/dir2') returns '/home/dir1'
+ return path[0:path.rfind('/')]
+
+ ###
+
+
+class Node:
+
+ """ A Node object represents a node from a FileSystem object's content.
+ It 'knows' it's name(str), if it is a directory(bool) and it's
+ owner(reference to a FileSystem object).
+ """
+
+ def __init__(self, name, is_directory):
+ self.__name = name
+ self.__is_directory = is_directory
+ self.__owner = None
+
+ def is_directory(self):
+ return self.__is_directory
+
+ def name(self):
+ return self.__name
+
+ def _get_owner(self):
+ return self.__owner
+
+ def _set_owner(self, file_system):
+ if not type(file_system) is FileSystem:
+ raise NotAValidOwnerError
+
+ self.__owner = file_system
+
+
+class Directory(Node):
+
+ """docstring for Directory"""
+
+ def __init__(self, name):
+ super(Directory, self).__init__(name, True)
+ self._nodes = []
+
+ def directories(self):
+ dirs = []
+ for i in range(0, len(_nodes) - 1):
+ if _nodes[i].is_directory():
+ dirs.append(_nodes[i])
+ return dirs
+
+ def files(self):
+ files = []
+ for i in range(0, len(_nodes) - 1):
+ if not _nodes[i].is_directory():
+ files.append(_nodes[i])
+ return files
+
+ def nodes(self):
+ return self._nodes
+
+
+class File(Node):
+
+ """docstring for File"""
+
+ def __init__(self, name, content):
+ super(File, self).__init__(name, False)
+ self.__content = content
+
+ def append(self, text):
+ if self._owner.available_size < len(text):
+ raise NotEnoughSpaceError
+
+ self.__content += text
+ self._owner.available_size -= len(text)
+
+ def content(self):
+ return self.__content
+
+ def size(self):
+ return len(self.__content) + 1
+
+ def truncate(self, text):
+ if self._owner.available_size - len(self.__content) < len(text):
+ raise NotEnoughSpaceError
+
+ self.__content = text
+
+ ###
+ # Exeptions:
+ ###
+
+
+class FileSystemError(BaseException):
+ # All exeptions inherit FileSystemError !
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+ # remove()
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+ # link()
+
+
+class DirectoryHardLinkError(FileSystemError):
+ pass
+ # mount()
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+ # unmount()
+
+
+class NotAMountPointError(FileSystemMountError):
+ pass
+
+ # MY ERRORS:
+
+
+class NotAValidOwnerError(FileSystemError):
+ pass
+
+
+class AttemptingToCreateInFileError(FileSystemError):
+ pass