chore: import upstream snapshot with attribution
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
Docker image release / Build base image (push) Has been cancelled
Sync docs with Docusaurus / sync (push) Has been cancelled
Tests / Check if changed (push) Has been cancelled
Tests / format (push) Has been cancelled
Tests / check-imports (push) Has been cancelled
Tests / Unit / macos-latest (push) Has been cancelled
Tests / Unit / ubuntu-latest (push) Has been cancelled
Tests / Unit / windows-latest (push) Has been cancelled
Tests / mypy (push) Has been cancelled
Tests / Integration / ubuntu-latest (push) Has been cancelled
Tests / Integration / macos-latest (push) Has been cancelled
Tests / Integration / windows-latest (push) Has been cancelled
Tests / notify-slack-on-failure (push) Has been cancelled
Tests / Mark tests as completed (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from haystack.dataclasses import ByteStream
|
||||
|
||||
|
||||
def get_bytestream_from_source(source: str | Path | ByteStream, guess_mime_type: bool = False) -> ByteStream:
|
||||
"""
|
||||
Creates a ByteStream object from a source.
|
||||
|
||||
:param source:
|
||||
A source to convert to a ByteStream. Can be a string (path to a file), a Path object, or a ByteStream.
|
||||
:param guess_mime_type:
|
||||
Whether to guess the mime type from the file.
|
||||
:return:
|
||||
A ByteStream object.
|
||||
"""
|
||||
|
||||
if isinstance(source, ByteStream):
|
||||
return source
|
||||
if isinstance(source, (str, Path)):
|
||||
bs = ByteStream.from_file_path(Path(source), guess_mime_type=guess_mime_type)
|
||||
bs.meta["file_path"] = str(source)
|
||||
return bs
|
||||
raise ValueError(f"Unsupported source type {type(source)}")
|
||||
|
||||
|
||||
def normalize_metadata(meta: dict[str, Any] | list[dict[str, Any]] | None, sources_count: int) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Normalize the metadata input for a converter.
|
||||
|
||||
Given all the possible value of the meta input for a converter (None, dictionary or list of dicts),
|
||||
makes sure to return a list of dictionaries of the correct length for the converter to use.
|
||||
|
||||
:param meta: the meta input of the converter, as-is
|
||||
:param sources_count: the number of sources the converter received
|
||||
:returns: a list of dictionaries of the make length as the sources list
|
||||
"""
|
||||
if meta is None:
|
||||
return [{}] * sources_count
|
||||
if isinstance(meta, dict):
|
||||
return [meta] * sources_count
|
||||
if isinstance(meta, list):
|
||||
if sources_count != len(meta):
|
||||
raise ValueError("The length of the metadata list must match the number of sources.")
|
||||
return meta
|
||||
raise ValueError("meta must be either None, a dictionary or a list of dictionaries.")
|
||||
Reference in New Issue
Block a user