MCPcopy Index your code
hub / github.com/python-telegram-bot/python-telegram-bot / parse_file_input

Function parse_file_input

src/telegram/_utils/files.py:98–160  ·  view source on GitHub ↗

Parses input for sending files: * For string input, if the input is an absolute path of a local file: * if ``local_mode`` is ``True``, adds the ``file://`` prefix. If the input is a relative path of a local file, computes the absolute path and adds the ``file://`` prefix.

(  # pylint: disable=too-many-return-statements
    file_input: "FileInput | TelegramObject",
    tg_type: type["TelegramObject"] | None = None,
    filename: str | None = None,
    attach: bool = False,
    local_mode: bool = False,
)

Source from the content-addressed store, hash-verified

96
97
98def parse_file_input( # pylint: disable=too-many-return-statements
99 file_input: "FileInput | TelegramObject",
100 tg_type: type["TelegramObject"] | None = None,
101 filename: str | None = None,
102 attach: bool = False,
103 local_mode: bool = False,
104) -> "str | InputFile | Any":
105 """
106 Parses input for sending files:
107
108 * For string input, if the input is an absolute path of a local file:
109
110 * if ``local_mode`` is ``True``, adds the ``file://`` prefix. If the input is a relative
111 path of a local file, computes the absolute path and adds the ``file://`` prefix.
112 * if ``local_mode`` is ``False``, loads the file as binary data and builds an
113 :class:`InputFile` from that
114
115 Returns the input unchanged, otherwise.
116 * :class:`pathlib.Path` objects are treated the same way as strings.
117 * For IO and bytes input, returns an :class:`telegram.InputFile`.
118 * If :attr:`tg_type` is specified and the input is of that type, returns the ``file_id``
119 attribute.
120
121 Args:
122 file_input (:obj:`str` | :obj:`bytes` | :term:`file object` | :class:`~telegram.InputFile`\
123 | Telegram media object): The input to parse.
124 tg_type (:obj:`type`, optional): The Telegram media type the input can be. E.g.
125 :class:`telegram.Animation`.
126 filename (:obj:`str`, optional): The filename. Only relevant in case an
127 :class:`telegram.InputFile` is returned.
128 attach (:obj:`bool`, optional): Pass :obj:`True` if the parameter this file belongs to in
129 the request to Telegram should point to the multipart data via an ``attach://`` URI.
130 Defaults to `False`. Only relevant if an :class:`telegram.InputFile` is returned.
131 local_mode (:obj:`bool`, optional): Pass :obj:`True` if the bot is running an api server
132 in ``--local`` mode.
133
134 Returns:
135 :obj:`str` | :class:`telegram.InputFile` | :obj:`object`: The parsed input or the untouched
136 :attr:`file_input`, in case it's no valid file input.
137 """
138 # Importing on file-level yields cyclic Import Errors
139 from telegram import InputFile # pylint: disable=import-outside-toplevel # noqa: PLC0415
140
141 if isinstance(file_input, str) and file_input.startswith("file://"):
142 if not local_mode:
143 raise ValueError("Specified file input is a file URI, but local mode is not enabled.")
144 return file_input
145 if isinstance(file_input, str | Path):
146 if is_local_file(file_input):
147 path = Path(file_input)
148 if local_mode:
149 return path.absolute().as_uri()
150 with path.open(mode="rb") as file_handle:
151 return InputFile(file_handle, filename=filename, attach=attach)
152
153 return file_input
154 if isinstance(file_input, bytes):
155 return InputFile(file_input, filename=filename, attach=attach)

Callers 15

_parse_file_inputMethod · 0.90
_parse_file_inputMethod · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls 2

InputFileClass · 0.90
is_local_fileFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…