Spaces:
Running
Running
Jeremiah Lowin commited on
Add path expansion to image/audio/file (#1038)
Browse files* Add path expansion to image/audio/file
* Make path tests platform agnostic
src/fastmcp/utilities/types.py
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
import base64
|
| 4 |
import inspect
|
| 5 |
import mimetypes
|
|
|
|
| 6 |
from collections.abc import Callable
|
| 7 |
from functools import lru_cache
|
| 8 |
from pathlib import Path
|
|
@@ -101,7 +102,7 @@ class Image:
|
|
| 101 |
if path is not None and data is not None:
|
| 102 |
raise ValueError("Only one of path or data can be provided")
|
| 103 |
|
| 104 |
-
self.path = Path(path) if path else None
|
| 105 |
self.data = data
|
| 106 |
self._format = format
|
| 107 |
self._mime_type = self._get_mime_type()
|
|
@@ -160,7 +161,7 @@ class Audio:
|
|
| 160 |
if path is not None and data is not None:
|
| 161 |
raise ValueError("Only one of path or data can be provided")
|
| 162 |
|
| 163 |
-
self.path = Path(path) if path else None
|
| 164 |
self.data = data
|
| 165 |
self._format = format
|
| 166 |
self._mime_type = self._get_mime_type()
|
|
@@ -219,7 +220,7 @@ class File:
|
|
| 219 |
if path is not None and data is not None:
|
| 220 |
raise ValueError("Only one of path or data can be provided")
|
| 221 |
|
| 222 |
-
self.path = Path(path) if path else None
|
| 223 |
self.data = data
|
| 224 |
self._format = format
|
| 225 |
self._mime_type = self._get_mime_type()
|
|
|
|
| 3 |
import base64
|
| 4 |
import inspect
|
| 5 |
import mimetypes
|
| 6 |
+
import os
|
| 7 |
from collections.abc import Callable
|
| 8 |
from functools import lru_cache
|
| 9 |
from pathlib import Path
|
|
|
|
| 102 |
if path is not None and data is not None:
|
| 103 |
raise ValueError("Only one of path or data can be provided")
|
| 104 |
|
| 105 |
+
self.path = Path(os.path.expandvars(str(path))).expanduser() if path else None
|
| 106 |
self.data = data
|
| 107 |
self._format = format
|
| 108 |
self._mime_type = self._get_mime_type()
|
|
|
|
| 161 |
if path is not None and data is not None:
|
| 162 |
raise ValueError("Only one of path or data can be provided")
|
| 163 |
|
| 164 |
+
self.path = Path(os.path.expandvars(str(path))).expanduser() if path else None
|
| 165 |
self.data = data
|
| 166 |
self._format = format
|
| 167 |
self._mime_type = self._get_mime_type()
|
|
|
|
| 220 |
if path is not None and data is not None:
|
| 221 |
raise ValueError("Only one of path or data can be provided")
|
| 222 |
|
| 223 |
+
self.path = Path(os.path.expandvars(str(path))).expanduser() if path else None
|
| 224 |
self.data = data
|
| 225 |
self._format = format
|
| 226 |
self._mime_type = self._get_mime_type()
|
tests/utilities/test_types.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
import base64
|
|
|
|
|
|
|
|
|
|
| 2 |
from types import EllipsisType
|
| 3 |
from typing import Annotated, Any
|
| 4 |
|
|
@@ -131,6 +134,23 @@ class TestImage:
|
|
| 131 |
assert image.data is None
|
| 132 |
assert image._mime_type == "image/png"
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
def test_image_initialization_with_data(self):
|
| 135 |
"""Test image initialization with data."""
|
| 136 |
image = Image(data=b"test")
|
|
@@ -215,6 +235,23 @@ class TestAudio:
|
|
| 215 |
assert audio.data is None
|
| 216 |
assert audio._mime_type == "audio/wav"
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
def test_audio_initialization_with_data(self):
|
| 219 |
"""Test audio initialization with data."""
|
| 220 |
audio = Audio(data=b"test")
|
|
@@ -312,6 +349,23 @@ class TestFile:
|
|
| 312 |
assert file.data is None
|
| 313 |
assert file._mime_type == "text/plain"
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
def test_file_initialization_with_data(self):
|
| 316 |
"""Test initialization with data and format."""
|
| 317 |
test_data = b"test data"
|
|
|
|
| 1 |
import base64
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
from pathlib import Path
|
| 5 |
from types import EllipsisType
|
| 6 |
from typing import Annotated, Any
|
| 7 |
|
|
|
|
| 134 |
assert image.data is None
|
| 135 |
assert image._mime_type == "image/png"
|
| 136 |
|
| 137 |
+
def test_image_path_expansion_with_tilde(self):
|
| 138 |
+
"""Test that ~ is expanded to the user's home directory."""
|
| 139 |
+
image = Image(path="~/test.png")
|
| 140 |
+
assert image.path is not None
|
| 141 |
+
assert not str(image.path).startswith("~")
|
| 142 |
+
assert str(image.path).startswith(os.path.expanduser("~"))
|
| 143 |
+
|
| 144 |
+
def test_image_path_expansion_with_env_var(self, monkeypatch):
|
| 145 |
+
"""Test that environment variables are expanded."""
|
| 146 |
+
test_dir = tempfile.mkdtemp()
|
| 147 |
+
monkeypatch.setenv("TEST_PATH", test_dir)
|
| 148 |
+
image = Image(path="$TEST_PATH/test.png")
|
| 149 |
+
assert image.path is not None
|
| 150 |
+
assert not str(image.path).startswith("$TEST_PATH")
|
| 151 |
+
expected_path = Path(test_dir) / "test.png"
|
| 152 |
+
assert image.path == expected_path
|
| 153 |
+
|
| 154 |
def test_image_initialization_with_data(self):
|
| 155 |
"""Test image initialization with data."""
|
| 156 |
image = Image(data=b"test")
|
|
|
|
| 235 |
assert audio.data is None
|
| 236 |
assert audio._mime_type == "audio/wav"
|
| 237 |
|
| 238 |
+
def test_audio_path_expansion_with_tilde(self):
|
| 239 |
+
"""Test that ~ is expanded to the user's home directory."""
|
| 240 |
+
audio = Audio(path="~/test.wav")
|
| 241 |
+
assert audio.path is not None
|
| 242 |
+
assert not str(audio.path).startswith("~")
|
| 243 |
+
assert str(audio.path).startswith(os.path.expanduser("~"))
|
| 244 |
+
|
| 245 |
+
def test_audio_path_expansion_with_env_var(self, monkeypatch):
|
| 246 |
+
"""Test that environment variables are expanded."""
|
| 247 |
+
test_dir = tempfile.mkdtemp()
|
| 248 |
+
monkeypatch.setenv("TEST_AUDIO_PATH", test_dir)
|
| 249 |
+
audio = Audio(path="$TEST_AUDIO_PATH/test.wav")
|
| 250 |
+
assert audio.path is not None
|
| 251 |
+
assert not str(audio.path).startswith("$TEST_AUDIO_PATH")
|
| 252 |
+
expected_path = Path(test_dir) / "test.wav"
|
| 253 |
+
assert audio.path == expected_path
|
| 254 |
+
|
| 255 |
def test_audio_initialization_with_data(self):
|
| 256 |
"""Test audio initialization with data."""
|
| 257 |
audio = Audio(data=b"test")
|
|
|
|
| 349 |
assert file.data is None
|
| 350 |
assert file._mime_type == "text/plain"
|
| 351 |
|
| 352 |
+
def test_file_path_expansion_with_tilde(self):
|
| 353 |
+
"""Test that ~ is expanded to the user's home directory."""
|
| 354 |
+
file = File(path="~/test.txt")
|
| 355 |
+
assert file.path is not None
|
| 356 |
+
assert not str(file.path).startswith("~")
|
| 357 |
+
assert str(file.path).startswith(os.path.expanduser("~"))
|
| 358 |
+
|
| 359 |
+
def test_file_path_expansion_with_env_var(self, monkeypatch):
|
| 360 |
+
"""Test that environment variables are expanded."""
|
| 361 |
+
test_dir = tempfile.mkdtemp()
|
| 362 |
+
monkeypatch.setenv("TEST_FILE_PATH", test_dir)
|
| 363 |
+
file = File(path="$TEST_FILE_PATH/test.txt")
|
| 364 |
+
assert file.path is not None
|
| 365 |
+
assert not str(file.path).startswith("$TEST_FILE_PATH")
|
| 366 |
+
expected_path = Path(test_dir) / "test.txt"
|
| 367 |
+
assert file.path == expected_path
|
| 368 |
+
|
| 369 |
def test_file_initialization_with_data(self):
|
| 370 |
"""Test initialization with data and format."""
|
| 371 |
test_data = b"test data"
|