8
8
9
9
from __future__ import annotations
10
10
11
+ import binascii
11
12
import csv
12
13
import datetime
14
+ import gzip
13
15
import io
14
- import itertools
16
+ import json
15
17
import pathlib
16
18
from typing import Callable
17
19
20
22
import platformdirs
21
23
import requests
22
24
23
- DIST_PATH = pathlib .Path (__file__ ).parent / "iersdata_dist.py"
24
-
25
- OLD_TABLE_START : datetime .date | None = None
26
- OLD_TABLE_END : datetime .date | None = None
27
- if DIST_PATH .exists ():
28
- import wwvb .iersdata_dist
29
-
30
- OLD_TABLE_START = wwvb .iersdata_dist .DUT1_DATA_START
31
- OLD_TABLE_END = OLD_TABLE_START + datetime .timedelta (days = len (wwvb .iersdata_dist .DUT1_OFFSETS ) - 1 )
25
+ DIST_PATH = pathlib .Path (__file__ ).parent / "iersdata.json"
32
26
33
27
IERS_URL = "https://datacenter.iers.org/data/csv/finals2000A.all.csv"
34
28
IERS_PATH = pathlib .Path ("finals2000A.all.csv" )
@@ -47,7 +41,7 @@ def _get_text(url: str) -> str:
47
41
return pathlib .Path (url ).read_text (encoding = "utf-8" )
48
42
49
43
50
- def update_iersdata ( # noqa: PLR0915, PLR0912
44
+ def update_iersdata ( # noqa: PLR0915
51
45
target_path : pathlib .Path ,
52
46
) -> None :
53
47
"""Update iersdata.py"""
@@ -124,57 +118,25 @@ def patch(patch_start: datetime.date, patch_end: datetime.date, val: int) -> Non
124
118
assert wwvb_start is not None
125
119
patch (wwvb_start , wwvb_data_stamp + datetime .timedelta (days = 1 ), wwvb_dut1 )
126
120
127
- with target_path .open ("w" , encoding = "utf-8" ) as output :
128
-
129
- def code (* args : str ) -> None :
130
- """Print to the output file"""
131
- print (* args , file = output )
132
-
133
- code ("# -*- python3 -*-" )
134
- code ("# fmt: off" )
135
- code ('"""File generated from public data - not subject to copyright"""' )
136
- code ("# SPDX" + "-FileCopyrightText: Public domain" )
137
- code ("# SPDX" + "-License-Identifier: CC0-1.0" )
138
- code ("# isort: skip_file" )
139
- code ("import datetime" )
140
-
141
- code ("__all__ = ['DUT1_DATA_START', 'DUT1_OFFSETS']" )
142
- code (f"DUT1_DATA_START = { table_start !r} " )
143
- c = sorted (chr (ord ("a" ) + ch + 10 ) for ch in set (offsets ))
144
- code (f"{ ',' .join (c )} = tuple({ '' .join (c )!r} )" )
145
- code (f"DUT1_OFFSETS = str( # { table_start .year :04d} { table_start .month :02d} { table_start .day :02d} " )
146
- line = ""
147
- j = 0
148
-
149
- for val , it in itertools .groupby (offsets ):
150
- part = ""
151
- ch = chr (ord ("a" ) + val + 10 )
152
- sz = len (list (it ))
153
- if j :
154
- part = part + "+"
155
- part = part + ch if sz < 2 else part + f"{ ch } *{ sz } "
156
- j += sz
157
- if len (line + part ) > 60 :
158
- d = table_start + datetime .timedelta (j - 1 )
159
- code (f" { line :<60s} # { d .year :04d} { d .month :02d} { d .day :02d} " )
160
- line = part
161
- else :
162
- line = line + part
163
- d = table_start + datetime .timedelta (j - 1 )
164
- code (f" { line :<60s} # { d .year :04d} { d .month :02d} { d .day :02d} " )
165
- code (")" )
166
121
table_end = table_start + datetime .timedelta (len (offsets ) - 1 )
167
- if OLD_TABLE_START :
168
- print (f"old iersdata covered { OLD_TABLE_START } .. { OLD_TABLE_END } " )
122
+ base = ord ("a" ) + 10
123
+ offsets_bin = bytes (base + ch for ch in offsets )
124
+
125
+ target_path .write_text (
126
+ json .dumps (
127
+ {
128
+ "START" : table_start .isoformat (),
129
+ "OFFSETS_GZ" : binascii .b2a_base64 (gzip .compress (offsets_bin )).decode ("ascii" ).strip (),
130
+ },
131
+ ),
132
+ )
133
+
169
134
print (f"iersdata covers { table_start } .. { table_end } " )
170
135
171
136
172
137
def iersdata_path (callback : Callable [[str , str ], pathlib .Path ]) -> pathlib .Path :
173
138
"""Find out the path for this directory"""
174
- print ("iersdata_path" , callback )
175
- r = callback ("wwvbpy" , "unpythonic.net" ) / "wwvb_iersdata.py"
176
- print (f"iersdata_path { r = !r} " )
177
- return r
139
+ return callback ("wwvbpy" , "unpythonic.net" ) / "iersdata.json"
178
140
179
141
180
142
@click .command ()
0 commit comments