Светлин обнови решението на 30.04.2015 16:30 (преди над 9 години)
+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(DirectoryDeletionError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(DirectoryDeletionError):
+ pass
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class File():
+ def __init__(self, path, content):
+ self.path = path
+ self.content = content
+ self.is_directory = False
+ self.size = len(content) + 1
+
+ def append(self, text):
+ self.content += text
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(text) + 1
+
+
+class Directory():
+ def __init__(self, path):
+ self.path = path
+ self.is_directory = True
+ self.size = 1
+ self.directories = []
+ self.files = []
+
+
+class FileSystem():
+ def __init__(self, full_size):
+ self.full_size = full_size
+ self.available_size = full_size
+ root = Directory('/')
+ self.nodes = [root]
+ self.available_size -= root.size
+
+ def get_node(self, path):
+ try:
+ exists = False
+ for item in self.nodes:
+ if item.path == path:
+ exists = True
+ node = item
+ if exists:
+ return node
+ else:
+ raise NodeDoesNotExistError
+ except NodeDoesNotExistError:
+ print ('File/Directory Does Not Exist')
+
+ def get_actual_dir(self, path):
+ dirs = path.split('/')
+ dirs.pop(-1)
+ actual_dir = '/'.join(dirs)
+ return actual_dir or '/'
+
+ def get_file_or_folder_name(self, path):
+ if '.' in path:
+ return path.split('/')[-1]
+ return '/' + path.split('/')[-1]
+
+
+ def create(self, path, directory=False, content=''):
+ try:
+ actual_dir = self.get_actual_dir(path)
+ dir_exists = False
+ for item in self.nodes:
+ if item.path == path:
+ raise DestinationNodeExistsError
+ break
+ if item.path == actual_dir:
+ dir_exists = True
+ if dir_exists is False:
+ raise DestinationNodeDoesNotExistError
+ if ((directory is False and len(content) + 1 > self.available_size)
+ or (directory is True and self.available_size < 1)):
+ raise NotEnoughSpaceError
+ if directory is False:
+ new_file = File(path, content=content)
+ self.nodes.append(new_file)
+ self.available_size -= new_file.size
+ current_dir = self.get_node(actual_dir)
+ # current_dir.files.append(self.get_file_or_folder_name(new_file.path).replace('/', ''))
+ current_dir.files.append(new_file)
+ else:
+ new_dir = Directory(path)
+ self.nodes.append(new_dir)
+ self.available_size -= new_dir.size
+ current_dir = self.get_node(actual_dir)
+ # current_dir.directories.append(self.get_file_or_folder_name(new_dir.path))
+ current_dir.directories.append(new_dir)
+
+ except DestinationNodeExistsError:
+ print('File/Directory Already Exists')
+ except DestinationNodeDoesNotExistError:
+ print('File/Directory Does Not Exist')
+ except NotEnoughSpaceError:
+ print('Not Enough Space')
+
+ def remove(self, path, directory=False, force=False):
+ to_delete = self.get_node(path)
+ try:
+ if to_delete:
+ if isinstance(to_delete, File):
+ self.nodes.remove(to_delete)
+ self.get_node(self.get_actual_dir(to_delete.path)).files.remove(to_delete)
+ elif isinstance(to_delete, Directory) and directory is True:
+ if not to_delete.directories and not to_delete.files:
+ if force is True:
+ self.nodes.remove(to_delete)
+ self.get_node(self.get_actual_dir(to_delete.path)).directories.remove(to_delete)
+ else:
+ raise NonEmptyDirectoryDeletionError
+ self.nodes.remove(to_delete)
+ self.get_node(self.get_actual_dir(to_delete.path)).directories.remove(to_delete)
+ else:
+ raise NonExplicitDirectoryDeletionError
+
+ except NonExplicitDirectoryDeletionError:
+ print('Non Explicit Directory Deletion')
+ except NonEmptyDirectoryDeletionError:
+ print('Directory Is Not Empty')
+
+
+ def move(self, source, destination):
+ try:
+ src = self.get_node(source)
+ dest = self.get_node(destination)
+ if not src:
+ raise SourceNodeDoesNotExistError
+ if not dest:
+ raise DestinationNodeDoesNotExistError
+ if not isinstance(dest, Directory):
+ raise DestinationNotADirectoryError
+ if (self.get_file_or_folder_name(source) in dest.directories or
+ self.get_file_or_folder_name(source) in dest.files):
+ raise DestinationNodeExistsError
+
+ except SourceNodeDoesNotExistError:
+ print('Source Does Not Exist')
+ except DestinationNodeDoesNotExistError:
+ print('Destination Does Not Exist')
+ except DestinationNotADirectoryError:
+ print('Destination Is Not A Directory')
+ except DestinationNodeExistsError:
+ print('Destination Already Exists')