|
|
@@ -4,57 +4,58 @@ import io
|
|
|
_open = io.open
|
|
|
|
|
|
SEEK_SET: int = 0
|
|
|
-'''
|
|
|
+"""
|
|
|
Set the pointer position relative to the beginning of the stream.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
-'''
|
|
|
+"""
|
|
|
|
|
|
SEEK_CUR: int = 1
|
|
|
-'''
|
|
|
+"""
|
|
|
Set the pointer position relative to the current position.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
-'''
|
|
|
+"""
|
|
|
|
|
|
SEEK_END: int = 2
|
|
|
-'''
|
|
|
+"""
|
|
|
Set the pointer position relative to the end of the stream.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
-'''
|
|
|
+"""
|
|
|
+
|
|
|
|
|
|
class BinaryFileIO:
|
|
|
- '''
|
|
|
+ """
|
|
|
Represents a file, opened in binary mode.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
|
|
|
name: str
|
|
|
- '''
|
|
|
+ """
|
|
|
The name of the file.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
|
|
|
readable: bool
|
|
|
- '''
|
|
|
+ """
|
|
|
Read-only attribute, indicating if the file is readable.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
|
|
|
writable: bool
|
|
|
- '''
|
|
|
+ """
|
|
|
Read-only attribute, indicating if the file is writable.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
|
|
|
def read(self, size: int = -1) -> bytes:
|
|
|
- '''
|
|
|
- Read from the file.
|
|
|
+ """
|
|
|
+ Read from the file.
|
|
|
The method will read up to ``size`` bytes and return them.
|
|
|
If ``size`` is not specified, all content up to EOF will be returned.
|
|
|
If the internal pointer is already at EOF, an empty byte string ``b''`` will be returned.
|
|
|
@@ -63,11 +64,11 @@ class BinaryFileIO:
|
|
|
:returns: Up to ``size`` bytes.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def readline(self, size: int = -1) -> bytes:
|
|
|
- '''
|
|
|
+ """
|
|
|
Read and return one line from the file.
|
|
|
If ``size`` is specified, at most ``size`` bytes will be read.
|
|
|
The line terminator is defined as ``b'\\n'``.
|
|
|
@@ -78,11 +79,11 @@ class BinaryFileIO:
|
|
|
:returns: Up to ``size`` bytes.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def readlines(self) -> typing.List[bytes]:
|
|
|
- '''
|
|
|
+ """
|
|
|
Read and return a list of lines from the file.
|
|
|
The line terminator is defined as ``b'\\n'``.
|
|
|
The new line character is included in the return value.
|
|
|
@@ -91,11 +92,11 @@ class BinaryFileIO:
|
|
|
:returns: A list of bytes.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def write(self, data: bytes) -> int:
|
|
|
- '''
|
|
|
+ """
|
|
|
Write the given bytes to the file.
|
|
|
The number of written bytes will be returned.
|
|
|
This can be less than the length of the provided data.
|
|
|
@@ -104,19 +105,19 @@ class BinaryFileIO:
|
|
|
:returns: The number of bytes written.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def flush(self) -> None:
|
|
|
- '''
|
|
|
+ """
|
|
|
Write the contents of the file buffer to the file on the SD card.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def seek(self, offset: int, whence: int = SEEK_SET) -> int:
|
|
|
- '''
|
|
|
+ """
|
|
|
Set the pointer position by the given ``offset``, relative to the position indicated by ``whence``.
|
|
|
The new absolute position will be returned.
|
|
|
|
|
|
@@ -125,86 +126,87 @@ class BinaryFileIO:
|
|
|
:returns: The new absolute position.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def tell(self) -> int:
|
|
|
- '''
|
|
|
+ """
|
|
|
Get the current pointer position.
|
|
|
|
|
|
:returns: The absolute position of the pointer.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def close(self) -> None:
|
|
|
- '''
|
|
|
+ """
|
|
|
Close the file handle.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
- def __enter__(self) -> 'BinaryFileIO':
|
|
|
- '''
|
|
|
+ def __enter__(self) -> "BinaryFileIO":
|
|
|
+ """
|
|
|
This method is invoked, when the instance enters a runtime context.
|
|
|
|
|
|
:returns: The :class:`BinaryFileIO` instance.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def __exit__(self, *args, **kwargs) -> None:
|
|
|
- '''
|
|
|
+ """
|
|
|
This method is invoked, when the instance leavs a runtime context.
|
|
|
This basically calls :meth:`close` on the instance.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def __del__(self) -> None:
|
|
|
- '''
|
|
|
+ """
|
|
|
This method is invoked, when the garbage collector removes the object.
|
|
|
This basically calls :meth:`close` on the instance.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
+
|
|
|
class TextFileIO:
|
|
|
- '''
|
|
|
+ """
|
|
|
Represents a file, opened in text mode.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
|
|
|
name: str
|
|
|
- '''
|
|
|
+ """
|
|
|
The name of the file.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
|
|
|
readable: bool
|
|
|
- '''
|
|
|
+ """
|
|
|
Read-only attribute, indicating if the file is readable.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
|
|
|
writable: bool
|
|
|
- '''
|
|
|
+ """
|
|
|
Read-only attribute, indicating if the file is writable.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
|
|
|
def read(self, size: int = -1) -> str:
|
|
|
- '''
|
|
|
- Read from the file.
|
|
|
+ """
|
|
|
+ Read from the file.
|
|
|
The method will read up to ``size`` characters and return them.
|
|
|
If ``size`` is not specified, all content up to EOF will be returned.
|
|
|
If the internal pointer is already at EOF, an empty string will be returned.
|
|
|
@@ -213,11 +215,11 @@ class TextFileIO:
|
|
|
:returns: Up to ``size`` characters.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def readline(self, size: int = -1) -> str:
|
|
|
- '''
|
|
|
+ """
|
|
|
Read and return one line from the file.
|
|
|
If ``size`` is specified, at most ``size`` characters will be read.
|
|
|
The line terminator is defined as ``'\\n'``.
|
|
|
@@ -228,11 +230,11 @@ class TextFileIO:
|
|
|
:returns: Up to ``size`` characters.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def readlines(self) -> typing.List[str]:
|
|
|
- '''
|
|
|
+ """
|
|
|
Read and return a list of lines from the file.
|
|
|
The line terminator is defined as ``'\\n'``.
|
|
|
The new line character is included in the return value.
|
|
|
@@ -241,11 +243,11 @@ class TextFileIO:
|
|
|
:returns: A list of strings.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def write(self, data: str) -> int:
|
|
|
- '''
|
|
|
+ """
|
|
|
Write the given string to the file.
|
|
|
The number of written characters will be returned.
|
|
|
This can be less than the length of the provided data.
|
|
|
@@ -254,19 +256,19 @@ class TextFileIO:
|
|
|
:returns: The number of characters written.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def flush(self) -> None:
|
|
|
- '''
|
|
|
+ """
|
|
|
Write the contents of the file buffer to the file on the SD card.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def seek(self, offset: int, whence: int = SEEK_SET) -> int:
|
|
|
- '''
|
|
|
+ """
|
|
|
Set the pointer position by the given ``offset``, relative to the position indicated by ``whence``.
|
|
|
The new absolute position will be returned.
|
|
|
|
|
|
@@ -275,57 +277,58 @@ class TextFileIO:
|
|
|
:returns: The new absolute position.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def tell(self) -> int:
|
|
|
- '''
|
|
|
+ """
|
|
|
Get the current pointer position.
|
|
|
|
|
|
:returns: The absolute position of the pointer.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def close(self) -> None:
|
|
|
- '''
|
|
|
+ """
|
|
|
Close the file handle.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
- def __enter__(self) -> 'TextFileIO':
|
|
|
- '''
|
|
|
+ def __enter__(self) -> "TextFileIO":
|
|
|
+ """
|
|
|
This method is invoked, when the instance enters a runtime context.
|
|
|
|
|
|
:returns: The :class:`BinaryFileIO` instance.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def __exit__(self, *args, **kwargs) -> None:
|
|
|
- '''
|
|
|
+ """
|
|
|
This method is invoked, when the instance leavs a runtime context.
|
|
|
This basically calls :meth:`close` on the instance.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
def __del__(self) -> None:
|
|
|
- '''
|
|
|
+ """
|
|
|
This method is invoked, when the garbage collector removes the object.
|
|
|
This basically calls :meth:`close` on the instance.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
pass
|
|
|
|
|
|
+
|
|
|
def open(path: str, mode: str, *args, **kwargs) -> BinaryFileIO | TextFileIO:
|
|
|
- '''
|
|
|
+ """
|
|
|
Open a file on the file system with the specified mode.
|
|
|
The file path must always be absolute, beginning with ``/ext``.
|
|
|
The following modifiers are available:
|
|
|
@@ -343,7 +346,7 @@ def open(path: str, mode: str, *args, **kwargs) -> BinaryFileIO | TextFileIO:
|
|
|
* - ``'w'``
|
|
|
- Open for writing, truncating an existing file first.
|
|
|
* - ``'b'``
|
|
|
- - Open the file in binary mode.
|
|
|
+ - Open the file in binary mode.
|
|
|
The return value will be a :class:`BinaryFileIO` instance.
|
|
|
* - ``'t'``
|
|
|
- Open the in text mode.
|
|
|
@@ -353,7 +356,7 @@ def open(path: str, mode: str, *args, **kwargs) -> BinaryFileIO | TextFileIO:
|
|
|
- Open for reading and writing.
|
|
|
Will create the file, if it not exists.
|
|
|
The pointer will be placed at the end of the file.
|
|
|
-
|
|
|
+
|
|
|
The modifiers can be combined, e.g. ``'rb+'`` would open a file for reading and writing in binary mode.
|
|
|
|
|
|
:param path: The path to the file to open.
|
|
|
@@ -362,5 +365,5 @@ def open(path: str, mode: str, *args, **kwargs) -> BinaryFileIO | TextFileIO:
|
|
|
:param kwargs: Is ignored at the moment.
|
|
|
|
|
|
.. versionadded:: 1.5.0
|
|
|
- '''
|
|
|
+ """
|
|
|
return io._open(path, mode, *args, **kwargs)
|