Python click.File() Examples

The following are 30 code examples of click.File(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module click , or try the search function .
Example #1
Source File: cli.py    From gitlint with MIT License 6 votes vote down vote up
def generate_config(ctx):
    """ Generates a sample gitlint config file. """
    path = click.prompt('Please specify a location for the sample gitlint config file', default=DEFAULT_CONFIG_FILE)
    path = os.path.realpath(path)
    dir_name = os.path.dirname(path)
    if not os.path.exists(dir_name):
        click.echo(u"Error: Directory '{0}' does not exist.".format(dir_name), err=True)
        ctx.exit(USAGE_ERROR_CODE)
    elif os.path.exists(path):
        click.echo(u"Error: File \"{0}\" already exists.".format(path), err=True)
        ctx.exit(USAGE_ERROR_CODE)

    LintConfigGenerator.generate_config(path)
    click.echo(u"Successfully generated {0}".format(path))
    ctx.exit(0)


# Let's Party! 
Example #2
Source File: helper.py    From pygreynoise with MIT License 6 votes vote down vote up
def get_queries(context, input_file, query):
    """Get queries passed as argument or via input file.

    :param context: Subcommand context
    :type context: click.Context
    :param input_file: Input file
    :type input_file: click.File | None
    :param query: GNQL query
    :type query: str | None

    """
    if input_file is None and not sys.stdin.isatty():
        input_file = click.open_file("-")

    if input_file is None and not query:
        click.echo(context.get_help())
        context.exit(-1)

    queries = []
    if input_file is not None:
        queries.extend([line.strip() for line in input_file])
    if query:
        queries.append(query)

    if not queries:
        output = [
            context.command.get_usage(context),
            (
                "Error: at least one query must be passed either as an argument "
                "(QUERY) or through the -i/--input_file option."
            ),
        ]
        click.echo("\n\n".join(output))
        context.exit(-1)

    return queries 
Example #3
Source File: config_parser.py    From genmod with MIT License 6 votes vote down vote up
def read_config(config_file, outfile, loglevel):
    """Parse the config file and print it to the output."""

    from genmod import logger
    from genmod.log import init_log
    init_log(logger, loglevel='DEBUG')

    logger.info("Reading Config File: {0}".format(config_file))

    config_reader = ConfigParser(config_file)

    for plugin in config_reader.plugins:
        logger.info("Found plugin:{0}".format(plugin))
        logger.info("{0}: {1}".format(
            plugin,config_reader.plugins[plugin])
            )

    for category in config_reader.categories:
        logger.info("Category {0}: {1}".format(
            category, config_reader.categories[category]
        )) 
Example #4
Source File: csvexport.py    From pyTenable with MIT License 6 votes vote down vote up
def cli(output, akey, skey, sevs, last_found, cidr, tags, fields, verbose):
    '''
    Export -> CSV Writer

    Generates a CSV File from the vulnerability export using the fields
    specified.
    '''
    # Setup the logging verbosity.
    if verbose == 0:
        logging.basicConfig(level=logging.WARNING)
    if verbose == 1:
        logging.basicConfig(level=logging.INFO)
    if verbose > 1:
        logging.basicConfig(level=logging.DEBUG)
    
    # Instantiate the Tenable.io instance & initiate the vulnerability export.
    tio = TenableIO(akey, skey)
    vulns = tio.exports.vulns(last_found=last_found, severity=list(sevs), 
        cidr_range=cidr, tags=list(tags))
    
    # Pass everything to the CSV generator.
    total = export_vulns_to_csv(output, vulns, *fields)
    click.echo('Processed {} Vulnerabilities'.format(total)) 
Example #5
Source File: files.py    From piicatcher with Apache License 2.0 6 votes vote down vote up
def scan(self):
        logging.debug("Scanning %s" % self._path)
        if os.path.isfile(self._path):
            mime_type = magic.from_file(self._path, mime=True)
            self._files.append(File(self._path, mime_type))
            logging.debug('\t- full path: %s, mime_type: %s' % (os.path.abspath(self._path), mime_type))
        else:
            for root, subdirs, files in os.walk(self._path):
                for filename in files:
                    file_path = os.path.join(root, filename)
                    mime_type = magic.from_file(file_path, mime=True)

                    logging.debug('\t- full path: %s, mime_type: %s' % (file_path, mime_type))
                    self._files.append(File(file_path, mime_type))

        context = {'tokenizer': Tokenizer(), 'regex': RegexScanner(), 'ner': NERScanner()}
        for f in self._files:
            f.scan(context) 
Example #6
Source File: __init__.py    From openag_python with GNU General Public License v3.0 6 votes vote down vote up
def codegen_options(f):
    f = click.option(
        "-c", "--categories", multiple=True, default=default_categories,
        type=click.Choice(all_categories),
        help="A list of the categories of inputs and outputs that should "
        "be enabled"
    )(f)
    f = click.option(
        "-f", "--param_file",
        type=click.File(),
        help="""YAML or JSON file describing the firmware module configuration to be flashed.
        This is the same file that is used for rosparam in the launch file."""
        "code"
    )(f)
    f = click.option(
        "-p", "--plugin", multiple=True, help="Enable a specific plugin"
    )(f)
    f = click.option(
        "-t", "--target", help="PlatformIO target (e.g.  upload)"
    )(f)
    f = click.option(
        "--status_update_interval", default=5,
        help="Minimum interval between driver status updates (in seconds)"
    )(f)
    return f 
Example #7
Source File: generate_cml.py    From StrepHit with GNU General Public License v3.0 5 votes vote down vote up
def generate_crowdflower_interface_template(input_csv, output_html):
    """ Generate CrowFlower interface template based on input data spreadsheet

    :param file input_csv: CSV file with the input data
    :param output_html: File in which to write the output
    :type output_html: file
    :return: 0 on success
    """
    # Get the filed names of the input data spreadsheet
    sheet = csv.DictReader(input_csv)
    fields = sheet.fieldnames
    # Get "fe_[0-9][0-9]" fields
    fe_fields = [f for f in fields if re.match(r'fe_[0-9]{2}$', f)]
    # Get "chunk[0-9][0-9]" fields
    token_fields = [f for f in fields if re.match(r'chunk_[0-9]{2}$', f)]
    # Generate fe blocks for every token field
    fe_blocks = []
    for fe_field in fe_fields:
        fe_blocks.append(FE_TEMPLATE % {'fe_field': fe_field})
    crowdflower_interface_template = HEADER
    # Generate fe_name blocks(question blocks) for every fe_name field
    for idx, token_field in enumerate(token_fields):
        dic = {'question_num': idx + 1, 'token_field': token_field}
        # Add fe blocks into template
        dic['fe_blocks'] = ''.join(fe_blocks)
        # Add current fe_name block or question block into template
        crowdflower_interface_template += (TOKEN_TEMPLATE % dic)

    crowdflower_interface_template += FOOTER
    output_html.write(crowdflower_interface_template)
    return 0 
Example #8
Source File: generate_test_data.py    From scout with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def remove_file(path):
    """Check if a file exists and remove it if so

    Args:
        path(str)
    """
    LOG.info("Removing file %s", path)
    try:
        os.remove(path)
        LOG.info("File %s removed", path)
    except OSError as err:
        LOG.info("File %s does not exists", path) 
Example #9
Source File: generate_test_data.py    From scout with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def generate_hpo_files(genes):
    """Generate files with hpo reduced information"""
    hpo_files = fetch_hpo_files(
        hpogenes=True, hpoterms=True, phenotype_to_terms=True, hpodisease=False
    )

    file_names = {
        "hpogenes": hpogenes_reduced_path,
        "hpoterms": hpoterms_reduced_path,
        "phenotype_to_terms": hpo_phenotype_to_terms_reduced_path,
    }

    for name in file_names:
        hpo_lines = hpo_files[name]
        out_path = file_names[name]
        outfile = open(out_path, "w")
        LOG.info("Writing file %s", out_path)

        for i, line in enumerate(hpo_lines):
            line = line.rstrip()
            if not len(line) > 1:
                continue
            if i == 0:  # Header line
                outfile.write(line + "\n")
                continue
            splitted_line = line.split("\t")
            if name == "hpogenes":
                hgnc_symbol = splitted_line[1]
            elif name == "hpoterms":
                hgnc_symbol = splitted_line[3]
            elif name == "phenotype_to_terms":
                hgnc_symbol = splitted_line[1]

            if hgnc_symbol in genes:
                outfile.write(line + "\n")
        LOG.info("File ready") 
Example #10
Source File: csvexport.py    From pyTenable with MIT License 5 votes vote down vote up
def cli(output, akey, skey, fields, verbose, **kwargs):
    '''
    Export -> CSV Writer

    Generates a CSV File from the vulnerability export using the fields
    specified.
    '''
    # Setup the logging verbosity.
    if verbose == 0:
        logging.basicConfig(level=logging.WARNING)
    if verbose == 1:
        logging.basicConfig(level=logging.INFO)
    if verbose > 1:
        logging.basicConfig(level=logging.DEBUG)

    kwargs['sources'] = list(kwargs['sources'])
    kwargs['tags'] = list(kwargs['tags'])
    filters = dict()
    for key in kwargs.keys():
        if kwargs[key]:
            filters[key] = kwargs[key]

    # Instantiate the Tenable.io instance & initiate the vulnerability export.
    tio = TenableIO(akey, skey)
    assets = tio.exports.assets(**filters)

    # Pass everything to the CSV generator.
    total = export_assets_to_csv(output, assets, *fields)
    click.echo('Processed {} Assets'.format(total)) 
Example #11
Source File: cli.py    From sqlite-utils with Apache License 2.0 5 votes vote down vote up
def insert_upsert_options(fn):
    for decorator in reversed(
        (
            click.argument(
                "path",
                type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
                required=True,
            ),
            click.argument("table"),
            click.argument("json_file", type=click.File(), required=True),
            click.option(
                "--pk", help="Columns to use as the primary key, e.g. id", multiple=True
            ),
            click.option("--nl", is_flag=True, help="Expect newline-delimited JSON"),
            click.option("-c", "--csv", is_flag=True, help="Expect CSV"),
            click.option("--tsv", is_flag=True, help="Expect TSV"),
            click.option(
                "--batch-size", type=int, default=100, help="Commit every X records"
            ),
            click.option(
                "--alter",
                is_flag=True,
                help="Alter existing table to add any missing columns",
            ),
            click.option(
                "--not-null",
                multiple=True,
                help="Columns that should be created as NOT NULL",
            ),
            click.option(
                "--default",
                multiple=True,
                type=(str, str),
                help="Default value that should be set for a column",
            ),
        )
    ):
        fn = decorator(fn)
    return fn 
Example #12
Source File: ui.py    From k2mosaic with MIT License 5 votes vote down vote up
def tpflist(campaign, channel, sc, wget):
    """Prints the Target Pixel File URLS for a given CAMPAIGN/QUARTER and ccd CHANNEL.

    CAMPAIGN can refer to a K2 Campaign (e.g. 'C4') or a Kepler Quarter (e.g. 'Q4').
    """
    try:
        urls = mast.get_tpf_urls(campaign, channel=channel, short_cadence=sc)
        if wget:
            WGET_CMD = 'wget -nH --cut-dirs=6 -c -N '
            print('\n'.join([WGET_CMD + url for url in urls]))
        else:
            print('\n'.join(urls))
    except mast.NoDataFoundException as e:
        click.echo(e) 
Example #13
Source File: cp_group_option_metadata_maker.py    From tutorials with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def find_linenumbers_for_filename_metadata(pipeline):
    """
    Find the linenumbers that contain the filename metadata info within the Metadata module in a CellProfiler pipeline.
    
    * pipeline: a list where each item is a line from a CellProfiler `*.cppipe` file.
    """

    metadata_filename_linenumber_list = []

    for ind, line in enumerate(pipeline):
        if re.match(r'^\s*Metadata source:File name', line) is not None:
            metadata_filename_linenumber_list.append(ind)
            
    return metadata_filename_linenumber_list 
Example #14
Source File: main.py    From fbchat-archive-parser with MIT License 5 votes vote down vote up
def common_options(f):
    f = click.option('-z', '--timezones', callback=validate_timezones, type=click.STRING,
                     help='Timezone disambiguators (TZ=OFFSET,[TZ=OFFSET[...]])')(f)
    f = click.option('-u', '--utc', is_flag=True,
                     help='Use UTC timestamps in the output')(f)
    f = click.option('-n', '--nocolor', is_flag=True,
                     help='Do not colorize output')(f)
    f = click.option('-p', '--noprogress', is_flag=True,
                     help='Do not show progress output')(f)
    f = click.option('-r', '--resolve', callback=collect_facebook_credentials, is_flag=True,
                     help='[BETA] Resolve profile IDs to names by connecting to Facebook')(f)
    f = click.argument('path', type=click.File('rt', encoding='utf8'))(f)
    return f 
Example #15
Source File: configure.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def init():
    """Return top level command handler."""

    @click.command()
    @click.option('--api-service-principal', required=False,
                  envvar='TREADMILL_API_SERVICE_PRINCIPAL',
                  callback=cli.handle_context_opt,
                  help='API service principal for SPNEGO auth (default HTTP)',
                  expose_value=False)
    @click.option('-m', '--manifest', help='App manifest file (stream)',
                  type=click.File('rb'))
    @click.option('--match', help='Application name pattern match')
    @click.option('--delete', help='Delete the app.',
                  is_flag=True, default=False)
    @click.argument('appname', required=False)
    @cli.handle_exceptions(restclient.CLI_REST_EXCEPTIONS)
    def configure(match, manifest, delete, appname):
        """Configure a Treadmill app"""
        restapi = context.GLOBAL.admin_api()
        if appname:
            if delete:
                return _delete(restapi, appname)
            return _configure(restapi, manifest, appname)
        else:
            if not match:
                cli.bad_exit('You must supply a --match option')
            return _list(restapi, match)

    return configure 
Example #16
Source File: binary.py    From MHWorldData with MIT License 5 votes vote down vote up
def dump(file, outfile: click.File):
    "Parses and dumps the file into stdout"
    dumped = binary.dump_file(click.format_filename(file))
    outfile.write(dumped) 
Example #17
Source File: utils.py    From ceph-lcm with Apache License 2.0 5 votes vote down vote up
def ssh_command(func):
    func = click.option(
        "-i", "--identity-file",
        type=click.File(lazy=False),
        default=str(get_private_key_path()),
        help="Path to the private key file",
        show_default=True
    )(func)
    func = click.option(
        "-b", "--batch-size",
        type=int,
        default=20,
        help="By default, command won't connect to all servers "
             "simultaneously, it is trying to process servers in batches. "
             "Negative number or 0 means connect to all hosts",
        show_default=True,
    )(func)

    @functools.wraps(func)
    @click.pass_context
    def decorator(ctx, identity_file, batch_size, *args, **kwargs):
        private_key = asyncssh.import_private_key(identity_file.read())
        batch_size = batch_size if batch_size > 0 else None
        identity_file.close()

        ctx.obj["private_key"] = private_key
        ctx.obj["batch_size"] = batch_size
        ctx.obj["event_loop"] = asyncio.get_event_loop()

        return func(*args, **kwargs)

    return decorator 
Example #18
Source File: cli.py    From gTTS with MIT License 5 votes vote down vote up
def tts_cli(text, file, output, slow, tld, lang, nocheck):
    """ Read <text> to mp3 format using Google Translate's Text-to-Speech API
    (set <text> or --file <file> to - for standard input)
    """

    # stdin for <text>
    if text == '-':
        text = click.get_text_stream('stdin').read()

    # stdout (when no <output>)
    if not output:
        output = click.get_binary_stream('stdout')

    # <file> input (stdin on '-' is handled by click.File)
    if file:
        try:
            text = file.read()
        except UnicodeDecodeError as e:  # pragma: no cover
            log.debug(str(e), exc_info=True)
            raise click.FileError(
                file.name,
                "<file> must be encoded using '%s'." %
                sys_encoding())

    # TTS
    try:
        tts = gTTS(
            text=text,
            lang=lang,
            slow=slow,
            tld=tld,
            lang_check=not nocheck)
        tts.write_to_fp(output)
    except (ValueError, AssertionError) as e:
        raise click.UsageError(str(e))
    except gTTSError as e:
        raise click.ClickException(str(e)) 
Example #19
Source File: migrate.py    From indico-plugins with MIT License 5 votes vote down vote up
def queue_for_rclone(self, obj, bucket, key):
        # XXX: we assume the file is local so the context manager doesn't create a
        # temporary file which becomes invalid afterwards
        with obj.get_local_path() as file_path:
            pass
        while not os.path.exists(file_path):
            raw_input(cformat('\n%{red}File not found on disk: %{yellow}{}').format(file_path))
        try:
            queue_entry = self.rclone_queue[bucket]
        except KeyError:
            tmpdir = tempfile.mkdtemp(prefix='indico-s3-import-')
            self.rclone_queue[bucket] = queue_entry = {
                'path': tmpdir,
                'files': 0,
                'bytes': 0,
            }
        fskey = os.path.join(queue_entry['path'], key)
        fsdir = os.path.dirname(fskey)
        try:
            os.makedirs(fsdir)
        except OSError as exc:
            if exc.errno != errno.EEXIST:
                raise
        os.symlink(file_path, fskey)
        queue_entry['files'] += 1
        queue_entry['bytes'] += obj.size 
Example #20
Source File: decorator.py    From pygreynoise with MIT License 5 votes vote down vote up
def gnql_command(function):
    """Decorator that groups decorators common to gnql query and stats subcommands."""

    @click.command()
    @click.argument("query", required=False)
    @click.option("-k", "--api-key", help="Key to include in API requests")
    @click.option("-i", "--input", "input_file", type=click.File(), help="Input file")
    @click.option(
        "-o", "--output", "output_file", type=click.File(mode="w"), help="Output file"
    )
    @click.option(
        "-f",
        "--format",
        "output_format",
        type=click.Choice(["json", "txt", "xml"]),
        default="txt",
        help="Output format",
    )
    @click.option("-v", "--verbose", count=True, help="Verbose output")
    @pass_api_client
    @click.pass_context
    @echo_result
    @handle_exceptions
    @functools.wraps(function)
    def wrapper(*args, **kwargs):
        return function(*args, **kwargs)

    return wrapper 
Example #21
Source File: cp_group_option_metadata_maker.py    From tutorials with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_metadata_from_foldernames(pipeline, metadata_foldername_linenumber, foldername_list):
    """
    Parse out the metadata for foldernames as defined within a `*.cppipe` file by the Metadata module.
    
    * pipeline: a list where each item is a line from a CellProfiler `*.cppipe` file.
    * metadata_foldername_linenumber: the line number that has the text `'    Metadata source:File name'` found in a pipeline file.
    * foldername_list: a list of the foldernames of images. They should be stripped of path information.
    """
    metadata_list_of_dict = [None]*len(foldername_list)
    
    pipe_re_text = pipeline[metadata_foldername_linenumber + 2]
    
    re_out_pattern_string = re.match(r"\s*Regular expression to extract from folder name:(.*)", pipe_re_text)
    
    pipe_re_text = decode_cellprofiler_pipeline_regular_expression_pattern_string(re_out_pattern_string.group(1))
    
    for ind, foldername in enumerate(foldername_list):
        re_out_metadata = re.match(pipe_re_text, foldername)

        if re_out_metadata:
            re_out_dict = re_out_metadata.groupdict()
            
            metadata_list_of_dict[ind] = re_out_dict
        else:
            metadata_list_of_dict[ind] = {"foldername":foldername}

    return metadata_list_of_dict 
Example #22
Source File: cp_group_option_metadata_maker.py    From tutorials with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_metadata_from_filenames(pipeline, metadata_filename_linenumber, filename_list):
    """
    Parse out the metadata for filenames as defined within a `*.cppipe` file by the Metadata module.
    
    * pipeline: a list where each item is a line from a CellProfiler `*.cppipe` file.
    * metadata_filename_linenumber: the line number that has the text `'    Metadata source:File name'` found in a pipeline file.
    * filename_list: a list of the names of images. They should be stripped of path information.
    """
    metadata_list_of_dict = [None]*len(filename_list)
    
    pipe_re_text = pipeline[metadata_filename_linenumber + 1]
    
    re_out_pattern_string = re.match(r"\s*Regular expression to extract from file name:(.*)", pipe_re_text)
    
    pipe_re_text = decode_cellprofiler_pipeline_regular_expression_pattern_string(re_out_pattern_string.group(1))

    for ind, filename in enumerate(filename_list):
        re_out_metadata = re.match(pipe_re_text, filename)

        if re_out_metadata:
            re_out_dict = re_out_metadata.groupdict()
            
            #re_out_dict = dict([(k,int(v)) if v.isdigit() else (k,v) for (k,v) in re_out_dict.items()])

            metadata_list_of_dict[ind] = re_out_dict
        else:
            metadata_list_of_dict[ind] = {"filename":filename}

    return metadata_list_of_dict 
Example #23
Source File: cli.py    From swag-client with Apache License 2.0 5 votes vote down vote up
def file(ctx, data_dir, data_file):
    """Use the File SWAG Backend"""
    if not ctx.file:
        ctx.data_file = data_file

    if not ctx.data_dir:
        ctx.data_dir = data_dir

    ctx.type = 'file' 
Example #24
Source File: flasher.py    From pros-cli2 with Mozilla Public License 2.0 5 votes vote down vote up
def lsusb(cfg):
    if len(prosflasher.ports.list_com_ports()) == 0 or prosflasher.ports.list_com_ports() is None:
        click.echo('No serial ports found.')
    else:
        click.echo('Available Ports:')
        click.echo(prosflasher.ports.create_port_list(cfg.verbosity > 0))


# @flasher_cli.command(name='dump-cortex', short_help='Dumps user flash contents to a specified file')
# @click.option('-v', '--verbose', is_flag=True)
# @click.argument('file', default=sys.stdout, type=click.File())
# def dump_cortex(file, verbose):
#     pass 
Example #25
Source File: cli.py    From chinese-support-redux with GNU General Public License v3.0 5 votes vote down vote up
def tts_cli(text, file, output, slow, lang, nocheck):
    """ Read <text> to mp3 format using Google Translate's Text-to-Speech API
    (set <text> or --file <file> to - for standard input)
    """

    # stdin for <text>
    if text == '-':
        text = click.get_text_stream('stdin').read()

    # stdout (when no <output>)
    if not output:
        output = click.get_binary_stream('stdout')

    # <file> input (stdin on '-' is handled by click.File)
    if file:
        try:
            text = file.read()
        except UnicodeDecodeError as e:  # pragma: no cover
            log.debug(str(e), exc_info=True)
            raise click.FileError(
                file.name,
                "<file> must be encoded using '%s'." %
                sys_encoding())

    # TTS
    try:
        tts = gTTS(
            text=text,
            lang=lang,
            slow=slow,
            lang_check=not nocheck)
        tts.write_to_fp(output)
    except (ValueError, AssertionError) as e:
        raise click.UsageError(str(e))
    except gTTSError as e:
        raise click.ClickException(str(e)) 
Example #26
Source File: files.py    From piicatcher with Apache License 2.0 5 votes vote down vote up
def dispatch(cls, ns):
        logging.debug("File Dispatch entered")
        explorer = cls(ns)
        explorer.scan()

        if ns.catalog['format'] == "ascii_table":
            headers = ["Path", "Mime/Type", "pii"]
            tableprint.table(explorer.get_tabular(), headers)
        elif ns.catalog['format'] == "json":
            FileStore.save_schemas(explorer) 
Example #27
Source File: files.py    From piicatcher with Apache License 2.0 5 votes vote down vote up
def __init__(self, name, mime_type):
        super(File, self).__init__(name, (), ())
        self._mime_type = mime_type 
Example #28
Source File: cli.py    From covimerage with MIT License 5 votes vote down vote up
def report_data_file_cb(ctx, param, value):
    """Use click.File for data_file only if it is used, to prevent an error
    if it does not exist (click tries to open it always)."""
    if ctx.params.get('profile_file', ()):
        return value
    return click.File('r').convert(value, param, ctx) 
Example #29
Source File: helper.py    From pygreynoise with MIT License 5 votes vote down vote up
def get_ip_addresses(context, input_file, ip_address):
    """Get IP addresses passed as argument or via input file.

    :param context: Subcommand context
    :type context: click.Context
    :param input_file: Input file
    :type input_file: click.File | None
    :param ip_address: IP addresses passed via the ip address argument
    :type query: tuple(str, ...)

    """
    if input_file is None and not sys.stdin.isatty():
        input_file = click.open_file("-")

    if input_file is None and not ip_address:
        click.echo(context.get_help())
        context.exit(-1)

    ip_addresses = []
    if input_file is not None:
        lines = [line.strip() for line in input_file]
        ip_addresses.extend([line for line in lines if validate_ip(line, strict=False)])
    ip_addresses.extend(list(ip_address))

    if not ip_addresses:
        output = [
            context.command.get_usage(context),
            (
                "Error: at least one valid IP address must be passed either as an "
                "argument (IP_ADDRESS) or through the -i/--input_file option."
            ),
        ]
        click.echo("\n\n".join(output))
        context.exit(-1)

    return ip_addresses 
Example #30
Source File: decorator.py    From pygreynoise with MIT License 5 votes vote down vote up
def ip_lookup_command(function):
    """Decorator that groups decorators common to ip and quick subcommand."""

    @click.command()
    @click.argument("ip_address", callback=ip_addresses_parameter, nargs=-1)
    @click.option("-k", "--api-key", help="Key to include in API requests")
    @click.option("-i", "--input", "input_file", type=click.File(), help="Input file")
    @click.option(
        "-o", "--output", "output_file", type=click.File(mode="w"), help="Output file"
    )
    @click.option(
        "-f",
        "--format",
        "output_format",
        type=click.Choice(["json", "txt", "xml"]),
        default="txt",
        help="Output format",
    )
    @pass_api_client
    @click.pass_context
    @echo_result
    @handle_exceptions
    @functools.wraps(function)
    def wrapper(*args, **kwargs):
        return function(*args, **kwargs)

    return wrapper