Python click.open_file() Examples

The following are 30 code examples of click.open_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: indexing.py    From ms_deisotope with Apache License 2.0 6 votes vote down vote up
def idzip_compression(path, output):
        '''Compress a file using  idzip, a gzip-compatible format with random access support.
        '''
        if output is None:
            output = '-'
        with click.open_file(output, mode='wb') as outfh:
            writer = _compression.GzipFile(fileobj=outfh, mode='wb')
            with click.open_file(path, 'rb') as infh:
                try:
                    infh_wrap = io.BufferedReader(infh)
                    header = infh_wrap.peek(2)
                    if _compression.starts_with_gz_magic(header):
                        click.echo("Detected gzip input file", err=True)
                        infh_wrap = _compression.GzipFile(fileobj=infh_wrap)
                except AttributeError:
                    infh_wrap = infh
                buffer_size = _compression.WRITE_BUFFER_SIZE
                chunk = infh_wrap.read(buffer_size)
                while chunk:
                    writer.write(chunk)
                    chunk = infh_wrap.read(buffer_size)
            writer.close() 
Example #2
Source File: spec.py    From pyhf with Apache License 2.0 6 votes vote down vote up
def combine(workspace_one, workspace_two, join, output_file):
    """
    Combine two workspaces into a single workspace.

    See :func:`pyhf.workspace.Workspace.combine` for more information.
    """
    with click.open_file(workspace_one, 'r') as specstream:
        spec_one = json.load(specstream)

    with click.open_file(workspace_two, 'r') as specstream:
        spec_two = json.load(specstream)

    ws_one = Workspace(spec_one)
    ws_two = Workspace(spec_two)
    combined_ws = Workspace.combine(ws_one, ws_two, join=join)

    if output_file is None:
        click.echo(json.dumps(combined_ws, indent=4, sort_keys=True))
    else:
        with open(output_file, 'w+') as out_file:
            json.dump(combined_ws, out_file, indent=4, sort_keys=True)
        log.debug("Written to {0:s}".format(output_file)) 
Example #3
Source File: spec.py    From pyhf with Apache License 2.0 6 votes vote down vote up
def rename(workspace, output_file, channel, sample, modifier, measurement):
    """
    Rename components of the workspace.

    See :func:`pyhf.workspace.Workspace.rename` for more information.
    """
    with click.open_file(workspace, 'r') as specstream:
        spec = json.load(specstream)

    ws = Workspace(spec)
    renamed_ws = ws.rename(
        channels=dict(channel),
        samples=dict(sample),
        modifiers=dict(modifier),
        measurements=dict(measurement),
    )

    if output_file is None:
        click.echo(json.dumps(renamed_ws, indent=4, sort_keys=True))
    else:
        with open(output_file, 'w+') as out_file:
            json.dump(renamed_ws, out_file, indent=4, sort_keys=True)
        log.debug("Written to {0:s}".format(output_file)) 
Example #4
Source File: patchset.py    From pyhf with Apache License 2.0 6 votes vote down vote up
def verify(background_only, patchset):
    """
    Verify the patchset digests against a background-only workspace specification. Verified if no exception was raised.

    Raises:
        :class:`~pyhf.exceptions.PatchSetVerificationError`: if the patchset cannot be verified against the workspace specification

    Returns:
        None
    """
    with click.open_file(background_only, 'r') as specstream:
        spec = json.load(specstream)

    ws = Workspace(spec)

    with click.open_file(patchset, 'r') as fstream:
        patchset_spec = json.load(fstream)

    patchset = PatchSet(patchset_spec)
    patchset.verify(ws)

    click.echo("All good.") 
Example #5
Source File: cli.py    From scfcli with Apache License 2.0 6 votes vote down vote up
def do_invoke(template, namespace, function, env_vars, event, no_event, debug_port, debug_args, quiet):
    if no_event:
        event_data = "{}"
    else:
        Operation('Enter an event:', color="green").echo()
        with click.open_file(event, 'r', encoding="utf-8") as f:
            event_data = f.read()
    try:
        with InvokeContext(
                template_file=template,
                namespace=namespace,
                function=function,
                env_file=env_vars,
                debug_port=debug_port,
                debug_args=debug_args,
                event=event_data,
                is_quiet=quiet
        ) as context:
            context.invoke()
    except Exception as e:
        Operation(e, err_msg=traceback.format_exc(), level="ERROR").no_output()
        raise e 
