/ bulletind / __main__.py
__main__.py
 1  #!/usr/bin/python3
 2  # Copyright (C) 2022 Jeff Epler <jepler@gmail.com>
 3  # SPDX-FileCopyrightText: 2022 Jeff Epler
 4  #
 5  # SPDX-License-Identifier: GPL-3.0-only
 6  
 7  """Commandline interface to 'Bulletin D' data"""
 8  import json as json_
 9  import pathlib
10  
11  import click
12  
13  from . import BulletinDInfo, get_bulletin_d_data, get_cached_bulletin_d_data
14  
15  
16  @click.group()
17  def cli() -> None:
18      """Download and show Bulletin D data in json format"""
19  
20  
21  @cli.command()
22  @click.option(
23      "--update-package-data/--no-update-package-data",
24      default=False,
25      help="Update package data, not user cache",
26  )
27  def update(update_package_data: bool) -> None:
28      """Update data from IERS"""
29      if update_package_data:
30          get_bulletin_d_data([pathlib.Path(__file__).resolve().parent / "data"])
31      else:
32          get_bulletin_d_data()
33  
34  
35  @cli.command()
36  def json() -> None:
37      """Print all data in JSON format"""
38      data = get_cached_bulletin_d_data()
39      data = sorted(data, key=lambda x: x.number)
40      json_data = BulletinDInfo.schema().dump(data, many=True)
41      print(json_.dumps(json_data, indent=4))
42  
43  
44  @cli.command()
45  def table() -> None:
46      "Print all data in tabular format"
47      data = get_cached_bulletin_d_data()
48      data = sorted(data, key=lambda x: x.number)
49      end = None
50      for start, end in zip(data, data[1:]):
51          daycount = (end.start_date - start.start_date).days
52          opt_ls = "LS" if (start.dut1 * end.dut1 < 0) else "  "
53          print(
54              f"{start.start_date} {start.dut1: 4.1f} {daycount:4} {opt_ls} "
55              f"# Bulletin {start.number:4} issued {start.date}, "
56              f"{(start.start_date - start.date).days} early"
57          )
58      if end is not None:
59          print(f"{end.start_date} {end.dut1: 4.1f}")
60  
61  
62  if __name__ == "__main__":
63      cli()