Add command to print info about data sources

This commit is contained in:
Jeff Epler 2024-07-17 21:18:37 -05:00
parent 30ee684c7d
commit ddd9a0e6ab

View file

@ -13,7 +13,7 @@ from dataclasses import dataclass
import click
from . import LeapSecondData, tai
from . import InvalidHashError, LeapSecondData, tai
utc = datetime.timezone.utc
@ -165,5 +165,29 @@ def table(ctx: click.Context, *, start: datetime.datetime, end: datetime.datetim
print(f"{leap_second.start:%Y-%m-%d}: {leap_second.tai_offset.total_seconds():.0f}")
@cli.command
def sources() -> None:
"""Print information about leap-second.list data sources"""
first = False
for location in LeapSecondData.standard_file_sources + LeapSecondData.standard_network_sources:
if not first:
print()
first = False
try:
leap_second_data = LeapSecondData.from_url(location, check_hash=True)
except InvalidHashError:
print(f"{location}: Invalid hash")
leap_second_data = LeapSecondData.from_url(location, check_hash=False)
except Exception as e: # noqa: BLE001
print(f"{location}: {e}")
leap_second_data = None
if leap_second_data is not None:
print(f"{location}: Last updated {leap_second_data.last_updated}")
print(f"{location}: Valid until {leap_second_data.valid_until}")
else:
print(f"{location}: Could not be read")
if __name__ == "__main__": # pragma no cover
cli()