Example #6
Source File: cjio.py    From cjio with MIT License 6 votes vote down vote up
def save_cmd(filename, indent, textures):
    """Save the city model to a CityJSON file."""
    def processor(cm):
        print_cmd_status("Saving CityJSON to a file (%s)" % (filename))
        f = os.path.basename(filename)
        d = os.path.abspath(os.path.dirname(filename))
        if not os.path.isdir(d):
            os.makedirs(d)
        p = os.path.join(d, f)
        try:
            fo = click.open_file(p, mode='w')
            if textures:
                cm.copy_textures(textures, p)
            if indent == 0:
                json_str = json.dumps(cm.j, separators=(',',':'))
                fo.write(json_str)
            else:
                json_str = json.dumps(cm.j, indent=indent)
                fo.write(json_str)
        except IOError as e:
            raise click.ClickException('Invalid output file: "%s".\n%s' % (p, e))                
        return cm
    return processor 
Example #7
Source File: error_logging.py    From kgx with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def append_errors_to_file(filename:str, errors:List[Error], time) -> None:
    """
    Creates a single file that logs all errors
    """
    dirname = os.path.dirname(filename)
    if dirname != '':
        os.makedirs(dirname, exist_ok=True)

    with click.open_file(filename, 'a+') as f:
        f.write('--- {} ---\n'.format(time))

        for e in errors:
            if isinstance(e, NodeError):
                f.write('node({})\t{}\n'.format(e.node, e.message))
            elif isinstance(e, EdgeError):
                f.write('edge({}, {})\t{}\n'.format(e.subject, e.object, e.message))
            else:
                raise Exception('Expected type {} but got: {}'.format(Error, type(error)))

        click.echo('Logged {} errors to {}'.format(len(errors), filename)) 
Example #8
Source File: dnsgate.py    From dnsgate with MIT License 6 votes vote down vote up
def write_output_file(config, domains_combined):
    config_dict = make_config_dict()

    leprint("Writing output file: %s in %s format", config.output, config.mode,
           level=LOG['INFO'])
    with click.open_file(config.output, 'wb', atomic=True, lazy=True) as fh:
        fh.write(make_output_file_header(config_dict))
        for domain in domains_combined:
            if config.mode == 'dnsmasq':
                if config.dest_ip:
                    dnsmasq_line = 'address=/.' + domain.decode('utf8') + \
                                   '/' + config.dest_ip + '\n'
                else:
                    dnsmasq_line = 'server=/.' + domain.decode('utf8') + \
                                   '/' '\n'  # return NXDOMAIN
                fh.write(dnsmasq_line.encode('utf8'))
            elif config.mode == 'hosts':
                if config.dest_ip:
                    hosts_line = config.dest_ip + ' ' + domain.decode('utf8') + '\n'
                else:
                    hosts_line = '127.0.0.1' + ' ' + domain.decode('utf8') + '\n'
                fh.write(hosts_line.encode('utf8')) 
Example #9
Source File: report.py    From temci with GNU General Public License v3.0 6 votes vote down vote up
def report(self) -> t.Optional[str]:
        """
        Create an report and output it as configured.

        :return: the report string if ``to_string == True``
        """
        if not self.misc["out"] == "-" and not os.path.exists(os.path.dirname(self.misc["out"])):
            logging.error("Folder for report ({}) doesn't exist".format(os.path.dirname(self.misc["out"])))
            exit(1)
        with click.open_file(self.misc["out"], mode='w') as f:
            import tablib
            data = tablib.Dataset(itertools.chain.from_iterable(x.split(",") for x in self.misc["columns"]))
            for row in self._table():
                data.append(row)
            f.write(data.csv)
            chown(f) 
Example #10
Source File: datasets.py    From mapbox-cli-py with MIT License 6 votes vote down vote up
def list(ctx, output):
    """List datasets.

    Prints a list of objects describing datasets.

        $ mapbox datasets list

    All endpoints require authentication. An access token with
    `datasets:read` scope is required, see `mapbox --help`.
    """

    stdout = click.open_file(output, 'w')
    service = ctx.obj.get('service')
    res = service.list()

    if res.status_code == 200:
        click.echo(res.text, file=stdout)
    else:
        raise MapboxCLIException(res.text.strip()) 
Example #11
Source File: subcommand.py    From pygreynoise with MIT License 6 votes vote down vote up
def analyze(
    context, api_client, api_key, input_file, output_file, output_format, verbose
):
    """Analyze the IP addresses in a log file, stdin, etc."""
    if input_file is None:
        if sys.stdin.isatty():
            output = [
                context.command.get_usage(context),
                (
                    "Error: at least one text file must be passed "
                    "either through the -i/--input_file option or through a shell pipe."
                ),
            ]
            click.echo("\n\n".join(output))
            context.exit(-1)
        else:
            input_file = click.open_file("-")
    if output_file is None:
        output_file = click.open_file("-", mode="w")

    result = api_client.analyze(input_file)
    return result 
