-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgenerate_docstrings.py
194 lines (174 loc) · 5.96 KB
/
generate_docstrings.py
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
"""This script ensures the Rust extension docstrings are identical to the
Python ones.
It does so by parsing the Python docstrings and generating a Rust file with the
same docstrings. This file is then included in the Rust extension.
"""
import enum
import inspect
import sys
from itertools import chain
from whenever import _pywhenever as W
assert sys.version_info >= (
3,
13,
), "This script requires Python 3.13 or later due to how docstrings are rendered."
classes = {
cls
for name, cls in W.__dict__.items()
if (
not name.startswith("_")
and inspect.isclass(cls)
and cls.__module__ == "whenever"
and not issubclass(cls, enum.Enum)
)
}
functions = {
func
for name, func in inspect.getmembers(W)
if (
not name.startswith("_")
and inspect.isfunction(func)
and func.__module__ == "whenever"
)
}
methods = {
getattr(cls, name)
for cls in chain(
classes,
(
# some methods are documented in their ABCs
W._BasicConversions,
W._KnowsLocal,
W._KnowsInstant,
W._KnowsInstantAndLocal,
),
)
for name, m in cls.__dict__.items()
if (
not name.startswith("_")
and (
inspect.isfunction(m)
or
# this catches classmethods
inspect.ismethod(getattr(cls, name))
)
)
}
MAGIC_STRINGS = {
(name, value)
for name, value in W.__dict__.items()
if isinstance(value, str) and name.isupper() and not name.startswith("_")
}
CSTR_TEMPLATE = 'pub(crate) const {varname}: &CStr = c"\\\n{doc}";'
STR_TEMPLATE = 'pub(crate) const {varname}: &str = "{value}";'
SIG_TEMPLATE = "{name}({self}, offset=0, /)\n--\n\n{doc}"
HEADER = """\
// Do not manually edit this file.
// It has been autogenerated by generate_docstrings.py
use std::ffi::CStr;
"""
MANUALLY_DEFINED_SIGS: dict[object, str] = {
W.ZonedDateTime.add: """\
($self, delta=None, /, *, years=0, months=0, days=0, hours=0, \
minutes=0, seconds=0, milliseconds=0, microseconds=0, nanoseconds=0, \
disambiguate=None)""",
W.ZonedDateTime.replace: """\
($self, /, *, year=None, month=None, day=None, hour=None, \
minute=None, second=None, nanosecond=None, tz=None, disambiguate)""",
W.OffsetDateTime.add: """\
($self, delta=None, /, *, years=0, months=0, weeks=0, days=0, \
hours=0, minutes=0, seconds=0, milliseconds=0, microseconds=0, nanoseconds=0, \
ignore_dst=False)""",
W.OffsetDateTime.replace: """\
($self, /, *, year=None, month=None, day=None, hour=None, \
minute=None, second=None, nanosecond=None, offset=None, ignore_dst=False)""",
W.LocalDateTime.add: """\
($self, delta=None, /, *, years=0, months=0, days=0, \
hours=0, minutes=0, seconds=0, milliseconds=0, microseconds=0, nanoseconds=0, \
ignore_dst=False)""",
W.LocalDateTime.replace: """\
($self, /, *, year=None, month=None, day=None, hour=None, \
minute=None, second=None, nanosecond=None)""",
W.Date.replace: "($self, /, *, year=None, month=None, day=None)",
W.MonthDay.replace: "($self, /, *, month=None, day=None)",
W.Time.replace: "($self, /, *, hour=None, minute=None, second=None, nanosecond=None)",
W.YearMonth.replace: "($self, /, *, year=None, month=None)",
W.Instant.add: """\
($self, delta=None, /, *, hours=0, minutes=0, seconds=0, \
milliseconds=0, microseconds=0, nanoseconds=0)""",
}
MANUALLY_DEFINED_SIGS.update(
{
W.ZonedDateTime.subtract: MANUALLY_DEFINED_SIGS[W.ZonedDateTime.add],
W.SystemDateTime.add: MANUALLY_DEFINED_SIGS[W.ZonedDateTime.add],
W.SystemDateTime.subtract: MANUALLY_DEFINED_SIGS[W.ZonedDateTime.add],
W.SystemDateTime.replace: MANUALLY_DEFINED_SIGS[
W.ZonedDateTime.replace
],
W.OffsetDateTime.subtract: MANUALLY_DEFINED_SIGS[W.OffsetDateTime.add],
W.LocalDateTime.subtract: MANUALLY_DEFINED_SIGS[W.LocalDateTime.add],
W.Instant.subtract: MANUALLY_DEFINED_SIGS[W.Instant.add],
}
)
SKIP = {
W._BasicConversions.format_common_iso,
W._BasicConversions.from_py_datetime,
W._BasicConversions.parse_common_iso,
W._KnowsInstant.from_timestamp,
W._KnowsInstant.from_timestamp_millis,
W._KnowsInstant.from_timestamp_nanos,
W._KnowsInstant.now,
W._KnowsLocal.add,
W._KnowsLocal.subtract,
W._KnowsLocal.replace,
W._KnowsLocal.replace_date,
W._KnowsLocal.replace_time,
}
def method_doc(method):
method.__annotations__.clear()
try:
sig = MANUALLY_DEFINED_SIGS[method]
except KeyError:
sig = (
str(inspect.signature(method))
# We use unicode escape of '(' to avoid messing up LSP in editors
.replace("\u0028self", "\u0028$self").replace(
"\u0028cls", "\u0028$type"
)
)
doc = method.__doc__.replace('"', '\\"')
return f"{method.__name__}{sig}\n--\n\n{doc}"
def print_everything():
print(HEADER)
for cls in sorted(classes, key=lambda x: x.__name__):
assert cls.__doc__
print(
CSTR_TEMPLATE.format(
varname=cls.__name__.upper(),
doc=cls.__doc__.replace('"', '\\"'),
)
)
for func in sorted(functions, key=lambda x: x.__name__):
assert func.__doc__
print(
CSTR_TEMPLATE.format(
varname=func.__name__.upper(),
doc=func.__doc__.replace('"', '\\"'),
)
)
for method in sorted(methods, key=lambda x: x.__qualname__):
if method.__doc__ is None or method in SKIP:
continue
qualname = method.__qualname__
if qualname.startswith("_"):
qualname = qualname[1:]
print(
CSTR_TEMPLATE.format(
varname=qualname.replace(".", "_").upper(),
doc=method_doc(method),
)
)
for name, value in sorted(MAGIC_STRINGS):
print(STR_TEMPLATE.format(varname=name, value=value))
if __name__ == "__main__":
print_everything()