Христо обнови решението на 30.04.2015 16:23 (преди над 9 години)
+import itertools
+
+
+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class File:
+ def __init__(self, content, path):
+ self.content = content
+ self.size = len(self.content) + 1
+ self.path = path
+ self.is_directory = False
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+
+class Directory:
+ def __init__(self, directories, files, path):
+ self.is_directory = True
+ self.path = path
+ self.directories = list()
+ self.directories.extend(directories)
+ self.files = list()
+ self.files.extend(files)
+ self.nodes = itertools.chain(directories, files)
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.size = size
+ self.available_size = self.size - 1
+ self.all_directories = list()
+ self.all_directories.append(Directory(list(), list(), '/'))
+ self.all_files = list()
+ self.all_paths = ['/', '']
+
+ def get_node(self, path):
+ if path in self.all_paths:
+ for directory in self.all_directories:
+ if directory.path == path:
+ return directory
+ for files in self.all_files:
+ if files.path == path:
+ return files
+ else:
+ raise NodeDoesNotExistError
+
+ def create(self, path, directory=False, content=''):
+ need_path = path.rsplit('/', 1)[0]
+ if directory is False:
+ if need_path in self.all_paths and path not in self.all_paths:
+ if (len(content) + 1) <= self.available_size:
+ for path_dir in self.all_directories:
+ if path_dir.path == need_path:
+ path_dir.files.append(File(content, path))
+ self.all_files.append(File(content, path))
+ self.all_paths.append(path)
+ self.available_size -= len(content) + 1
+ else:
+ raise NotEnoughSpaceError
+ else:
+ raise DestinationNodeExistsError
+ else:
+ if need_path in self.all_paths:
+ if path not in self.all_paths:
+ if (self.available_size + 1) >= 0:
+ for path_dir in self.all_directories:
+ if path_dir.path == need_path:
+ path_dir.directories.append(
+ Directory(list(), list(), path))
+ self.all_directories.append(
+ Directory(list(), list(), path))
+ self.all_paths.append(path)
+ self.available_size -= 1
+ else:
+ raise NotEnoughSpaceError
+ else:
+ raise DestinationNodeExistsError
+ else:
+ raise DestinationNodeDoesNotExistError
+
+ def remove(self, path, directory=False, force=True):
+ if path in self.all_paths:
+ for dirs in self.all_directories:
+ if dirs.path == path and directory is False:
+ raise NonExplicitDirectoryDeletionError
+ if directory is True:
+ for dirs in self.all_directories:
+ if dirs.path == path and len(dirs.files) != 0 and len(dirs.directories) != 0 and force is True:
+ for dirs in self.all_directories:
+ if dirs.path == path:
+ for sub_dir in dirs.directories:
+ self.all_paths.remove(sub_dir.path)
+ self.available_size += 1
+ for sub_file in dirs.directories:
+ self.all_paths.remove(sub_file.path)
+ self.available_size += sub_file.size
+ else:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ self.all_paths.remove(path)
+ for files in self.all_files:
+ if files.path == path:
+ self.available_size += files.size
+ else:
+ raise NodeDoesNotExistError
+
+ def move(self, source, destination):
+ if source not in self.all_paths:
+ raise SourceNodeDoesNotExistError
+ if destination not in self.all_paths:
+ raise DestinationNodeDoesNotExistError
+ flag = False
+ for dirs in self.all_directories:
+ if dirs.path == destination:
+ flag = True
+ if flag:
+ raise DestinationNotADirectoryError
+ for dirs in self.all_directories:
+ if dirs.path == destination:
+ for sub_dirs in dirs.directories:
+ if sub_dirs.path == source:
+ raise DestinationNodeExistsError
+ for all_files in dirs.files:
+ if all_files.path == source:
+ raise DestinationNodeExistsError
+ for dirs in self.all_directories:
+ if dirs.path == source:
+ for sub_dirs in self.all_directories:
+ if sub_dirs.path == destination:
+ sub_dirs.directories.update(dirs)
+ self.all_directories.remove(dirs)
+
+ def link(self, source, destination, symbolic=True):
+ pass
+
+ def mount(self, file_system, path):
+ pass
+
+ def unmount(self, path):
+ pass