Example #12
Source File: subcommand.py    From pygreynoise with MIT License 6 votes vote down vote up
def filter(context, api_client, api_key, input_file, output_file, noise_only):
    """Filter the noise from a log file, stdin, etc."""
    if input_file is None:
        if sys.stdin.isatty():
            output = [
                context.command.get_usage(context),
                (
                    "Error: at least one text file must be passed "
                    "either through the -i/--input_file option or through a shell pipe."
                ),
            ]
            click.echo("\n\n".join(output))
            context.exit(-1)
        else:
            input_file = click.open_file("-")
    if output_file is None:
        output_file = click.open_file("-", mode="w")

    for chunk in api_client.filter(input_file, noise_only=noise_only):
        output_file.write(ANSI_MARKUP(chunk)) 
Example #13
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 #14
Source File: command_line.py    From ivona-speak with MIT License 6 votes vote down vote up
def synthesize(access_key, secret_key, output_file, voice_name, voice_language,
               codec, text):
    """Synthesize passed text and save it as an audio file"""
    try:
        ivona_api = IvonaAPI(
            access_key, secret_key,
            voice_name=voice_name, language=voice_language, codec=codec,
        )
    except (ValueError, IvonaAPIException) as e:
        raise click.ClickException("Something went wrong: {}".format(repr(e)))

    with click.open_file(output_file, 'wb') as file:
        ivona_api.text_to_speech(text, file)

    click.secho(
        "File successfully saved as '{}'".format(output_file),
        fg='green',
    ) 
Example #15
Source File: datasets.py    From mapbox-cli-py with MIT License 6 votes vote down vote up
def read_feature(ctx, dataset, fid, output):
    """Read a dataset feature.

    Prints a GeoJSON representation of the feature.

        $ mapbox datasets read-feature dataset-id feature-id

    All endpoints require authentication. An access token with
    `datasets:read` scope is required, see `mapbox --help`.
    """

    stdout = click.open_file(output, 'w')
    service = ctx.obj.get('service')
    res = service.read_feature(dataset, fid)

    if res.status_code == 200:
        click.echo(res.text, file=stdout)
    else:
        raise MapboxCLIException(res.text.strip()) 
Example #16
Source File: datasets.py    From mapbox-cli-py with MIT License 6 votes vote down vote up
def read_dataset(ctx, dataset, output):
    """Read the attributes of a dataset.

    Prints a JSON object containing the attributes
    of a dataset. The attributes: owner (a Mapbox account),
    id (dataset id), created (Unix timestamp), modified
    (timestamp), name (string), and description (string).

        $ mapbox datasets read-dataset dataset-id

    All endpoints require authentication. An access token with
    `datasets:read` scope is required, see `mapbox --help`.
    """

    stdout = click.open_file(output, 'w')
    service = ctx.obj.get('service')
    res = service.read_dataset(dataset)

    if res.status_code == 200:
        click.echo(res.text, file=stdout)
    else:
        raise MapboxCLIException(res.text.strip()) 
Example #17
Source File: datasets.py    From mapbox-cli-py with MIT License 6 votes vote down vote up
def list_features(ctx, dataset, reverse, start, limit, output):
    """Get features of a dataset.

    Prints the features of the dataset as a GeoJSON feature collection.

        $ mapbox datasets list-features dataset-id

    All endpoints require authentication. An access token with
    `datasets:read` scope is required, see `mapbox --help`.
    """

    stdout = click.open_file(output, 'w')
    service = ctx.obj.get('service')
    res = service.list_features(dataset, reverse, start, limit)

    if res.status_code == 200:
        click.echo(res.text, file=stdout)
    else:
        raise MapboxCLIException(res.text.strip()) 
Example #18
Source File: run_driver.py    From temci with GNU General Public License v3.0 5 votes vote down vote up
def store_example_config(cls, file: str):
        import click
        with click.open_file(file, "w") as f:
            print(List(cls.get_full_block_typescheme()).get_default_yaml(), file=f) 
