io.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import typing
  2. import io
  3. _open = io.open
  4. SEEK_SET: int = 0
  5. """
  6. Set the pointer position relative to the beginning of the stream.
  7. .. versionadded:: 1.5.0
  8. """
  9. SEEK_CUR: int = 1
  10. """
  11. Set the pointer position relative to the current position.
  12. .. versionadded:: 1.5.0
  13. """
  14. SEEK_END: int = 2
  15. """
  16. Set the pointer position relative to the end of the stream.
  17. .. versionadded:: 1.5.0
  18. """
  19. class BinaryFileIO:
  20. """
  21. Represents a file, opened in binary mode.
  22. .. versionadded:: 1.5.0
  23. """
  24. name: str
  25. """
  26. The name of the file.
  27. .. versionadded:: 1.5.0
  28. """
  29. readable: bool
  30. """
  31. Read-only attribute, indicating if the file is readable.
  32. .. versionadded:: 1.5.0
  33. """
  34. writable: bool
  35. """
  36. Read-only attribute, indicating if the file is writable.
  37. .. versionadded:: 1.5.0
  38. """
  39. def read(self, size: int = -1) -> bytes:
  40. """
  41. Read from the file.
  42. The method will read up to ``size`` bytes and return them.
  43. If ``size`` is not specified, all content up to EOF will be returned.
  44. If the internal pointer is already at EOF, an empty byte string ``b''`` will be returned.
  45. :param size: The maximum number of bytes to read.
  46. :returns: Up to ``size`` bytes.
  47. .. versionadded:: 1.5.0
  48. """
  49. pass
  50. def readline(self, size: int = -1) -> bytes:
  51. """
  52. Read and return one line from the file.
  53. If ``size`` is specified, at most ``size`` bytes will be read.
  54. The line terminator is defined as ``b'\\n'``.
  55. The new line character is included in the return value.
  56. If the internal pointer is at EOF, an empty byte string ``b''`` will be returned.
  57. :param size: The maximum number of bytes to read.
  58. :returns: Up to ``size`` bytes.
  59. .. versionadded:: 1.5.0
  60. """
  61. pass
  62. def readlines(self) -> typing.List[bytes]:
  63. """
  64. Read and return a list of lines from the file.
  65. The line terminator is defined as ``b'\\n'``.
  66. The new line character is included in the return value.
  67. If the internal pointer is at EOF, an empty list will be returned.
  68. :returns: A list of bytes.
  69. .. versionadded:: 1.5.0
  70. """
  71. pass
  72. def write(self, data: bytes) -> int:
  73. """
  74. Write the given bytes to the file.
  75. The number of written bytes will be returned.
  76. This can be less than the length of the provided data.
  77. :param data: The data to write.
  78. :returns: The number of bytes written.
  79. .. versionadded:: 1.5.0
  80. """
  81. pass
  82. def flush(self) -> None:
  83. """
  84. Write the contents of the file buffer to the file on the SD card.
  85. .. versionadded:: 1.5.0
  86. """
  87. pass
  88. def seek(self, offset: int, whence: int = SEEK_SET) -> int:
  89. """
  90. Set the pointer position by the given ``offset``, relative to the position indicated by ``whence``.
  91. The new absolute position will be returned.
  92. :param offset: The offset to use.
  93. :param whence: How to interpret the offset (e.g. :const:`SEEK_SET`).
  94. :returns: The new absolute position.
  95. .. versionadded:: 1.5.0
  96. """
  97. pass
  98. def tell(self) -> int:
  99. """
  100. Get the current pointer position.
  101. :returns: The absolute position of the pointer.
  102. .. versionadded:: 1.5.0
  103. """
  104. pass
  105. def close(self) -> None:
  106. """
  107. Close the file handle.
  108. .. versionadded:: 1.5.0
  109. """
  110. pass
  111. def __enter__(self) -> "BinaryFileIO":
  112. """
  113. This method is invoked, when the instance enters a runtime context.
  114. :returns: The :class:`BinaryFileIO` instance.
  115. .. versionadded:: 1.5.0
  116. """
  117. pass
  118. def __exit__(self, *args, **kwargs) -> None:
  119. """
  120. This method is invoked, when the instance leavs a runtime context.
  121. This basically calls :meth:`close` on the instance.
  122. .. versionadded:: 1.5.0
  123. """
  124. pass
  125. def __del__(self) -> None:
  126. """
  127. This method is invoked, when the garbage collector removes the object.
  128. This basically calls :meth:`close` on the instance.
  129. .. versionadded:: 1.5.0
  130. """
  131. pass
  132. class TextFileIO:
  133. """
  134. Represents a file, opened in text mode.
  135. .. versionadded:: 1.5.0
  136. """
  137. name: str
  138. """
  139. The name of the file.
  140. .. versionadded:: 1.5.0
  141. """
  142. readable: bool
  143. """
  144. Read-only attribute, indicating if the file is readable.
  145. .. versionadded:: 1.5.0
  146. """
  147. writable: bool
  148. """
  149. Read-only attribute, indicating if the file is writable.
  150. .. versionadded:: 1.5.0
  151. """
  152. def read(self, size: int = -1) -> str:
  153. """
  154. Read from the file.
  155. The method will read up to ``size`` characters and return them.
  156. If ``size`` is not specified, all content up to EOF will be returned.
  157. If the internal pointer is already at EOF, an empty string will be returned.
  158. :param size: The maximum number of characters to read.
  159. :returns: Up to ``size`` characters.
  160. .. versionadded:: 1.5.0
  161. """
  162. pass
  163. def readline(self, size: int = -1) -> str:
  164. """
  165. Read and return one line from the file.
  166. If ``size`` is specified, at most ``size`` characters will be read.
  167. The line terminator is defined as ``'\\n'``.
  168. The new line character is included in the return value.
  169. If the internal pointer is at EOF, an empty string will be returned.
  170. :param size: The maximum number of characters to read.
  171. :returns: Up to ``size`` characters.
  172. .. versionadded:: 1.5.0
  173. """
  174. pass
  175. def readlines(self) -> typing.List[str]:
  176. """
  177. Read and return a list of lines from the file.
  178. The line terminator is defined as ``'\\n'``.
  179. The new line character is included in the return value.
  180. If the internal pointer is at EOF, an empty list will be returned.
  181. :returns: A list of strings.
  182. .. versionadded:: 1.5.0
  183. """
  184. pass
  185. def write(self, data: str) -> int:
  186. """
  187. Write the given string to the file.
  188. The number of written characters will be returned.
  189. This can be less than the length of the provided data.
  190. :param data: The data to write.
  191. :returns: The number of characters written.
  192. .. versionadded:: 1.5.0
  193. """
  194. pass
  195. def flush(self) -> None:
  196. """
  197. Write the contents of the file buffer to the file on the SD card.
  198. .. versionadded:: 1.5.0
  199. """
  200. pass
  201. def seek(self, offset: int, whence: int = SEEK_SET) -> int:
  202. """
  203. Set the pointer position by the given ``offset``, relative to the position indicated by ``whence``.
  204. The new absolute position will be returned.
  205. :param offset: The offset to use.
  206. :param whence: How to interpret the offset (e.g. :const:`SEEK_SET`).
  207. :returns: The new absolute position.
  208. .. versionadded:: 1.5.0
  209. """
  210. pass
  211. def tell(self) -> int:
  212. """
  213. Get the current pointer position.
  214. :returns: The absolute position of the pointer.
  215. .. versionadded:: 1.5.0
  216. """
  217. pass
  218. def close(self) -> None:
  219. """
  220. Close the file handle.
  221. .. versionadded:: 1.5.0
  222. """
  223. pass
  224. def __enter__(self) -> "TextFileIO":
  225. """
  226. This method is invoked, when the instance enters a runtime context.
  227. :returns: The :class:`BinaryFileIO` instance.
  228. .. versionadded:: 1.5.0
  229. """
  230. pass
  231. def __exit__(self, *args, **kwargs) -> None:
  232. """
  233. This method is invoked, when the instance leavs a runtime context.
  234. This basically calls :meth:`close` on the instance.
  235. .. versionadded:: 1.5.0
  236. """
  237. pass
  238. def __del__(self) -> None:
  239. """
  240. This method is invoked, when the garbage collector removes the object.
  241. This basically calls :meth:`close` on the instance.
  242. .. versionadded:: 1.5.0
  243. """
  244. pass
  245. def open(path: str, mode: str, *args, **kwargs) -> BinaryFileIO | TextFileIO:
  246. """
  247. Open a file on the file system with the specified mode.
  248. The file path must always be absolute, beginning with ``/ext``.
  249. The following modifiers are available:
  250. .. list-table::
  251. :header-rows: 1
  252. :width: 90%
  253. * - Character
  254. - Description
  255. * - ``'r'``
  256. - Open for reading.
  257. This is the default.
  258. Will fail, if the file not exists.
  259. * - ``'w'``
  260. - Open for writing, truncating an existing file first.
  261. * - ``'b'``
  262. - Open the file in binary mode.
  263. The return value will be a :class:`BinaryFileIO` instance.
  264. * - ``'t'``
  265. - Open the in text mode.
  266. This is the default.
  267. The return value will be a :class:`TextFileIO` instance.
  268. * - ``'+'``
  269. - Open for reading and writing.
  270. Will create the file, if it not exists.
  271. The pointer will be placed at the end of the file.
  272. The modifiers can be combined, e.g. ``'rb+'`` would open a file for reading and writing in binary mode.
  273. :param path: The path to the file to open.
  274. :param mode: How the file should be opened.
  275. :param args: Is ignored at the moment.
  276. :param kwargs: Is ignored at the moment.
  277. .. versionadded:: 1.5.0
  278. """
  279. return io._open(path, mode, *args, **kwargs)