-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathos_.py
278 lines (261 loc) · 8.3 KB
/
os_.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
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
"""
Generate `pyi` from corresponding `rst` docs.
"""
from typing import Final
import rst
from rst2pyi import RST2PyI
__author__ = rst.__author__
__copyright__ = rst.__copyright__
__license__ = rst.__license__
__version__ = "7.5.3" # Version set by https://github.com/hlovatt/tag2ver
def os(shed: RST2PyI) -> None:
shed.module(
name="os",
old='basic "operating system" services',
post_doc=f'''
from abc import abstractmethod
from typing import Final, TypeVar, runtime_checkable, Protocol, overload, Literal
from uio import IOBase
_StrOrBytesT: Final = TypeVar('_StrOrBytesT', str, bytes)
class _PathLike(Protocol[_StrOrBytesT]):
@abstractmethod
def __fspath__(self) -> _StrOrBytesT:
"""Return the file system path representation of the object, preferably as a `str`."""
_AnyPath: Final = str | bytes | _PathLike[str] | _PathLike[bytes]
_FdOrAnyPath: Final = int | _AnyPath
''',
end="General functions",
)
shed.def_(
old=r".. function:: uname()",
new="def uname() -> tuple[str, str, str, str, str]",
indent=0,
)
shed.def_(
old=r".. function:: urandom(n)",
new="def urandom(n: int, /) -> bytes",
end="Filesystem access",
indent=0,
)
shed.def_(
old=r".. function:: chdir(path)",
new="def chdir(path: _FdOrAnyPath, /) -> None",
indent=0,
)
shed.def_(
old=r".. function:: getcwd()", new="def getcwd() -> str", indent=0,
)
shed.def_(
old=r".. function:: ilistdir([dir])",
new=[
"def ilistdir() -> list[tuple[str, int, int] | tuple[str, int, int, int]]",
"def ilistdir(dir: int, /) -> list[tuple[str, int, int] | tuple[str, int, int, int]]",
"def ilistdir(dir: str, /) -> list[tuple[str, int, int] | tuple[str, int, int, int]]",
"def ilistdir(dir: bytes, /) -> list[tuple[bytes, int, int] | tuple[bytes, int, int, int]]",
"def ilistdir(dir: _PathLike[str], /) -> list[tuple[str, int, int] | tuple[str, int, int, int]]",
"def ilistdir(dir: _PathLike[bytes], /) -> list[tuple[bytes, int, int] | tuple[bytes, int, int, int]]",
],
indent=0,
)
shed.def_(
old=r".. function:: listdir([dir])",
new=[
"def listdir() -> list[str]",
"def listdir(dir: int, /) -> list[str]",
"def listdir(dir: str, /) -> list[str]",
"def listdir(dir: bytes, /) -> list[bytes]",
"def listdir(dir: _PathLike[str], /) -> list[str]",
"def listdir(dir: _PathLike[bytes], /) -> list[bytes]",
],
indent=0,
)
shed.def_(
old=r".. function:: mkdir(path)",
new="def mkdir(path: _AnyPath, /) -> None",
indent=0,
)
shed.def_(
old=r".. function:: remove(path)",
new="def remove(path: _AnyPath, /) -> None",
indent=0,
)
shed.def_(
old=r".. function:: rmdir(path)",
new="def rmdir(path: _AnyPath, /) -> None",
indent=0,
)
shed.def_(
old=r".. function:: rename(old_path, new_path)",
new="def rename(old_path: _AnyPath, new_path: _AnyPath, /) -> None",
indent=0,
)
shed.def_(
old=r".. function:: stat(path)",
new="def stat(path: _FdOrAnyPath, /) -> tuple[int, int, int, int, int, int, int, int, int, int]",
indent=0,
)
shed.def_(
old=r".. function:: statvfs(path)",
new="def statvfs(path: _FdOrAnyPath, /) -> tuple[int, int, int, int, int, int, int, int, int, int]",
indent=0,
)
shed.def_(
old=r".. function:: sync()",
new="def sync() -> None",
end="Terminal redirection and duplication",
indent=0,
)
shed.def_(
old=r".. function:: dupterm(stream_object, index=0, /)",
new="def dupterm(stream_object: IOBase | None, index: int = 0, /) -> IOBase | None",
end="Filesystem mounting",
indent=0,
)
mount_def = r".. function:: mount(fsobj, mount_point, *, readonly)"
extra_mount_docs: Final = shed.extra_docs(end=mount_def)
shed.def_(
old=mount_def,
new='def mount(fsobj: "AbstractBlockDev", mount_point: str, /, *, readonly: bool = False) -> IOBase | None',
extra_docs=extra_mount_docs,
indent=0,
)
shed.def_(
old=r".. function:: umount(mount_point)",
new="def umount(mount_point: str, /) -> None",
indent=0,
)
vfs_fat_class: Final = r".. class:: VfsFat(block_dev)"
shed.class_(
name='VfsFat("AbstractBlockDev")', end=vfs_fat_class,
)
shed.def_(
old=vfs_fat_class, new='def __init__(self, block_dev: "AbstractBlockDev", /)',
)
shed.def_(
old=r".. staticmethod:: mkfs(block_dev)",
new="""
@staticmethod
def mkfs(block_dev: "AbstractBlockDev", /) -> None
""",
)
vfs_lfs1_class: Final = r".. class:: VfsLfs1(block_dev, readsize=32, progsize=32, lookahead=32)"
shed.class_(name='VfsLfs1("AbstractBlockDev")', end=vfs_lfs1_class)
shed.def_(
old=vfs_lfs1_class,
new="""
def __init__(
self,
block_dev: "AbstractBlockDev",
readsize: int = 32,
progsize: int = 32,
lookahead: int = 32,
/,
)
""",
)
shed.def_(
old=r".. staticmethod:: mkfs(block_dev, readsize=32, progsize=32, lookahead=32)",
new="""
@staticmethod
def mkfs(
block_dev: "AbstractBlockDev",
readsize: int = 32,
progsize: int = 32,
lookahead: int = 32,
/,
) -> None
""",
)
vfs_lfs2_class: Final = r".. class:: VfsLfs2(block_dev, readsize=32, progsize=32, lookahead=32, mtime=True)"
extra_note_lfs1: Final = shed.extra_notes(end=vfs_lfs2_class)
shed.pyi.classes[-1].doc.extend(extra_note_lfs1)
shed.class_(
name='VfsLfs2("AbstractBlockDev")', end=vfs_lfs2_class,
)
shed.def_(
old=vfs_lfs2_class,
new="""
def __init__(
self,
block_dev: "AbstractBlockDev",
readsize: int = 32,
progsize: int = 32,
lookahead: int = 32,
mtime: bool = True,
/,
)
""",
)
shed.def_(
old=r".. staticmethod:: mkfs(block_dev, readsize=32, progsize=32, lookahead=32)",
new="""
@staticmethod
def mkfs(
block_dev: "AbstractBlockDev",
readsize: int = 32,
progsize: int = 32,
lookahead: int = 32,
mtime: bool = True,
/,
) -> None
""",
)
unused_ref = (
".. _littlefs v1 filesystem format: https://github.com/ARMmbed/littlefs/tree/v1"
)
extra_note_lfs2: Final = shed.extra_notes(end=unused_ref)
shed.pyi.classes[-1].doc.extend(extra_note_lfs2)
shed.consume_containing_line(string=unused_ref)
shed.consume_containing_line(
string=".. _littlefs v2 filesystem format: https://github.com/ARMmbed/littlefs"
)
shed.consume_containing_line(
string=".. _littlefs issue 295: https://github.com/ARMmbed/littlefs/issues/295"
)
shed.consume_containing_line(
string=".. _littlefs issue 347: https://github.com/ARMmbed/littlefs/issues/347"
)
vfs_abd_class: Final = r".. class:: AbstractBlockDev(...)"
shed.class_(
pre_str="@runtime_checkable",
name="AbstractBlockDev(Protocol)",
end=vfs_abd_class,
)
shed.def_(
old=vfs_abd_class, new="def __init__(self)",
)
shed.defs_with_common_description(
cmd=r" .. method:: ",
old2new={
r"readblocks(block_num, buf)": """
@overload
def readblocks(self, block_num: int, buf: bytearray, /) -> None
""",
r"readblocks(block_num, buf, offset)": """
@overload
def readblocks(self, block_num: int, buf: bytearray, offset: int, /) -> None
""",
},
)
shed.defs_with_common_description(
cmd=r" .. method:: ",
old2new={
r"writeblocks(block_num, buf)": """
@overload
def writeblocks(self, block_num: int, buf: bytes | bytearray, /) -> None
""",
r"writeblocks(block_num, buf, offset)": """
@overload
def writeblocks(self, block_num: int, buf: bytes | bytearray, offset: int, /) -> None
""",
},
)
shed.def_(
old=r".. method:: ioctl(op, arg)",
new=[
"def ioctl(self, op: int, arg: int, /) -> int | None",
"def ioctl(self, op: Literal[4, 5], arg: int, /) -> int",
"def ioctl(self, op: Literal[1, 2, 3, 6], arg: int, /) -> int | None",
],
)
shed.write(u_also=True)