Example #19
Source File: utils.py    From crimson with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_handle(
    input: Union[str, PathLike, IO],
    encoding: Optional[str] = None,
    mode: str = "r",
) -> Generator[TextIO, None, None]:
    """Context manager for opening files.

    This function returns a file handle of the given file name. You may also
    give an open file handle, in which case the file handle will be yielded
    immediately. The purpose of this is to allow the context manager handle
    both file objects and file names as inputs.

    If a file handle is given, it is not closed upon exiting the context.
    If a file name is given, it will be closed upon exit.

    :param input: Handle of open file or file name.
    :param encoding: Encoding of the file. Ignored if input is file handle.
    :param mode: Mode for opening file. Ignored if input is file handle.

    """
    if isinstance(input, (str, Path)):
        fh = click.open_file(f"{input}", mode=mode, encoding=encoding)
    else:
        fh = input

    yield fh

    if isinstance(input, str):
        fh.close() 
Example #20
Source File: build_processor.py    From temci with GNU General Public License v3.0 5 votes vote down vote up
def store_example_config(cls, file: str):
        import click
        with click.open_file(file, "w") as f:
            print(List(cls.block_scheme).get_default_yaml(), file=f) 
Example #21
Source File: patchset.py    From pyhf with Apache License 2.0 5 votes vote down vote up
def extract(patchset, name, output_file, with_metadata):
    """
    Extract a patch from a patchset.

    Raises:
        :class:`~pyhf.exceptions.InvalidPatchLookup`: if the provided patch name is not in the patchset

    Returns:
        jsonpatch (:obj:`list`): A list of jsonpatch operations to apply to a workspace.
    """
    with click.open_file(patchset, 'r') as fstream:
        patchset_spec = json.load(fstream)

    patchset = PatchSet(patchset_spec)
    patch = patchset[name]

    if with_metadata:
        result = {'metadata': patch.metadata, 'patch': patch.patch}
        result['metadata'].update(patchset.metadata)
    else:
        result = patch.patch

    if output_file:
        with open(output_file, 'w+') as out_file:
            json.dump(result, out_file, indent=4, sort_keys=True)
        log.debug("Written to {0:s}".format(output_file))
    else:
        click.echo(json.dumps(result, indent=4, sort_keys=True)) 
Example #22
Source File: decorator.py    From pygreynoise with MIT License 5 votes vote down vote up
def echo_result(function):
    """Decorator that prints subcommand results correctly formatted.

    :param function: Subcommand that returns a result from the API.
    :type function: callable
    :returns: Wrapped function that prints subcommand results
    :rtype: callable

    """

    @functools.wraps(function)
    def wrapper(*args, **kwargs):
        result = function(*args, **kwargs)
        context = click.get_current_context()
        params = context.params
        output_format = params["output_format"]
        formatter = FORMATTERS[output_format]
        if isinstance(formatter, dict):
            # For the text formatter, there's a separate formatter for each subcommand
            formatter = formatter[context.command.name]

        output = formatter(result, params.get("verbose", False)).strip("\n")
        click.echo(
            output, file=params.get("output_file", click.open_file("-", mode="w"))
        )

    return wrapper 
Example #23
Source File: rootio.py    From pyhf with Apache License 2.0 5 votes vote down vote up
def json2xml(workspace, output_dir, specroot, dataroot, resultprefix, patch):
    """Convert pyhf JSON back to XML + ROOT files."""
    try:
        import uproot

        assert uproot
    except ImportError:
        log.error(
            "json2xml requires uproot, please install pyhf using the "
            "xmlio extra: python -m pip install pyhf[xmlio]"
        )
    from .. import writexml

    os.makedirs(output_dir, exist_ok=True)
    with click.open_file(workspace, 'r') as specstream:
        spec = json.load(specstream)
        for pfile in patch:
            patch = json.loads(click.open_file(pfile, 'r').read())
            spec = jsonpatch.JsonPatch(patch).apply(spec)
        os.makedirs(Path(output_dir).joinpath(specroot), exist_ok=True)
        os.makedirs(Path(output_dir).joinpath(dataroot), exist_ok=True)
        with click.open_file(
            Path(output_dir).joinpath(f'{resultprefix}.xml'), 'w'
        ) as outstream:
            outstream.write(
                writexml.writexml(
                    spec,
                    Path(output_dir).joinpath(specroot),
                    Path(output_dir).joinpath(dataroot),
                    resultprefix,
                ).decode('utf-8')
            ) 
Example #24
Source File: cli.py    From make-surface with MIT License 5 votes vote down vote up
def fillfacets(infile, sampleraster, output, noproject, bidxs, zooming, batchprint, outputgeojson, color):
    """
    Use GeoJSON-like geometry to get raster values
    """
    try:
        input = click.open_file(infile).readlines()
    except IOError:
        input = [infile]

    makesurface.fillfacets(input, sampleraster, noproject, output, bidxs, zooming, batchprint, outputgeojson, color) 
