hammad-python 0.0.10__py3-none-any.whl → 0.0.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. hammad/__init__.py +64 -10
  2. hammad/based/__init__.py +52 -0
  3. hammad/based/fields.py +546 -0
  4. hammad/based/model.py +968 -0
  5. hammad/based/utils.py +455 -0
  6. hammad/cache/__init__.py +30 -0
  7. hammad/{cache.py → cache/_cache.py} +83 -12
  8. hammad/cli/__init__.py +25 -0
  9. hammad/cli/plugins/__init__.py +786 -0
  10. hammad/cli/styles/__init__.py +5 -0
  11. hammad/cli/styles/animations.py +548 -0
  12. hammad/cli/styles/settings.py +135 -0
  13. hammad/cli/styles/types.py +358 -0
  14. hammad/cli/styles/utils.py +480 -0
  15. hammad/data/__init__.py +51 -0
  16. hammad/data/collections/__init__.py +32 -0
  17. hammad/data/collections/base_collection.py +58 -0
  18. hammad/data/collections/collection.py +227 -0
  19. hammad/data/collections/searchable_collection.py +556 -0
  20. hammad/data/collections/vector_collection.py +497 -0
  21. hammad/data/databases/__init__.py +21 -0
  22. hammad/data/databases/database.py +551 -0
  23. hammad/data/types/__init__.py +33 -0
  24. hammad/data/types/files/__init__.py +1 -0
  25. hammad/data/types/files/audio.py +81 -0
  26. hammad/data/types/files/configuration.py +475 -0
  27. hammad/data/types/files/document.py +195 -0
  28. hammad/data/types/files/file.py +358 -0
  29. hammad/data/types/files/image.py +80 -0
  30. hammad/json/__init__.py +21 -0
  31. hammad/{utils/json → json}/converters.py +4 -1
  32. hammad/logging/__init__.py +27 -0
  33. hammad/logging/decorators.py +432 -0
  34. hammad/logging/logger.py +534 -0
  35. hammad/pydantic/__init__.py +43 -0
  36. hammad/{utils/pydantic → pydantic}/converters.py +2 -1
  37. hammad/pydantic/models/__init__.py +28 -0
  38. hammad/pydantic/models/arbitrary_model.py +46 -0
  39. hammad/pydantic/models/cacheable_model.py +79 -0
  40. hammad/pydantic/models/fast_model.py +318 -0
  41. hammad/pydantic/models/function_model.py +176 -0
  42. hammad/pydantic/models/subscriptable_model.py +63 -0
  43. hammad/text/__init__.py +37 -0
  44. hammad/text/text.py +1068 -0
  45. hammad/text/utils/__init__.py +1 -0
  46. hammad/{utils/text → text/utils}/converters.py +2 -2
  47. hammad/text/utils/markdown/__init__.py +1 -0
  48. hammad/{utils → text/utils}/markdown/converters.py +3 -3
  49. hammad/{utils → text/utils}/markdown/formatting.py +1 -1
  50. hammad/{utils/typing/utils.py → typing/__init__.py} +75 -2
  51. hammad/web/__init__.py +42 -0
  52. hammad/web/http/__init__.py +1 -0
  53. hammad/web/http/client.py +944 -0
  54. hammad/web/openapi/client.py +740 -0
  55. hammad/web/search/__init__.py +1 -0
  56. hammad/web/search/client.py +936 -0
  57. hammad/web/utils.py +463 -0
  58. hammad/yaml/__init__.py +30 -0
  59. hammad/yaml/converters.py +19 -0
  60. {hammad_python-0.0.10.dist-info → hammad_python-0.0.11.dist-info}/METADATA +14 -8
  61. hammad_python-0.0.11.dist-info/RECORD +65 -0
  62. hammad/database.py +0 -447
  63. hammad/logger.py +0 -273
  64. hammad/types/color.py +0 -951
  65. hammad/utils/json/__init__.py +0 -0
  66. hammad/utils/markdown/__init__.py +0 -0
  67. hammad/utils/pydantic/__init__.py +0 -0
  68. hammad/utils/text/__init__.py +0 -0
  69. hammad/utils/typing/__init__.py +0 -0
  70. hammad_python-0.0.10.dist-info/RECORD +0 -22
  71. /hammad/{types/__init__.py → py.typed} +0 -0
  72. /hammad/{utils → web/openapi}/__init__.py +0 -0
  73. {hammad_python-0.0.10.dist-info → hammad_python-0.0.11.dist-info}/WHEEL +0 -0
  74. {hammad_python-0.0.10.dist-info → hammad_python-0.0.11.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,358 @@
1
+ """hammad.data.types.files.file"""
2
+
3
+ from pathlib import Path
4
+ import httpx
5
+ from typing import Any, Self
6
+ import mimetypes
7
+ from urllib.parse import urlparse
8
+
9
+ from ....based.model import BasedModel
10
+ from ....based.fields import basedfield
11
+
12
+ __all__ = ("File", "FileSource")
13
+
14
+
15
+ _FILE_SIGNATURES = {
16
+ b"\x89PNG": "image/png",
17
+ b"\xff\xd8\xff": "image/jpeg",
18
+ b"GIF87a": "image/gif",
19
+ b"GIF89a": "image/gif",
20
+ b"%PDF": "application/pdf",
21
+ b"PK": "application/zip",
22
+ }
23
+
24
+
25
+ _mime_cache: dict[str, str] = {}
26
+ """Cache for MIME types."""
27
+
28
+
29
+ class FileSource(BasedModel, kw_only=True, dict=True, frozen=True):
30
+ """Represents the source of a `File` object."""
31
+
32
+ is_file: bool = basedfield(default=False)
33
+ """Whether this data represents a file."""
34
+ is_dir: bool = basedfield(default=False)
35
+ """Whether this data represents a directory."""
36
+ is_url: bool = basedfield(default=False)
37
+ """Whether this data originates from a URL."""
38
+ path: Path | None = basedfield(default=None)
39
+ """The file path if this is file-based data."""
40
+ url: str | None = basedfield(default=None)
41
+ """The URL if this is URL-based data."""
42
+ size: int | None = basedfield(default=None)
43
+ """Size in bytes if available."""
44
+ encoding: str | None = basedfield(default=None)
45
+ """Text encoding if applicable."""
46
+
47
+
48
+ class File(BasedModel, kw_only=True, dict=True):
49
+ """Base object for all file-like structure types within
50
+ the `hammad` ecosystem."""
51
+
52
+ data: Any | None = basedfield(default=None)
53
+ """The actual data content (bytes, string, path object, etc.)"""
54
+ type: str | None = basedfield(default=None)
55
+ """The MIME type or identifier for the data."""
56
+
57
+ source: FileSource = basedfield(default_factory=FileSource)
58
+ """The source of the data. Contains metadata as well."""
59
+
60
+ # Private cached attributes
61
+ _name: str | None = basedfield(default=None)
62
+ _extension: str | None = basedfield(default=None)
63
+ _repr: str | None = basedfield(default=None)
64
+
65
+ @property
66
+ def name(self) -> str | None:
67
+ """Returns the name of this data object."""
68
+ if self._name is not None:
69
+ return self._name
70
+
71
+ if self.source.path:
72
+ self._name = self.source.path.name
73
+ elif self.source.url:
74
+ parsed = urlparse(self.source.url)
75
+ self._name = parsed.path.split("/")[-1] or parsed.netloc
76
+ else:
77
+ self._name = "" # Cache empty result
78
+
79
+ return self._name if self._name else None
80
+
81
+ @property
82
+ def extension(self) -> str | None:
83
+ """Returns the extension of this data object."""
84
+ if self._extension is not None:
85
+ return self._extension
86
+
87
+ if self.source.path:
88
+ self._extension = self.source.path.suffix
89
+ elif name := self.name:
90
+ if "." in name:
91
+ self._extension = f".{name.rsplit('.', 1)[-1]}"
92
+ else:
93
+ self._extension = "" # Cache empty result
94
+ else:
95
+ self._extension = "" # Cache empty result
96
+
97
+ return self._extension if self._extension else None
98
+
99
+ @property
100
+ def exists(self) -> bool:
101
+ """Returns whether this data object exists."""
102
+ if self.data is not None:
103
+ return True
104
+ if self.source.path and (self.source.is_file or self.source.is_dir):
105
+ return self.source.path.exists()
106
+ return False
107
+
108
+ def read(self) -> bytes | str:
109
+ """Reads the data content.
110
+
111
+ Returns:
112
+ The data content as bytes or string depending on the source.
113
+
114
+ Raises:
115
+ ValueError: If the data cannot be read.
116
+ """
117
+ if self.data is not None:
118
+ return self.data
119
+
120
+ if self.source.path and self.source.is_file and self.source.path.exists():
121
+ if self.source.encoding:
122
+ return self.source.path.read_text(encoding=self.source.encoding)
123
+ return self.source.path.read_bytes()
124
+
125
+ raise ValueError(f"Cannot read data from {self.name or 'unknown source'}")
126
+
127
+ def to_file(self, path: str | Path, *, overwrite: bool = False) -> Path:
128
+ """Save the data to a file.
129
+
130
+ Args:
131
+ path: The path to save to.
132
+ overwrite: If True, overwrite existing files.
133
+
134
+ Returns:
135
+ The path where the file was saved.
136
+
137
+ Raises:
138
+ FileExistsError: If file exists and overwrite is False.
139
+ ValueError: If data cannot be saved.
140
+ """
141
+ save_path = Path(path)
142
+
143
+ if save_path.exists() and not overwrite:
144
+ raise FileExistsError(f"File already exists: {save_path}")
145
+
146
+ # Ensure parent directory exists
147
+ save_path.parent.mkdir(parents=True, exist_ok=True)
148
+
149
+ data = self.read()
150
+ if isinstance(data, str):
151
+ save_path.write_text(data, encoding=self.source.encoding or "utf-8")
152
+ else:
153
+ save_path.write_bytes(data)
154
+
155
+ return save_path
156
+
157
+ def __repr__(self) -> str:
158
+ """Returns a string representation of the data object."""
159
+ if self._repr is not None:
160
+ return self._repr
161
+
162
+ parts = []
163
+
164
+ if self.source.path:
165
+ parts.append(f"path={self.source.path!r}")
166
+ elif self.source.url:
167
+ parts.append(f"url={self.source.url!r}")
168
+ elif self.data is not None:
169
+ parts.append(f"data={self.data!r}")
170
+
171
+ if self.source.is_file:
172
+ parts.append("is_file=True")
173
+ elif self.source.is_dir:
174
+ parts.append("is_dir=True")
175
+ elif self.source.is_url:
176
+ parts.append("is_url=True")
177
+
178
+ if (size := self.source.size) is not None:
179
+ if size < 1024:
180
+ size_str = f"{size}B"
181
+ elif size < 1048576: # 1024 * 1024
182
+ size_str = f"{size / 1024:.1f}KB"
183
+ elif size < 1073741824: # 1024 * 1024 * 1024
184
+ size_str = f"{size / 1048576:.1f}MB"
185
+ else:
186
+ size_str = f"{size / 1073741824:.1f}GB"
187
+ parts.append(f"size={size_str}")
188
+
189
+ if self.source.encoding:
190
+ parts.append(f"encoding={self.source.encoding!r}")
191
+
192
+ self._repr = f"<{', '.join(parts)}>"
193
+ return self._repr
194
+
195
+ def __eq__(self, other: Any) -> bool:
196
+ """Returns whether this data object is equal to another."""
197
+ return isinstance(other, File) and self.data == other.data
198
+
199
+ @classmethod
200
+ def from_path(
201
+ cls,
202
+ path: str | Path,
203
+ *,
204
+ encoding: str | None = None,
205
+ lazy: bool = True,
206
+ ) -> Self:
207
+ """Creates a data object from a filepath and
208
+ assigns the appropriate type and flags.
209
+
210
+ Args:
211
+ path: The file or directory path.
212
+ encoding: Text encoding for reading text files.
213
+ lazy: If True, defer loading content until needed.
214
+
215
+ Returns:
216
+ A new Data instance representing the file or directory.
217
+ """
218
+ path = Path(path)
219
+
220
+ # Use cached stat call
221
+ try:
222
+ stat = path.stat()
223
+ is_file = stat.st_mode & 0o170000 == 0o100000 # S_IFREG
224
+ is_dir = stat.st_mode & 0o170000 == 0o040000 # S_IFDIR
225
+ size = stat.st_size if is_file else None
226
+ except OSError:
227
+ is_file = is_dir = False
228
+ size = None
229
+
230
+ # Get MIME type for files using cache
231
+ mime_type = None
232
+ if is_file:
233
+ path_str = str(path)
234
+ if path_str in _mime_cache:
235
+ mime_type = _mime_cache[path_str]
236
+ else:
237
+ mime_type, _ = mimetypes.guess_type(path_str)
238
+ _mime_cache[path_str] = mime_type
239
+
240
+ # Load data if not lazy and it's a file
241
+ data = None
242
+ if not lazy and is_file and size is not None:
243
+ if encoding or (mime_type and mime_type.startswith("text/")):
244
+ data = path.read_text(encoding=encoding or "utf-8")
245
+ else:
246
+ data = path.read_bytes()
247
+
248
+ return cls(
249
+ data=data,
250
+ type=mime_type,
251
+ source=FileSource(
252
+ is_file=is_file,
253
+ is_dir=is_dir,
254
+ is_url=False,
255
+ path=path,
256
+ size=size,
257
+ encoding=encoding,
258
+ ),
259
+ )
260
+
261
+ @classmethod
262
+ def from_url(
263
+ cls,
264
+ url: str,
265
+ *,
266
+ type: str | None = None,
267
+ lazy: bool = True,
268
+ ) -> Self:
269
+ """Creates a data object from either a downloadable
270
+ URL (treated as a file), or a web page itself treated as a
271
+ document.
272
+
273
+ Args:
274
+ url: The URL to create data from.
275
+ type: Optional MIME type override.
276
+ lazy: If True, defer loading content until needed.
277
+
278
+ Returns:
279
+ A new Data instance representing the URL.
280
+ """
281
+ data = None
282
+ size = None
283
+ encoding = None
284
+
285
+ # Load data if not lazy
286
+ if not lazy:
287
+ try:
288
+ with httpx.Client() as client:
289
+ response = client.get(url)
290
+ response.raise_for_status()
291
+
292
+ data = response.content
293
+ size = len(data)
294
+
295
+ # Get content type from response headers if not provided
296
+ if not type:
297
+ content_type = response.headers.get("content-type", "")
298
+ type = content_type.split(";")[0] if content_type else None
299
+
300
+ # Get encoding from response if it's text content
301
+ if response.headers.get("content-type", "").startswith("text/"):
302
+ encoding = response.encoding
303
+ data = response.text
304
+
305
+ except Exception:
306
+ # If download fails, still create the object but without data
307
+ pass
308
+
309
+ return cls(
310
+ data=data,
311
+ type=type,
312
+ source=FileSource(
313
+ is_url=True,
314
+ is_file=False,
315
+ is_dir=False,
316
+ url=url,
317
+ size=size,
318
+ encoding=encoding,
319
+ ),
320
+ )
321
+
322
+ @classmethod
323
+ def from_bytes(
324
+ cls,
325
+ data: bytes,
326
+ *,
327
+ type: str | None = None,
328
+ name: str | None = None,
329
+ ) -> Self:
330
+ """Creates a data object from a bytes object.
331
+
332
+ Args:
333
+ data: The bytes data.
334
+ type: Optional MIME type.
335
+ name: Optional name for the data.
336
+
337
+ Returns:
338
+ A new Data instance containing the bytes data.
339
+ """
340
+ # Try to detect type from content if not provided
341
+ if not type and data:
342
+ # Check against pre-compiled signatures
343
+ for sig, mime in _FILE_SIGNATURES.items():
344
+ if data.startswith(sig):
345
+ type = mime
346
+ break
347
+
348
+ return cls(
349
+ data=data,
350
+ type=type,
351
+ source=FileSource(
352
+ is_file=True,
353
+ is_dir=False,
354
+ is_url=False,
355
+ size=len(data),
356
+ path=Path(name) if name else None,
357
+ ),
358
+ )
@@ -0,0 +1,80 @@
1
+ """hammad.data.types.files.image"""
2
+
3
+ import httpx
4
+ from typing import Self
5
+
6
+ from .file import File, FileSource
7
+ from ....based.fields import basedfield
8
+
9
+ __all__ = ("Image",)
10
+
11
+
12
+ class Image(File):
13
+ """A representation of an image, that is loadable from both a URL, file path
14
+ or bytes."""
15
+
16
+ # Image-specific metadata
17
+ _width: int | None = basedfield(default=None)
18
+ _height: int | None = basedfield(default=None)
19
+ _format: str | None = basedfield(default=None)
20
+
21
+ @property
22
+ def is_valid_image(self) -> bool:
23
+ """Check if this is a valid image based on MIME type."""
24
+ return self.type is not None and self.type.startswith("image/")
25
+
26
+ @property
27
+ def format(self) -> str | None:
28
+ """Get the image format from MIME type."""
29
+ if self._format is None and self.type:
30
+ # Extract format from MIME type (e.g., 'image/png' -> 'png')
31
+ self._format = self.type.split("/")[-1].upper()
32
+ return self._format
33
+
34
+ @classmethod
35
+ def from_url(
36
+ cls,
37
+ url: str,
38
+ *,
39
+ lazy: bool = True,
40
+ timeout: float = 30.0,
41
+ ) -> Self:
42
+ """Download and create an image from a URL.
43
+
44
+ Args:
45
+ url: The URL to download from.
46
+ lazy: If True, defer loading content until needed.
47
+ timeout: Request timeout in seconds.
48
+
49
+ Returns:
50
+ A new Image instance.
51
+ """
52
+ data = None
53
+ size = None
54
+ type = None
55
+
56
+ if not lazy:
57
+ with httpx.Client(timeout=timeout) as client:
58
+ response = client.get(url)
59
+ response.raise_for_status()
60
+
61
+ data = response.content
62
+ size = len(data)
63
+
64
+ # Get content type
65
+ content_type = response.headers.get("content-type", "")
66
+ type = content_type.split(";")[0] if content_type else None
67
+
68
+ # Validate it's an image
69
+ if type and not type.startswith("image/"):
70
+ raise ValueError(f"URL does not point to an image: {type}")
71
+
72
+ return cls(
73
+ data=data,
74
+ type=type,
75
+ source=FileSource(
76
+ is_url=True,
77
+ url=url,
78
+ size=size,
79
+ ),
80
+ )
@@ -0,0 +1,21 @@
1
+ """hammad.utils.json"""
2
+
3
+ from typing import TYPE_CHECKING
4
+ from ..based.utils import auto_create_lazy_loader
5
+
6
+ if TYPE_CHECKING:
7
+ from .converters import (
8
+ convert_to_json_schema,
9
+ encode_json,
10
+ decode_json,
11
+ )
12
+
13
+ __all__ = ("convert_to_json_schema", "encode_json", "decode_json")
14
+
15
+
16
+ __getattr__ = auto_create_lazy_loader(__all__)
17
+
18
+
19
+ def __dir__() -> list[str]:
20
+ """Get the attributes of the json module."""
21
+ return list(__all__)
@@ -5,12 +5,15 @@ Contains various utility functions used when working with JSON data."""
5
5
  import dataclasses
6
6
  from typing import Any
7
7
  import msgspec
8
+ from msgspec.json import encode as encode_json, decode as decode_json
8
9
 
9
- from ..typing.utils import get_type_description, inspection
10
+ from ..typing import get_type_description, inspection
10
11
 
11
12
  __all__ = (
12
13
  "SchemaError",
13
14
  "convert_to_json_schema",
15
+ "encode",
16
+ "decode",
14
17
  )
15
18
 
16
19
 
@@ -0,0 +1,27 @@
1
+ """hammad.logging"""
2
+
3
+ from typing import TYPE_CHECKING
4
+ from ..based.utils import auto_create_lazy_loader
5
+
6
+ if TYPE_CHECKING:
7
+ from .logger import Logger, create_logger, create_logger_level
8
+ from .decorators import trace_function, trace_cls, trace
9
+
10
+
11
+ __all__ = (
12
+ "Logger",
13
+ "LoggerLevel",
14
+ "create_logger",
15
+ "create_logger_level",
16
+ "trace_function",
17
+ "trace_cls",
18
+ "trace",
19
+ )
20
+
21
+
22
+ __getattr__ = auto_create_lazy_loader(__all__)
23
+
24
+
25
+ def __dir__() -> list[str]:
26
+ """Get the attributes of the logging module."""
27
+ return list(__all__)