def _validate_static_prefix(ctx, param, value): # pylint: disable=unused-argument """ Validate that the static_prefix option starts with a "/" and does not end in a "/". Conforms to the callback interface of click documented at http://click.pocoo.org/5/options/#callbacks-for-validation. """ if value is not None: if not value.startswith("/"): raise UsageError("--static-prefix must begin with a '/'.") if value.endswith("/"): raise UsageError("--static-prefix should not end with a '/'.") return value
def handle_parse_result(self, ctx, opts, args): if self.mutually_exclusive.intersection(opts) and self.name in opts: raise UsageError("Illegal usage: `{}` is mutually exclusive with " "arguments `{}`.".format( self.name, ', '.join(self.mutually_exclusive))) if not self.required_options.intersection(opts) and self.name in opts: raise UsageError("Illegal usage: `{}` requires " "options `{}` to be provided.".format( self.name, ', '.join(self.required_options))) return super(MEOWro, self).handle_parse_result(ctx, opts, args)
def value_proc(user_input): remaining_candidate = [ choice for choice in choices if choice.startswith(user_input) ] if not remaining_candidate: raise UsageError("Expected one of {}, got {}".format( choices, user_input)) if len(remaining_candidate) == 1: return remaining_candidate[0] else: raise UsageError( "Ambiguous input. '{}' can refer to one of {}".format( user_input, remaining_candidate))
def login(api_host: str, token: str, client_id: str, client_secret: str, issuer: str): """ Authorize on API endpoint. Check that credentials is correct and save to the config """ # clean config from previous credentials _reset_credentials() # update config update_config_file( API_URL=api_host, ODAHUFLOWCTL_OAUTH_CLIENT_ID=client_id, ODAHUFLOWCTL_OAUTH_CLIENT_SECRET=client_secret, API_TOKEN=token, ISSUER_URL=issuer ) # set predicates is_token_login = bool(token) is_client_cred_login = bool(client_id) or bool(client_secret) is_interactive_login = not (is_token_login or is_client_cred_login) # validate if is_token_login and is_client_cred_login: raise UsageError('You should use either --token or --client_id/--client_secret to login. ' 'Otherwise skipp all options to launch interactive login mode') if is_client_cred_login and (not client_id or not client_secret): raise UsageError('You must pass both client_id and client_secret to client_credentials login') if is_client_cred_login and not config.ISSUER_URL: raise UsageError('You must provide --issuer parameter to do client_credentials login. ' 'Or set ISSUER_URL option in config file') # fetch openid configuration if config.ISSUER_URL: fetch_openid_conf(config.ISSUER_URL) # login try: api_client = api.RemoteAPIClient(api_host, token, client_id, client_secret, non_interactive=not is_interactive_login) api_client.info() print('Success! Credentials have been saved.') except api.IncorrectAuthorizationToken as wrong_token: LOG.error('Wrong authorization token\n%s', wrong_token) sys.exit(1)
def dump(path, strip, meta, content): """Dump Tika extraction results for PATH (file or url path).""" media_type = mime_clean(mime_guess(path)) if media_type not in SUPPORTED_MIME_TYPES: click.echo("Unsupported media type {}.".format(media_type)) click.echo("Please request support at https://github.com/iscc/iscc-cli/issues") if media_type == "application/x-mobipocket-ebook": tempdir, epub_filepath = mobi.extract(path) tika_result = parser.from_file(epub_filepath) shutil.rmtree(tempdir) else: tika_result = parser.from_file(path) if all([meta, content]): raise UsageError("Use either --meta or --content for selective output.") if strip: tika_result["content"] = tika_result.get("content", "")[:strip] if meta: click.echo(json.dumps(tika_result.get("metadata", ""), indent=2)) elif content: click.echo(json.dumps(tika_result.get("content", ""), indent=2)) else: click.echo(json.dumps(tika_result, indent=2))
def main(**kwargs): # fmt: on if kwargs["generate_config"]: print(Config.default) return if kwargs["config_path"] is None: raise UsageError('Error: Missing option "-c" / "--config-path".') config_path = kwargs["config_path"] if not config_path.exists(): sys.exit(f"Configuration file does not exist! {config_path!r}") config = Config.from_file(config_path) # Overload the configs values with any suitable cli args. overloads = toml_loads(Config.default)["ep"] for key, value in [ (value, kwargs[value]) for value in overloads if kwargs.get(value, None) is not None ]: config["ep"][key] = value get_logger( "discord", "WARN", fmt="[[ discord ]] [%(asctime)s] %(levelname)s - %(message)s" ) config["disabled"] = disable = kwargs["disable"] with Client(config=config, disable=disable) as client: client.run()
def handle_parse_result(self, ctx, opts, args): if self.mutually_exclusive.intersection(opts) and self.name in opts: raise UsageError(f'Illegal usage: `{self.name}` is mutually ' f'exclusive with arguments [{self.exclusives}].') return super(MutuallyExclusiveOption, self).\ handle_parse_result(ctx, opts, args)
def make(context, repository, target, parameters, make_destination): """ Execute some make targets on the remote host. :param context: Click context :param parameters: parameters to pass to make :param make_destination: optional destination on the remote host :param repository: name of the repository in which to work :param target: make target or targets """ playbook_variables = { 'origin_ci_make_repository': repository, 'origin_ci_make_targets': list(target) } if parameters: make_parameters = {} for param in parameters: if '=' not in param: raise UsageError( 'Parameter values must be a key-value pair. Parameter %s is invalid.' % param) (key, val) = param.split('=', 1) make_parameters[key] = val playbook_variables['origin_ci_make_parameters'] = make_parameters if make_destination: playbook_variables['origin_ci_make_destination'] = make_destination context.obj.run_playbook(playbook_relative_path='make/main', playbook_variables=playbook_variables)
def handle_parse_result(self, ctx, opts, args): if self.name in opts and self.mutually_exclusive and self.mutually_exclusive.intersection( opts): raise UsageError("Illegal usage: `{}` is mutually exclusive with " "arguments `{}`.".format( self.name, ', '.join(self.mutually_exclusive))) if self.name in opts and self.requires and not self.requires.intersection( opts): raise UsageError("Illegal usage: `{}` requires " "arguments `{}`.".format(self.name, ', '.join( self.requires))) return super(MutuallyExclusiveAndRequireOption, self).handle_parse_result(ctx, opts, args)
def main(log_file, drop_event, drop_logger, replacements, replacements_from_file, output): if replacements_from_file: replacements = replacements_from_file.read() if not replacements: replacements = '{}' try: replacements = json.loads(replacements) except (JSONDecodeError, UnicodeDecodeError) as ex: raise UsageError( f'Option "--replacements" contains invalid JSON: {ex}') from ex log_records, known_fields = parse_log(log_file) prog_bar = click.progressbar(log_records, label='Rendering', file=_default_text_stderr()) with prog_bar as log_records_progr: print( render( log_file.name, transform_records( filter_records( log_records_progr, drop_events=set(d.lower() for d in drop_event), drop_loggers=set(l.lower() for l in drop_logger), ), replacements, ), len(log_records), known_fields, ), file=output, )
def handle_parse_result(self, ctx, opts, args): cleaned_opts = set([o.replace('_', '-') for o in opts.keys()]) if len(cleaned_opts.intersection(set(self.one_of))) > 1: raise UsageError('Only one of {} should be provided.'.format( self.one_of)) return super(OptionalOneOfOption, self).handle_parse_result(ctx, opts, args)
def merge(ctx: ContextObject, ref: str, force: bool, expected_hash: str, hash_on_ref: str, from_ref: str) -> None: """Merge FROM_REF into another branch. FROM_REF can be a hash or branch. Examples: nessie merge -c 12345678abcdef dev -> merge dev to default branch with default branch's expected hash '12345678abcdef' nessie merge -b main -c 12345678abcdef dev -> merge dev to a branch named main with main branch's expected hash '12345678abcdef' nessie merge -b main -o 56781234abcdef -c 12345678abcdef dev -> merge dev at hash-on-ref '56781234abcdef' to main branch with main branch's expected hash '12345678abcdef' nessie merge -f -b main dev -> forcefully merge dev to a branch named main """ if not force and not expected_hash: raise UsageError( """Either condition or force must be set. Condition should be set to a valid hash for concurrency control or force to ignore current state of Nessie Store.""" ) ctx.nessie.merge(from_ref, ref, hash_on_ref, expected_hash) click.echo()
def project_name_proc(user_input): try: node._assert_symbol_name(user_input, "project name") except LoadError as e: message = "{}\n\n{}\n".format(e, e.detail) raise UsageError(message) from e return user_input
def load_targets(urls, files): if not any((urls, files)): raise UsageError("option url/file is required") targets = _load_from_links_and_files( urls, files) # parse targets targets = [parse_path(target, ("parser.url",)) for target in targets] # list expand if targets: targets = tuple(chain.from_iterable(targets)) CONFIG.base.targets = targets detail_msgs = "" for target in targets: temp_msg = header("Load target", "*", target) + "\n" logger.info(temp_msg, extra={"markup": True}) detail_msgs += temp_msg if CONFIG.option.get("very_verbose", False): cprint(detail_msgs) count_msg = header("Load targets", "+", "Loaded [%d] targets" % len(targets)) logger.info(count_msg, extra={"markup": True}) if CONFIG.option.get("verbose", False): cprint(count_msg) cprint()
def send_mail(id): if not send_to: raise UsageError(WARN) if not (EMAIL_ADDRESS and EMAIL_PASSWORD): raise UsageError('`EMAIL` or `PASSWORD`' ' environment variables not found.' ' These are needed in order to send emails.') session = session_factory() content = session.query(Event).filter_by(id=id).first() if content is None: raise UsageError(f'No event with ID : {id}') _send_mail(content.title, content.html, send_to=send_to) session.close()
def create_pull_request(username, password, title, description, source_branch_name, destination_branch_name, repository_name): url = 'https://api.bitbucket.org/2.0/repositories/{repository}/pullrequests'.format( repository=repository_name) json_data = { 'title': title, 'description': description, 'source': { 'branch': { 'name': source_branch_name } }, 'destination': { 'branch': { 'name': destination_branch_name } }, 'reviewers': get_default_reviewers(username, password, repository_name), } response = requests.post(url, headers={'content-type': 'application/json'}, json=json_data, auth=HTTPBasicAuth(username, password)) if response.status_code != 201: raise UsageError('Bitbucket error: {}'.format( response.content.decode('utf-8'))) return response.json()['links']['html']['href']
def download(self, pid, filename, output_filepath=None): """Download a file attached to your analysis. :param pid: analysis PID :type pid: str :param filename: filename :type filename: str :param output_filepath: save your file as.. :type output_filepath: str, optional :return: None """ if output_filepath: dirpath = os.path.dirname(output_filepath) if dirpath and not os.path.exists(dirpath): raise UsageError( 'Directory {} does not exist.'.format(dirpath)) bucket_url = self._get_bucket_link(pid) data = self._make_request( url=bucket_url + '/' + filename, headers={}, stream=True, ) with open(output_filepath or filename, 'wb') as fp: fp.write(data.content)
def staging( staging_cluster: str, staging_service: str, project_name: Optional[str], repository_id: Optional[Tuple[str, str, str]], build_number: str, build_url: str, commit_id: str, commit_message: str, smtp_host: str, smtp_port: int, smtp_user: str, smtp_password: str, email_sender: str, email_recipient: str, image_local: str, image_ecr: str, trial_run: bool, ) -> None: """ Deploy a new image to the staging environment. """ if not trial_run: ensureCI() if ":" not in image_ecr: repo = Repo() commitID = repo.head.object.hexsha image_ecr = f"{image_ecr}:{commitID[:7]}" if image_local: ecrClient = ECRServiceClient() ecrClient.push(image_local, image_ecr, trialRun=trial_run) stagingClient = ECSServiceClient(cluster=staging_cluster, service=staging_service) try: stagingClient.deployImage(image_ecr, trialRun=trial_run) except NoSuchServiceError as e: raise UsageError(f"Unknown service: {e.service}") if (repository_id is not None and smtp_host and smtp_port and smtp_user and smtp_password and email_sender and email_recipient): notifyStaging( project_name=project_name, repository_id=repository_id, build_number=build_number, build_url=build_url, commit_id=commit_id, commit_message=commit_message, smtp_host=smtp_host, smtp_port=smtp_port, smtp_user=smtp_user, smtp_password=smtp_password, email_sender=email_sender, email_recipient=email_recipient, trial_run=trial_run, ) else: log.info("SMTP notification not configured")
def handle_parse_result(self, ctx, opts, args): cleaned_opts = set([o.replace('_', '-') for o in opts.keys()]) if len(cleaned_opts.intersection(set(self.one_of))) == 0: raise MissingParameter('One of {} must be provided.'.format(self.one_of)) if len(cleaned_opts.intersection(set(self.one_of))) > 1: raise UsageError('Only one of {} should be provided.'.format(self.one_of)) return super(RequiredOptions, self).handle_parse_result(ctx, opts, args)
def create_merge_release_pull_request(username, password, source_branch_name, destination_branch_name, repository_name): url = 'https://api.bitbucket.org/2.0/repositories/{repository}/pullrequests'.format( repository=repository_name) headers = {'content-type': 'application/json'} json_data = { 'title': 'Merge branch "{}"'.format(source_branch_name), 'source': { 'branch': { 'name': source_branch_name } }, 'destination': { 'branch': { 'name': destination_branch_name } } } resp = requests.post(url, headers=headers, json=json_data, auth=HTTPBasicAuth(username, password)) if resp.status_code != 201: raise UsageError('Bitbucket error: {}'.format( resp.content.decode('utf-8'))) return resp.json()['links']['html']['href']
def _cli_load_invariant(condition, msg=None): msg = ( msg or "Invalid set of CLI arguments for loading repository/pipeline. See --help for details." ) if not condition: raise UsageError(msg)
def cred_file_present(): setting_path = cred_path() if not os.path.isfile(setting_path): raise UsageError( "zen report data is not present please use zen create command") return setting_path
def read_ics(source: str): if re.match("https?://", source): return requests.get(source).text elif isfile(source): with open(source) as f: return f.read() raise UsageError(f"Unable to fetch source {source}")
def handle_parse_result(self, ctx, opts, args): if self.mutually_exclusive.intersection(opts) and self.name in opts: raise UsageError("Illegal usage: `{}` is mutually exclusive with:" " `{}`.".format( self.name, ', '.join(self.mutually_exclusive))) return super(MutexOption, self).handle_parse_result(ctx, opts, args)
def connection(self): try: if not self._session: self._session = boto3.Session(profile_name=self._aws_profile) return self._session.resource('ec2') except exceptions.ProfileNotFound as e: raise UsageError(str(e)) except exceptions.ConfigParseError as e: raise UsageError(str(e)) except exceptions.ConfigNotFound as e: raise UsageError(str(e)) except exceptions.UnknownCredentialError as e: raise UsageError(str(e)) except exceptions.NoRegionError as e: raise UsageError(str(e))
def show_dashboard(ctx): kube = ctx.obj['KUBE'] if not kube.kubeconfig: raise UsageError( "You need to either set your KUBECONFIG env var or use the -k option to the path to your " + "product/environment's kube config") kube.show_dashboard()
def handle_parse_result(self, ctx, opts, args): if self.mutually_exclusive.intersection(opts) and self.name in opts: raise UsageError( "Usage: `{}` is mutually exclusive with " "arguments `{}`.".format(self.name, ", ".join(self.mutually_exclusive)) ) return super(MutuallyExclusiveOption, self).handle_parse_result(ctx, opts, args)
def resolve_command(self, ctx, args): # noqa: D102 try: return super(DYMMixin, self).resolve_command(ctx, args) except UsageError as error: cmd_name = click.utils.make_str(args[0]) cmd_list = self.list_commands(ctx) error_msg = self._create_error_msg(str(error), cmd_name, cmd_list) raise UsageError(error_msg, error.ctx)
def get_current_user_uuid(username, password): response = requests.get('https://api.bitbucket.org/2.0/user', headers={'content-type': 'application/json'}, auth=HTTPBasicAuth(username, password)) if response.status_code != 200: raise UsageError('Bitbucket error: {}'.format( response.content.decode('utf-8'))) return response.json()['uuid']
def config(ctx, set_wizard): """ Management of the ML-Git config file. """ if set_wizard: change_wizard_mode(set_wizard) elif ctx.invoked_subcommand is None: raise UsageError('Missing command')