r"""Creates a TxtFile object from a BytesIO object. Args: file (BytesIO): A BytesIO object representing the contents of the txt file. filename (str): The name of the file. Returns: TxtFile: A TxtFile object.
(cls, file: BytesIO, filename: str)
| 233 | class TxtFile(File): |
| 234 | @classmethod |
| 235 | def from_bytes(cls, file: BytesIO, filename: str) -> "TxtFile": |
| 236 | r"""Creates a TxtFile object from a BytesIO object. |
| 237 | |
| 238 | Args: |
| 239 | file (BytesIO): A BytesIO object representing the contents of the |
| 240 | txt file. |
| 241 | filename (str): The name of the file. |
| 242 | |
| 243 | Returns: |
| 244 | TxtFile: A TxtFile object. |
| 245 | """ |
| 246 | # Read the text from the file |
| 247 | text = file.read().decode("utf-8") |
| 248 | text = strip_consecutive_newlines(text) |
| 249 | # Create a dictionary with the extracted text |
| 250 | doc = {"page_content": text.strip()} |
| 251 | # Calculate a unique identifier for the file |
| 252 | file_id = md5(file.getvalue()).hexdigest() |
| 253 | # Reset the file pointer to the beginning |
| 254 | file.seek(0) |
| 255 | return cls( |
| 256 | name=filename, |
| 257 | file_id=file_id, |
| 258 | docs=[doc], |
| 259 | raw_bytes=file.getvalue(), |
| 260 | ) |
| 261 | |
| 262 | |
| 263 | class JsonFile(File): |
nothing calls this directly
no test coverage detected