Example #25
Source File: features.py    From cligj with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def normalize_feature_inputs(ctx, param, value):
    """Click callback that normalizes feature input values.

    Returns a generator over features from the input value.

    Parameters
    ----------
    ctx: a Click context
    param: the name of the argument or option
    value: object
        The value argument may be one of the following:

        1. A list of paths to files containing GeoJSON feature
           collections or feature sequences.
        2. A list of string-encoded coordinate pairs of the form
           "[lng, lat]", or "lng, lat", or "lng lat".

        If no value is provided, features will be read from stdin.
    """
    for feature_like in value or ('-',):
        try:
            with click.open_file(feature_like) as src:
                for feature in iter_features(iter(src)):
                    yield feature
        except IOError:
            coords = list(coords_from_query(feature_like))
            yield {
                'type': 'Feature',
                'properties': {},
                'geometry': {
                    'type': 'Point',
                    'coordinates': coords}} 
Example #26
Source File: features.py    From cligj with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def iter_query(query):
    """Accept a filename, stream, or string.
    Returns an iterator over lines of the query."""
    try:
        itr = click.open_file(query).readlines()
    except IOError:
        itr = [query]
    return itr 
Example #27
Source File: cli.py    From scfcli with Apache License 2.0 5 votes vote down vote up
def _get_event(event_file):
    if event_file == STD_IN:
        Operation('read event from stdin').process()

    with click.open_file(event_file, 'r') as f:
        return f.read() 
Example #28
Source File: report.py    From temci with GNU General Public License v3.0 5 votes vote down vote up
def report(self, with_tester_results: bool = True, to_string: bool = False) -> t.Optional[str]:
        """
        Create an report and output it as configured.

        :param with_tester_results: include the hypothesis tester results
        :param to_string: return the report as a string and don't output it?
        :return: the report string if ``to_string == True``
        """
        output = [""]

        with_tester_results = with_tester_results and self.misc["with_tester_results"]

        baselines = filter_runs(self.stats_helper.runs, self.misc["baseline"]) if self.misc["baseline"] else []

        def string_printer(line: str, **args):
            output[0] += str(line) + "\n"

        with click.open_file(self.misc["out"], mode='w') as f:
            print_func = string_printer if to_string else lambda x: print(x, file=f)
            if self.misc["mode"] == "auto":
                single, clusters = self.stats_helper.get_description_clusters_and_single()
                self._report_cluster("single runs", single, print_func, with_tester_results, baselines)
                self._report_clusters(clusters, print_func, with_tester_results, baselines)
            if self.misc["mode"] in ["both", "single"]:
                self._report_cluster("all runs",
                                     self.stats_helper.runs,
                                     print_func,
                                     with_tester_results,
                                     baselines)
            if self.misc["mode"] in ["both", "cluster"]:
                self._report_clusters(self.stats_helper.get_description_clusters(), print_func, with_tester_results,
                                      baselines)
                print_func("")
            if self.misc["report_errors"] and len(self.stats_helper.errorneous_runs) > 0:
                self._report_errors(self.stats_helper.errorneous_runs, print_func)
            chown(f)
        if to_string:
            return output[0] 
Example #29
Source File: cli.py    From scfcli with Apache License 2.0 5 votes vote down vote up
def _get_event(event_file):
    if event_file == STD_IN:
        Operation('read event from stdin').echo()

    with click.open_file(event_file, 'r') as f:
        return f.read() 
Example #30
Source File: error_logging.py    From kgx with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def append_errors_to_files(dirname:str, errors:List[Error], time) -> None:
    """
    Creates a series of files that logs all errors, each error type being logged
    to its own file.
    """
    os.makedirs(dirname, exist_ok=True)

    error_dict = defaultdict(list)
    for error in errors:
        error_dict[error.error_type].append(error)

    for error_type, typed_errors in error_dict.items():
        if error_type is None:
            error_type = 'default.log'
        else:
            error_type = error_type.replace(' ', '_') + '.log'

        filename = os.path.join(dirname, error_type)
        with click.open_file(filename, 'a+') as f:
            f.write('--- {} ---\n'.format(time))
            for e in typed_errors:
                if isinstance(e, NodeError):
                    f.write('node({})\t{}\n'.format(e.node, e.message))
                elif isinstance(e, EdgeError):
                    f.write('edge({}, {})\t{}\n'.format(e.subject, e.object, e.message))
                else:
                    raise Exception('Expected type {} but got: {}'.format(Error, type(e)))

            click.echo('Logged {} errors to {}'.format(len(typed_errors), filename))