Repository navigation
Expand file tree
/
Copy pathformat_tzdata.py
More file actions
executable file
·426 lines (322 loc) · 9.54 KB
/
Copy pathformat_tzdata.py
File metadata and controls
executable file
·426 lines (322 loc) · 9.54 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/usr/bin/env python3
# /// script
# dependencies = [
# "tzdata",
# ]
# requires-python = ">=3.12"
# ///
# this script refreshes world timezone definitions in
# cores/esp8266/TZ.h
#
# use the file output argument or stdout redirect to overwrite the target file
import argparse
import contextlib
import itertools
import datetime
import pathlib
import sys
from dataclasses import dataclass
from operator import attrgetter
from importlib import resources
from importlib.resources.abc import Traversable
from collections.abc import Iterable, Callable
import typing
from typing import TextIO, Self
import tzdata # https://tzdata.readthedocs.io/en/latest/
class NotZoneInfo(Exception):
pass
def tz_from_zonefile(zonefile: Traversable) -> str:
with zonefile.open("rb") as f:
magic = f.read(4)
if magic != b"TZif":
raise NotZoneInfo()
*_, tail = f.readlines()
tz = tail.strip()
tz = tz.decode("ascii")
return tz
@dataclass(frozen=True)
class Tz:
name: str
value: str
@dataclass(frozen=True)
class File:
"""
Newline-delimited file listing IANA keys. Zone information files listed MUST exist.
"""
file: Traversable
@classmethod
def fromString(cls, s: str) -> Self:
return cls(pathlib.Path(s))
def multiple_from_file(root: Traversable, zones: File) -> Iterable[Tz]:
with zones.file.open("r") as f:
names = [name.strip() for name in f.readlines()]
for name in names:
zonefile = root.joinpath(name)
tz = tz_from_zonefile(zonefile)
yield Tz(name, tz)
@dataclass(frozen=True)
class Area:
"""
Area name of one or multiple IANA keys.
--area Europe
This source is ignored when given an invalid area name.
--area DoesntExist"""
name: str
def maybe_multiple_for_area(root: Traversable, area: Area) -> list[Tz]:
out: list[Tz] = []
root = root.joinpath(area.name)
if root.is_dir():
for location in root.iterdir():
if location.is_dir():
continue
try:
tz = tz_from_zonefile(location)
out.append(Tz(f"{area.name}/{location.name}", tz))
except NotZoneInfo:
pass
return out
def split_kv(s: str) -> tuple[str, str]:
key, _, val = s.partition("=")
return (key, val)
@dataclass(frozen=True)
class Link:
"""
Named link to an existing IANA key.
--link Foo/Bar=Europe/London
This source is ignored if named zone information file already exists.
--link Europe/Isle_of_Man=Europe/London
This source is ignored if target zone information file doesn't exist.
--link Europe/Isle_of_Man=Doesnt/Exist
Intended to be combined with either --file or --key"""
name: str
target: str
@classmethod
def fromString(cls, s: str) -> Self:
return cls(*split_kv(s))
def maybe_one_link(root: Traversable, link: Link) -> tuple[Tz] | tuple[()]:
if not root.joinpath(link.name).is_file() and root.joinpath(link.target).is_file():
tz = tz_from_zonefile(root.joinpath(link.target))
return (Tz(link.name, tz),)
return tuple()
@dataclass(frozen=True)
class Key:
"""
IANA key name. This source is ignored if zone information file doesn't exist."""
name: str
def maybe_one_key(root: Traversable, key: Key) -> tuple[Tz] | tuple[()]:
if root.joinpath(key.name).is_file():
tz = tz_from_zonefile(root.joinpath(key.name))
return (Tz(key.name, tz),)
return tuple()
@dataclass(frozen=True)
class Raw:
"""
Inject a named TZ variable."""
name: str
value: str
@classmethod
def fromString(cls, s: str) -> Self:
return cls(*split_kv(s))
def safe_macro_name(name: str) -> str:
swaps = [["-", "m"], ["+", "p"], ["/", "_"]]
for lhs, rhs in swaps:
name = name.replace(lhs, rhs)
return name
def header(info: Iterable[Tz]):
print("// ! ! ! DO NOT EDIT, AUTOMATICALLY GENERATED ! ! !")
print(f"// File created {datetime.datetime.now(tz=datetime.timezone.utc)}")
print(f"// Based on IANA database {tzdata.IANA_VERSION}")
print(
f"// Re-run <esp8266 arduino core>/tools/{pathlib.Path(__file__).name} to update"
)
print()
print("#pragma once")
print()
for tz in info:
print(f'#define TZ_{safe_macro_name(tz.name)}\tPSTR("{tz.value}")')
UTC_NAMES: tuple[str, ...] = (
"Universal",
"UTC",
"UCT",
"Zulu",
"GMT",
"GMT+0",
"GMT-0",
"GMT0",
"Greenwich",
)
def markdown_group(tz: Tz) -> str:
if tz.name in UTC_NAMES:
return "Universal Coordinated Time"
if not "/" in tz.name:
return "Other"
prefix, _, _ = tz.name.partition("/")
return prefix
def markdown(info: Iterable[Tz]):
print(f"> [!NOTE]")
print(f"> Based on IANA database {tzdata.IANA_VERSION}")
print()
print(f"> [!TIP]")
print(f"> Re-run {pathlib.Path(__file__).name} to update")
print()
print("---")
print()
info = sorted(info, key=markdown_group)
grouped = itertools.groupby(info, key=markdown_group)
last = ""
for heading, group in grouped:
if heading != last:
print(f"# {heading}")
print()
last = heading
print("|Name|Value|")
print("|---|---|")
for tz in group:
print(f"|{tz.name}|`{tz.value}`|")
print()
if last == "":
print()
FORMATTERS = {
"header": header,
"markdown": markdown,
}
type Source = File | Link | Key | Raw
DEFAULT_SOURCES: tuple[Source, ...] = (
File(resources.files("tzdata").joinpath("zones")),
Link("Europe/Zaporizhzhia", "Europe/Zaporozhye"),
Link("Europe/Uzhhorod", "Europe/Uzhgorod"),
)
def format_argparse_source(src: Source):
match src:
case File() as file:
return f"--file {file.file}"
case Area() as area:
return f"--area {area.name}"
case Link() as link:
return f"--link {link.name}={link.target}"
case Key() as key:
return f"--key {key.name}"
case Raw() as raw:
return f"--raw {raw.name}={raw.value}"
case _:
raise ValueError(f"Unknown type {type(src)}")
# default=... is awkward to use with groups of arguments
def format_argparse_default_sources(sources: Iterable[Source]) -> str:
return "\n".join(f" `{format_argparse_source(src)}`" for src in sources)
class Formatter(
argparse.ArgumentDefaultsHelpFormatter,
argparse.RawTextHelpFormatter,
):
pass
VERSION = f"""
version:
{tzdata.IANA_VERSION=}
{tzdata.__version__=}
"""
EPILOG = f"""
default sources:
{format_argparse_default_sources(DEFAULT_SOURCES)}
{VERSION}
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(
epilog=EPILOG,
formatter_class=Formatter,
)
sources_group = parser.add_argument_group("iana zone key sources")
sources_group.add_argument(
"--file",
type=File.fromString,
metavar="FILE",
dest="sources",
action="append",
help=File.__doc__,
default=argparse.SUPPRESS,
)
sources_group.add_argument(
"--area",
type=Area,
metavar="NAME",
dest="sources",
action="append",
help=Area.__doc__,
default=argparse.SUPPRESS,
)
sources_group.add_argument(
"--link",
type=Link.fromString,
metavar="NAME=TARGET",
dest="sources",
action="append",
help=Link.__doc__,
default=argparse.SUPPRESS,
)
sources_group.add_argument(
"--key",
type=Key,
metavar="NAME",
dest="sources",
action="append",
help=Key.__doc__,
default=argparse.SUPPRESS,
)
sources_group.add_argument(
"--raw",
type=Raw.fromString,
metavar="NAME=TZ",
dest="sources",
action="append",
help=Raw.__doc__,
default=argparse.SUPPRESS,
)
parser.add_argument(
"--format",
choices=tuple(FORMATTERS.keys()),
default="header",
help="Choose output formatter",
)
parser.add_argument(
"--root",
help="Where do we get raw zoneinfo files from",
type=pathlib.Path,
default=resources.files("tzdata.zoneinfo"),
)
parser.add_argument(
"--version",
action="version",
version=VERSION,
)
parser.add_argument(
"output",
type=str,
nargs="?",
default="-",
)
args = parser.parse_args()
if not getattr(args, "sources", None):
args.sources = DEFAULT_SOURCES
root: Traversable = args.root
mapping: dict[str, Tz] = {}
def update(items: Iterable[Tz]):
for item in items:
if not item.name in mapping:
mapping[item.name] = item
for src in args.sources:
match src:
case File() as file:
update(multiple_from_file(root, file))
case Area() as area:
update(maybe_multiple_for_area(root, area))
case Link() as link:
update(maybe_one_link(root, link))
case Key() as key:
update(maybe_one_key(root, key))
case Raw() as raw:
update(Tz(raw.name, raw.value))
info = sorted(mapping.values(), key=attrgetter("name"))
def maybe_stdout(s: str) -> TextIO:
if s == "-":
return sys.stdout
return pathlib.Path(s).open("w")
with contextlib.redirect_stdout(maybe_stdout(args.output)):
FORMATTERS[args.format](info)