def remove(self, target: str, *args): """Remove command.""" if target not in ['archive', 'version']: raise CLICommandError('Unknown create target') if target == 'archive': if self.archive_selected is None: raise CLICommandError('No archive selected') archive_uuid = self.archive_selected.uuid if input(REMOVE_ARCHIVE_FORMAT.format( self.archive_selected.uuid, self.archive_selected.source_dir, self.archive_selected.archive_dir, self.archive_selected.max_number_of_versions, self.archive_selected.algorithm )).lower() == 'y': Abundant.remove_archive(uuid=self.archive_selected.uuid) self.archive_selected = None print('Removed archive %s' % archive_uuid) elif target == 'version': if self.version_selected is None: raise CLICommandError('No version selected') version_uuid = self.version_selected.uuid if input(REMOVE_VERSION_FORMAT.format( self.version_selected.uuid, self.version_selected.archive_agent.uuid, self.version_selected.time_of_creation, self.version_selected.is_base_version )).lower() == 'y': self.version_selected.remove() self.version_selected = None print('Removed version %s' % version_uuid)
def create(self, target: str, *args, **kwargs): """Create command.""" if target not in ['archive', 'version']: raise CLICommandError('Unknown create target') if target == 'archive': if len(args) != 4: raise CLICommandError('Incorrect archive parameter') try: int(args[3]) except ValueError: raise CLICommandError('Invalid max number of versions') if input(CREATE_ARCHIVE_FORMAT.format( args[0], args[1], args[2], args[3] )).lower() == 'y': archive = Abundant.create_archive(args[0], args[1], args[2], int(args[3])) print('Created archive %s' % archive.uuid) elif target == 'version': if len(args) != 0: raise CLICommandError('Unknown parameter') if self.archive_selected is None: raise CLICommandError('No archive selected') if input(CREATE_VERSION_FORMAT.format(self.archive_selected)).lower() == 'y': version = self.archive_selected.create_version() print('Created version %s' % version.uuid)
def __init__(self): """Create the command line interface.""" self.archive_selected = self.version_selected = None """:type archive_selected: ArchiveAgent :type version_selected: VersionAgent""" self.archives = Abundant.get_all_archives() self.VERB_TO_FUNCTION = { 'list': self.list, 'list-exact': self.list_exact, 'select': self.select, 'detail': self.detail, 'quit': self.quit, 'create': self.create, 'remove': self.remove, 'migrate': self.migrate, 'export': self.export, 'export-exact': self.export_exact }
def validate_selected_archive_and_version(self): """Make sure selected archive and version are valid.""" if self.archive_selected is None: self.version_selected = None else: all_archives = Abundant.get_all_archives() found_match = False for archive in all_archives: if archive['UUID'] == self.archive_selected.uuid: found_match = True if not found_match: self.archive_selected = None else: if self.version_selected is not None: found_match = False for version in self.archive_selected.versions: if version == self.version_selected: found_match = True if not found_match: self.version_selected = None
def select(self, target: str, index: str, *args): """Select command.""" if target not in ['archive', 'version']: raise CLICommandError('Unknown select target') try: index = int(index) except ValueError: # which indicates that index is not an integer raise CLICommandError('Invalid select index') if target == 'archive': if not self.archives: raise CLICommandError('No available archive to be selected') if index >= len(self.archives): raise CLICommandError('Invalid select index') self.archive_selected = Abundant.get_archive(uuid=self.archives[index]['UUID']) print('Selected archive %s\nUUID: %s' % (index, self.archive_selected.uuid)) elif target == 'version': if self.archive_selected is None: raise CLICommandError('No archive selected') if not self.archive_selected.versions: raise CLICommandError('No available version to be selected') if index > len(self.archive_selected.versions): raise CLICommandError('Invalid select index') self.version_selected = self.archive_selected.versions[index] print('Selected version %s\nUUID: %s' % (index, self.version_selected.uuid))