File: info.py

package info (click to toggle)
python-cooler 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,596 kB
  • sloc: python: 10,555; makefile: 198; sh: 31
file content (66 lines) | stat: -rw-r--r-- 1,361 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys

import click
import simplejson as json

from ..api import Cooler
from ..util import attrs_to_jsonable
from . import cli
from ._util import exit_on_broken_pipe


@cli.command()
@click.argument(
    "cool_uri",
    type=str,
    metavar="COOL_PATH"
)
@click.option(
    "--field", "-f",
    help="Print the value of a specific info field.",
    type=str
)
@click.option(
    "--metadata", "-m",
    help="Print the user metadata in JSON format.",
    is_flag=True,
    default=False,
)
@click.option(
    "--out", "-o",
    help="Output file (defaults to stdout)"
)
@exit_on_broken_pipe(1)
def info(cool_uri, field, metadata, out):
    """
    Display a cooler's info and metadata.

    COOL_PATH : Path to a COOL file or cooler URI.

    """
    c = Cooler(cool_uri)

    # Write output
    if out is None:
        f = sys.stdout
    else:
        f = open(out, "w")
    if metadata:
        json.dump(c.info["metadata"], f, indent=4)
        print(end="\n", file=f)

    elif field is not None:
        try:
            result = c.info[field]
        except KeyError:
            print(f"Data field {field} not found.")
            sys.exit(1)
        print(result, file=f)

    else:
        dct = c.info.copy()
        dct.pop("metadata", None)
        json.dump(attrs_to_jsonable(dct), f, indent=4)
        print(end="\n", file=f)

    f.flush()