Емине обнови решението на 30.04.2015 16:56 (преди над 9 години)
+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class DirectoryHardLinkError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class NotAMountpointError(FileSystemMountError):
+ pass
+
+
+class File:
+
+ def __init__(self, content):
+ self.is_directory = False
+ self.content = str(content)
+ self.size = len(self.content) + 1
+
+ def append(self, text):
+ self.content += str(text)
+
+ def truncate(self, text):
+ self.content = str(text)
+
+ def __repr__(self):
+ return 'file("' + self.content[:7] + '...")'
+
+
+class Directory:
+
+ def __init__(self, path):
+ self.is_directory = True
+ self.path = path
+ self.directories = []
+ self.files = []
+ self.nodes = []
+
+ def __repr__(self):
+ return 'dir("' + self.path + '")'
+
+ def __eq__(self, other):
+ return self.path == other.path and self.nodes == other.nodes
+
+ def __hash__(self):
+ return hash(self.path)
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.size = size
+ self._root = Directory('/')
+ self.available_size = self.size - 1
+ self._all_paths = {'/': self._root}
+
+ def _has_space_for(self, directory, content):
+ if directory:
+ return self.available_size - 1 >= 0
+ return self.available_size - len(content) - 1 >= 0
+
+ def _is_exist_path(self, path):
+ return path in self._all_paths
+
+ def _path_to_previous(self, path):
+ if path[0] == '/' and path.count('/') == 1:
+ return '/'
+ return path[:len(path) - path[::-1].index('/') - 1]
+
+ def _is_valid_path_to_file(self, path):
+ return self._path_to_previous(path) in self._all_paths
+
+ def get_node(self, path):
+ if path not in self._all_paths:
+ raise NodeDoesNotExistError
+ return self._all_paths[path]
+
+ def create(self, path, directory=False, content=''):
+ if path in self._all_paths:
+ raise DestinationNodeExistsError
+
+ if not self._has_space_for(directory, content):
+ raise NotEnoughSpaceError
+
+ if self._is_valid_path_to_file(path):
+ if directory:
+ self._all_paths[path] = Directory(path)
+
+ previous = self._path_to_previous(path)
+ current = self._all_paths[path]
+
+ self._all_paths[previous].directories.append(current)
+ self._all_paths[previous].nodes.append(current)
+
+ self.available_size -= 1
+ else:
+ self._all_paths[path] = File(content)
+
+ previous = self._path_to_previous(path)
+ current = self._all_paths[path]
+
+ self._all_paths[previous].files.append(current)
+ self._all_paths[previous].nodes.append(current)
+
+ self.available_size -= len(content) + 1
+ else:
+ raise DestinationNodeDoesNotExistError
+
+ def remove(self, path, directory=False, force=True):
+ if path not in self._all_paths:
+ raise NodeDoesNotExistError
+
+ if self._all_paths[path].is_directory:
+ if not directory:
+ raise NonExplicitDirectoryDeletionError
+ else:
+ if len(self._all_paths[path].nodes) > 0:
+ if not force:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ previous = self._path_to_previous(path)
+ self._all_paths[previous].directories.remove(
+ self._all_paths[path])
+ self._all_paths[previous].nodes.remove(
+ self._all_paths[path])
+ del self._all_paths[path]
+ else:
+ previous = self._path_to_previous(path)
+ self._all_paths[previous].files.remove(self._all_paths[path])
+ self._all_paths[previous].nodes.remove(self._all_paths[path])
+
+ def move(self, source, destination):
+ if not self._is_exist_path(source):
+ raise SourceNodeDoesNotExistError
+
+ if not self._is_exist_path(destination):
+ raise DestinationNodeDoesNotExistError
+
+ if not self.get_node(destination).is_directory:
+ raise DestinationNotADirectoryError
+ else:
+ if self._is_exist_path(destination):
+ raise DestinationNodeExistsError
+
+ old_dir_path = self._path_to_previous(source)
+ new_dir_path = self._path_to_previous(destination)
+ current_name = source[len(old_dir_path) + 1:]
+ old = self.get_node(source)
+
+ if old.is_directory:
+ pass
+ else:
+ self.create(destination, directory=False, content=old.content)
+ self.remove(source, directory=False)
+
+ def link(self, source, destination, symbolic=True):
+ pass
+
+ def mount(self, file_system, path):
+ pass
+
+ def unmount(self, path):
+ pass