Божидар обнови решението на 30.04.2015 16:51 (преди над 9 години)
+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.root = Directory('/')
+ self.size = size
+ self.available_size = self.size - 1
+
+ def size(self):
+ return self.size
+
+ def available_size(self):
+ return self.available_size
+
+ def __validate_existence_of_dir(self, dirs):
+ size = len(dirs) - 1
+ if size is -1:
+ return True
+ root = self.root
+ for i in range(0, size):
+ current_dir = (r for r in root.directories if r.path == dirs[i])
+ current_dir = list(current_dir)
+ if len(current_dir) is 0:
+ return False
+ index = root.directories.index((current_dir)[0])
+ root = root.directories[index]
+ return True
+
+ def get_node(self, path):
+ dirs = path.split('/')
+ dirs = list(filter(None, dirs))
+ if self.__validate_existence_of_dir(dirs):
+ current = self.root
+ for i in range(0, len(dirs)):
+ temp = (x for x in current.nodes if x.path == dirs[i])
+ current = list(temp)[0]
+ return current
+ else:
+ raise NodeDoesNotExistError
+
+ def create(self, path, directory=False, content=''):
+ dirs = list(filter(None, path.split('/')))
+ if self.available_size - len(content) - 1 < 0:
+ raise NotEnoughSpaceError
+ elif not self.__validate_existence_of_dir(dirs):
+ raise DestinationNodeDoesNotExistError
+ elif directory is True:
+ if self.__validate_existence_of_dir(dirs):
+ root_dir = self.get_node('/'.join(dirs[0:len(dirs)-1]))
+ child_dir = Directory(dirs[-1])
+ root_dir.directories.append(child_dir)
+ self.available_size -= 1
+ else:
+ if self.__validate_existence_of_dir(dirs):
+ root_dir = self.get_node('/'.join(dirs[0:len(dirs) - 1]))
+ child_file = File(dirs[-1], content)
+ root_dir.files.append(child_file)
+ self.available_size -= len(content) + 1
+ else:
+ pass
+ # TODO error
+
+ def remove(self, path, directory=False, force=True):
+ dirs = path.split('/')
+ dirs = list(filter(None, dirs))
+ # TODO validate path existing
+ item = self.get_node(path)
+ if not item.is_directory:
+ parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
+ index = parent.files.index(item)
+ parent.files.pop(index)
+ self.size -= len(item.content()) + 1
+ elif directory is False:
+ # NonExplicitDirectoryDeletionError
+ pass
+ elif directory is True and len(item.nodes) is 0:
+ parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
+ index = parent.directories.index(item)
+ parent.directories.pop(index)
+ self.available_size += 1
+ elif (directory is True or item.is_directory) and force is False:
+ # NonEmptyDirectoryDeletionError
+ pass
+ elif directory is True and force is True:
+ parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
+ index = parent.directories.index(item)
+ parent.directories.pop(index)
+ self.available_size += 1
+ return
+
+ def move(self, source, destination):
+ return
+
+ def link(self, source, destination, symbolic=True):
+ return
+
+ def mount(self, file_system, path):
+ return
+
+ def unmount(self, path):
+ return
+
+
+class File:
+ def __init__(self, path, content=''):
+ self.path = path
+ self.size = len(content) + 1
+ self.content = content
+ self.is_directory = False
+
+ def content(self):
+ return self.content
+
+ def append(self, text):
+ self.content += text
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(text) + 1
+
+ def size(self):
+ return self.size
+
+
+class Directory:
+ def __init__(self, path):
+ self.path = path
+ self.is_directory = True
+ self.directories = []
+ self.files = []
+
+ def directories(self):
+ return self.directories
+
+ def files(self):
+ return self.files
+
+ @property
+ def nodes(self):
+ return self.directories + self.files
+
+ def __iter__(self):
+ return iter(self.nodes)