def _assert_valid_variable_value(self, variable, value): if variable == CHECKSUM_NAME: try: d1_common.checksum.get_checksum_calculator_by_dataone_designator(value) except LookupError: raise cli_exceptions.InvalidArguments( 'Invalid checksum algorithm: {}'.format(value) ) elif variable == CN_URL_NAME: # TODO: Add warning if URL is not a known CN / environment pass elif variable == MN_URL_NAME: cn_base_url = self.get(CN_URL_NAME) if value not in [ n[2] for n in self._nodes.get(cn_base_url) if n[0] == 'mn' ]: if not cli_util.confirm( '"{}" is not a known DataONE Member Node. Use anyway?'. format(value) ): raise cli_exceptions.InvalidArguments('Member Node update cancelled') elif variable == FORMAT_NAME: cn_base_url = self.get(CN_URL_NAME) if value not in self._format_ids.get(cn_base_url): raise cli_exceptions.InvalidArguments( 'Invalid Object Format ID: {}'.format(value) )
def clear(self): self._assert_queue_not_empty() if cli_util.confirm( 'You are about to clear the queue of {} write operations. Continue?' .format(len(self._operations)), default='yes'): self._clear()
def _confirm_special_subject_write(self, subject, permission): if subject in ('public', 'authenticatedUser', 'verifiedUser') and permission != 'read': if not cli_util.confirm( 'It is not recommended to give {} access to {}. Continue?'. format(permission, subject)): raise cli_exceptions.InvalidArguments('Cancelled')
def execute(self): self._assert_queue_not_empty() self._print_operation_queue() if not cli_util.confirm( 'You are about to perform {} queued write operations. Continue?' .format(len(self._operations)), default='yes'): raise cli_exceptions.InvalidArguments('Cancelled') while len(self._operations): self._execute_operation(self._operations[0]) self._operations = self._operations[1:]
def do_exit(self, line): """exit Exit from the CLI """ n_remaining_operations = len(self._command_processor.get_operation_queue()) if n_remaining_operations: cli_util.print_warn( """There are {} unperformed operations in the write operation queue. These will be lost if you exit.""".format(n_remaining_operations) ) if not cli_util.confirm('Exit?', default='yes'): return sys.exit()
def _test_confirm( self, default=None, answer=None, allow_blank=False, expected_prompt=None, expected_result=None ): test_prompt = 'Test Prompt' with d1_test.d1_test_case.capture_std() as (out_stream, err_stream): with d1_test.d1_test_case.mock_input(answer or ''): is_confirmed = cli_util.confirm( test_prompt, default=default or '', allow_blank=allow_blank ) assert expected_result == is_confirmed assert '{} {}'.format(test_prompt, expected_prompt or '').strip() in \ out_stream.getvalue()
def _output_to_file(self, file_like_object, path): abs_path = cli_util.os.path.expanduser(path) if os.path.exists(abs_path): if not cli_util.confirm( 'You are about to overwrite an existing file at "{}". Continue? ' .format(abs_path), default='yes'): cli_util.print_info('Cancelled') if isinstance(file_like_object, requests.Response): cli_util.copy_requests_stream_to_file(file_like_object, path) else: cli_util.copy_file_like_object_to_file(file_like_object, abs_path) cli_util.print_info('Created file: {}'.format(abs_path))