Иван обнови решението на 30.04.2015 12:07 (преди над 9 години)
+class FileSystemError(Exception):
+
+ def __init__(self, message):
+ self.message = message
+
+
+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 NonExplicitDirectoryDeletionError(FileSystemError):
+
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+
+ pass
+
+
+class DirectoryHardLinkError(FileSystemError):
+
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+
+ pass
+
+
+class File:
+
+ def __init__(self, content):
+ self.__is_directory = False
+ self.__content = content
+ self.__size = len(content) + 1
+
+ @property
+ def content(self):
+ return self.__content
+
+ @property
+ def size(self):
+ return self.__size
+
+ @property
+ def is_directory(self):
+ return self.__is_directory
+
+ def append(self, text):
+ self.__content += text
+
+ def truncate(self, text):
+ self.__content = text
+ self.__size = len(text) + 1
+
+
+class Directory:
+
+ def __init__(self, name):
+ self.__name = name
+ self.__is_directory = True
+ self.__directories = []
+ self.__files = []
+ self.__nodes = []
+
+ @property
+ def files(self):
+ return self.__files
+
+ @property
+ def directories(self):
+ return self.__directories
+
+ @property
+ def nodes(self):
+ return self.__files + self.__directories
+
+ @property
+ def name(self):
+ return self.__name
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.__size = size
+ self.__available_size = size - 1 # 1 byte for root(/)
+ self.__root = Directory('/')
+
+ @property
+ def size(self):
+ return self.__size
+
+ @property
+ def available_size(self):
+ return self.__available_size
+
+ # not implemented yet
+ def get_node(self, path):
+ path_directories = path.split('/')
+ path_directories = list(filter(lambda x: x != '', path_directories))
+
+ # doesn't work, don't bother reading
+ def create(self, path, directory=False, content=''):
+ path_directories = path.split('/')
+ path_directories = list(filter(lambda x: x != '', path_directories))
+ if len(path_directories) == 1:
+ if directory is True:
+ self.root.directories.append(Directory(path_directories[0]))
+ else:
+ self.root.files.append(File(content))
+ else:
+ current_dir = self.root.directories[0]
+ destination_dir = path_directories[len(path_directories) - 1]
+ for i in range(0, len(path_directories) - 1):
+ if (current_dir.name != path_directories[i] and
+ path_directories[i] == destination_dir):
+ if directory is True:
+ if self.__available_size > 0:
+ new_dir = Directory(path_directories[i])
+ current_dir.directories.append(new_dir)
+ else:
+ raise NotEnoughSpaceError
+ else:
+ if len(content + 1) <= self.__available_size:
+ current_dir.files.append(File(content))
+ else:
+ raise NotEnoughSpaceError
160 реда код без никаква функционалност...