Виктор обнови решението на 30.04.2015 02:16 (преди над 9 години)
+def prev_path(path):
+ sample = ''
+ for i in path.split('/')[1:-1]:
+ sample += ('/' + i)
+ return sample
+
+
+class File:
+ def __init__(self, content, path):
+ self.content = content
+ self.path = path
+
+ def is_directory(self):
+ return False
+
+
+class Directory:
+ def is_empty(self):
+ return self.empty
+
+ def __init__(self, path):
+ self.path = path
+ self.empty = True
+ self.files = []
+ self.directories = []
+
+ def is_directory(self):
+ return True
+
+
+class FileSystem(File, Directory):
+ def __init__(self, memo):
+ self.size = memo
+ self.avaliable_size = memo - 1
+ self.paths = {'': Directory('')}
+
+ def get_node(self, path):
+ if path not in self.paths:
+ raise NodeDoesNotExistError
+ else:
+ return self.paths[path]
+
+ def create(self, path, directory=False, content=''):
+ if prev_path(path) not in self.paths:
+ raise DestinationNodeDoesNotExistError
+ if self.avaliable_size - len(content) - 1 < 0:
+ raise NotEnoughSpaceError
+ if path in self.paths:
+ raise DestinationNodeExistsError
+ self.avaliable_size = self.avaliable_size - len(content) - 1
+ if self.paths[prev_path(path)].is_directory():
+ self.paths[prev_path(path)].empty = False
+ if directory is False:
+ self.paths[path] = File(content, path)
+ self.paths[prev_path(path)].files.append(self.get_node(path))
+ self.paths[prev_path(path)].files.append(path)
+ self.paths[prev_path(path)].files.append(content)
+ else:
+ self.paths[path] = Directory(path)
+ self.paths[prev_path(path)].directories.append(self.get_node(path))
+ self.paths[prev_path(path)].directories.append(path)
+
+ def remove(self, path, directory=False, force=True):
+ if self.paths[path].is_directory():
+ if directory is False:
+ raise NonExplicitDirectoryDeletionError
+ if self.paths[path].is_empty() is True and force is not True:
+ raise NonEmptyDirectoryDeletionError
+ if path not in self.paths:
+ raise NodeDoesNotExistError
+ if self.paths[path].files is not []:
+ for i in self.paths[path].files:
+ remove(i, get_node(path).is_directory(), force=True)
+ del self.paths[path]
+ else:
+ self.avaliable_size += (len(self.paths[path].content) + 1)
+ del self.paths[path]
+
+ def move(self, source, destination):
+ if source not in self.paths:
+ raise SourceNodeDoesNotExistError
+ if destination not in self.paths:
+ raise DestinationNodeDoesNotExistError
+ if get_node(destination).is_directory is False:
+ raise DestinationNotADirectoryError
+ if (destination + '/' + source.split('/')[-1]) in self.paths:
+ raise DestinationNodeExistsError
+ self.remove(source, directory=False, force=True)
+ self.create(destination, directory=False, content='')
+
+ def mount(self, file_system, path):
+ if self.get_node(path).is_empty is False:
+ raise MountPointNotEmptyError
+ if self.get_node(path).is_directory is False:
+ raise MountPointNotADirectoryError
+ if path not in self.paths:
+ raise MountPointDoesNotExistError
+ self.paths[path] = file_system
+
+ def unmount(self, path):
+ if path not in self.paths:
+ raise NodeDoesNotExistError
+ if type(self.paths[path]) is not FileSystem:
+ raise NotAMountpointError
+ self.paths[path] = Directory(path)
+
+ def link(self, source, destination, symbolic=True):
+ if source not in self.paths and symbolic is True:
+ raise NodeDoesNotExistError
+ if self.paths[source].is_directory and symbolic is False:
+ raise DirectoryHardLinkError
+ if symbolic is False and destination not in self.paths:
+ raise SourceNodeDoesNotExistError
+
+
+class FileSystemError:
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError, FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError, FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError, FileSystemError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError, FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError, FileSystemError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass