hammad-python 0.0.10__py3-none-any.whl → 0.0.12__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 (96) hide show
  1. hammad/__init__.py +177 -10
  2. hammad/_core/__init__.py +1 -0
  3. hammad/_core/_utils/__init__.py +4 -0
  4. hammad/_core/_utils/_import_utils.py +182 -0
  5. hammad/ai/__init__.py +59 -0
  6. hammad/ai/_utils.py +142 -0
  7. hammad/ai/completions/__init__.py +44 -0
  8. hammad/ai/completions/client.py +729 -0
  9. hammad/ai/completions/create.py +686 -0
  10. hammad/ai/completions/types.py +711 -0
  11. hammad/ai/completions/utils.py +374 -0
  12. hammad/ai/embeddings/__init__.py +35 -0
  13. hammad/ai/embeddings/client/__init__.py +1 -0
  14. hammad/ai/embeddings/client/base_embeddings_client.py +26 -0
  15. hammad/ai/embeddings/client/fastembed_text_embeddings_client.py +200 -0
  16. hammad/ai/embeddings/client/litellm_embeddings_client.py +288 -0
  17. hammad/ai/embeddings/create.py +159 -0
  18. hammad/ai/embeddings/types.py +69 -0
  19. hammad/base/__init__.py +35 -0
  20. hammad/base/fields.py +546 -0
  21. hammad/base/model.py +1078 -0
  22. hammad/base/utils.py +280 -0
  23. hammad/cache/__init__.py +48 -0
  24. hammad/cache/base_cache.py +181 -0
  25. hammad/cache/cache.py +169 -0
  26. hammad/cache/decorators.py +261 -0
  27. hammad/cache/file_cache.py +80 -0
  28. hammad/cache/ttl_cache.py +74 -0
  29. hammad/cli/__init__.py +33 -0
  30. hammad/cli/animations.py +604 -0
  31. hammad/cli/plugins.py +781 -0
  32. hammad/cli/styles/__init__.py +55 -0
  33. hammad/cli/styles/settings.py +139 -0
  34. hammad/cli/styles/types.py +358 -0
  35. hammad/cli/styles/utils.py +480 -0
  36. hammad/configuration/__init__.py +35 -0
  37. hammad/configuration/configuration.py +564 -0
  38. hammad/data/__init__.py +39 -0
  39. hammad/data/collections/__init__.py +34 -0
  40. hammad/data/collections/base_collection.py +58 -0
  41. hammad/data/collections/collection.py +452 -0
  42. hammad/data/collections/searchable_collection.py +556 -0
  43. hammad/data/collections/vector_collection.py +603 -0
  44. hammad/data/databases/__init__.py +21 -0
  45. hammad/data/databases/database.py +902 -0
  46. hammad/json/__init__.py +21 -0
  47. hammad/{utils/json → json}/converters.py +4 -1
  48. hammad/logging/__init__.py +35 -0
  49. hammad/logging/decorators.py +834 -0
  50. hammad/logging/logger.py +954 -0
  51. hammad/multimodal/__init__.py +24 -0
  52. hammad/multimodal/audio.py +96 -0
  53. hammad/multimodal/image.py +80 -0
  54. hammad/multithreading/__init__.py +304 -0
  55. hammad/pydantic/__init__.py +43 -0
  56. hammad/{utils/pydantic → pydantic}/converters.py +2 -1
  57. hammad/pydantic/models/__init__.py +28 -0
  58. hammad/pydantic/models/arbitrary_model.py +46 -0
  59. hammad/pydantic/models/cacheable_model.py +79 -0
  60. hammad/pydantic/models/fast_model.py +318 -0
  61. hammad/pydantic/models/function_model.py +176 -0
  62. hammad/pydantic/models/subscriptable_model.py +63 -0
  63. hammad/text/__init__.py +82 -0
  64. hammad/text/converters.py +723 -0
  65. hammad/{utils/markdown/formatting.py → text/markdown.py} +25 -23
  66. hammad/text/text.py +1066 -0
  67. hammad/types/__init__.py +11 -0
  68. hammad/types/file.py +358 -0
  69. hammad/{utils/typing/utils.py → typing/__init__.py} +142 -15
  70. hammad/web/__init__.py +43 -0
  71. hammad/web/http/__init__.py +1 -0
  72. hammad/web/http/client.py +944 -0
  73. hammad/web/models.py +245 -0
  74. hammad/web/openapi/client.py +740 -0
  75. hammad/web/search/__init__.py +1 -0
  76. hammad/web/search/client.py +988 -0
  77. hammad/web/utils.py +472 -0
  78. hammad/yaml/__init__.py +30 -0
  79. hammad/yaml/converters.py +19 -0
  80. {hammad_python-0.0.10.dist-info → hammad_python-0.0.12.dist-info}/METADATA +16 -7
  81. hammad_python-0.0.12.dist-info/RECORD +85 -0
  82. hammad/cache.py +0 -675
  83. hammad/database.py +0 -447
  84. hammad/logger.py +0 -273
  85. hammad/types/color.py +0 -951
  86. hammad/utils/markdown/__init__.py +0 -0
  87. hammad/utils/markdown/converters.py +0 -506
  88. hammad/utils/pydantic/__init__.py +0 -0
  89. hammad/utils/text/__init__.py +0 -0
  90. hammad/utils/text/converters.py +0 -229
  91. hammad/utils/typing/__init__.py +0 -0
  92. hammad_python-0.0.10.dist-info/RECORD +0 -22
  93. /hammad/{utils/__init__.py → py.typed} +0 -0
  94. /hammad/{utils/json → web/openapi}/__init__.py +0 -0
  95. {hammad_python-0.0.10.dist-info → hammad_python-0.0.12.dist-info}/WHEEL +0 -0
  96. {hammad_python-0.0.10.dist-info → hammad_python-0.0.12.dist-info}/licenses/LICENSE +0 -0
hammad/types/color.py DELETED
@@ -1,951 +0,0 @@
1
- """hammad.types.color"""
2
-
3
- from dataclasses import dataclass
4
- from typing import ClassVar
5
- from rich.color import Color as _RichColorClass
6
- from rich.text import Text
7
- from rich.style import Style
8
- from typing import Any, Dict, Optional, Literal, Tuple, TypeAlias, Union, Self
9
-
10
- __all__ = (
11
- "Color",
12
- "ColorName",
13
- "HexColor",
14
- "RGBColor",
15
- )
16
-
17
- # ------------------------------------------------------------
18
- # Generic Color Types
19
- # ------------------------------------------------------------
20
-
21
- HexColor: TypeAlias = str
22
- """Hexadecimal color string."""
23
-
24
- RGBColor: TypeAlias = Tuple[int, int, int]
25
- """RGB Color Tuple Parameter & Type."""
26
-
27
- # ------------------------------------------------------------
28
- # Color Names
29
- # ------------------------------------------------------------
30
-
31
- _COLORS_BY_NAME: Dict[str, RGBColor] = {
32
- "aliceblue": (240, 248, 255),
33
- "antiquewhite": (250, 235, 215),
34
- "aqua": (0, 255, 255),
35
- "aquamarine": (127, 255, 212),
36
- "azure": (240, 255, 255),
37
- "beige": (245, 245, 220),
38
- "bisque": (255, 228, 196),
39
- "black": (0, 0, 0),
40
- "blanchedalmond": (255, 235, 205),
41
- "blue": (0, 0, 255),
42
- "blueviolet": (138, 43, 226),
43
- "brown": (165, 42, 42),
44
- "burlywood": (222, 184, 135),
45
- "cadetblue": (95, 158, 160),
46
- "chartreuse": (127, 255, 0),
47
- "chocolate": (210, 105, 30),
48
- "coral": (255, 127, 80),
49
- "cornflowerblue": (100, 149, 237),
50
- "cornsilk": (255, 248, 220),
51
- "crimson": (220, 20, 60),
52
- "cyan": (0, 255, 255),
53
- "darkblue": (0, 0, 139),
54
- "darkcyan": (0, 139, 139),
55
- "darkgoldenrod": (184, 134, 11),
56
- "darkgray": (169, 169, 169),
57
- "darkgreen": (0, 100, 0),
58
- "darkgrey": (169, 169, 169),
59
- "darkkhaki": (189, 183, 107),
60
- "darkmagenta": (139, 0, 139),
61
- "darkolivegreen": (85, 107, 47),
62
- "darkorange": (255, 140, 0),
63
- "darkorchid": (153, 50, 204),
64
- "darkred": (139, 0, 0),
65
- "darksalmon": (233, 150, 122),
66
- "darkseagreen": (143, 188, 143),
67
- "darkslateblue": (72, 61, 139),
68
- "darkslategray": (47, 79, 79),
69
- "darkslategrey": (47, 79, 79),
70
- "darkturquoise": (0, 206, 209),
71
- "darkviolet": (148, 0, 211),
72
- "deeppink": (255, 20, 147),
73
- "deepskyblue": (0, 191, 255),
74
- "dimgray": (105, 105, 105),
75
- "dimgrey": (105, 105, 105),
76
- "dodgerblue": (30, 144, 255),
77
- "firebrick": (178, 34, 34),
78
- "floralwhite": (255, 250, 240),
79
- "forestgreen": (34, 139, 34),
80
- "fuchsia": (255, 0, 255),
81
- "gainsboro": (220, 220, 220),
82
- "ghostwhite": (248, 248, 255),
83
- "gold": (255, 215, 0),
84
- "goldenrod": (218, 165, 32),
85
- "gray": (128, 128, 128),
86
- "green": (0, 128, 0),
87
- "greenyellow": (173, 255, 47),
88
- "grey": (128, 128, 128),
89
- "honeydew": (240, 255, 240),
90
- "hotpink": (255, 105, 180),
91
- "indianred": (205, 92, 92),
92
- "indigo": (75, 0, 130),
93
- "ivory": (255, 255, 240),
94
- "khaki": (240, 230, 140),
95
- "lavender": (230, 230, 250),
96
- "lavenderblush": (255, 240, 245),
97
- "lawngreen": (124, 252, 0),
98
- "lemonchiffon": (255, 250, 205),
99
- "lightblue": (173, 216, 230),
100
- "lightcoral": (240, 128, 128),
101
- "lightcyan": (224, 255, 255),
102
- "lightgoldenrodyellow": (250, 250, 210),
103
- "lightgray": (211, 211, 211),
104
- "lightgreen": (144, 238, 144),
105
- "lightgrey": (211, 211, 211),
106
- "lightpink": (255, 182, 193),
107
- "lightsalmon": (255, 160, 122),
108
- "lightseagreen": (32, 178, 170),
109
- "lightskyblue": (135, 206, 250),
110
- "lightslategray": (119, 136, 153),
111
- "lightslategrey": (119, 136, 153),
112
- "lightsteelblue": (176, 196, 222),
113
- "lightyellow": (255, 255, 224),
114
- "lime": (0, 255, 0),
115
- "limegreen": (50, 205, 50),
116
- "linen": (250, 240, 230),
117
- "magenta": (255, 0, 255),
118
- "maroon": (128, 0, 0),
119
- "mediumaquamarine": (102, 205, 170),
120
- "mediumblue": (0, 0, 205),
121
- "mediumorchid": (186, 85, 211),
122
- "mediumpurple": (147, 112, 219),
123
- "mediumseagreen": (60, 179, 113),
124
- "mediumslateblue": (123, 104, 238),
125
- "mediumspringgreen": (0, 250, 154),
126
- "mediumturquoise": (72, 209, 204),
127
- "mediumvioletred": (199, 21, 133),
128
- "midnightblue": (25, 25, 112),
129
- "mintcream": (245, 255, 250),
130
- "mistyrose": (255, 228, 225),
131
- "moccasin": (255, 228, 181),
132
- "navajowhite": (255, 222, 173),
133
- "navy": (0, 0, 128),
134
- "oldlace": (253, 245, 230),
135
- "olive": (128, 128, 0),
136
- "olivedrab": (107, 142, 35),
137
- "orange": (255, 165, 0),
138
- "orangered": (255, 69, 0),
139
- "orchid": (218, 112, 214),
140
- "palegoldenrod": (238, 232, 170),
141
- "palegreen": (152, 251, 152),
142
- "paleturquoise": (175, 238, 238),
143
- "palevioletred": (219, 112, 147),
144
- "papayawhip": (255, 239, 213),
145
- "peachpuff": (255, 218, 185),
146
- "peru": (205, 133, 63),
147
- "pink": (255, 192, 203),
148
- "plum": (221, 160, 221),
149
- "powderblue": (176, 224, 230),
150
- "purple": (128, 0, 128),
151
- "red": (255, 0, 0),
152
- "rosybrown": (188, 143, 143),
153
- "royalblue": (65, 105, 225),
154
- "saddlebrown": (139, 69, 19),
155
- "salmon": (250, 128, 114),
156
- "sandybrown": (244, 164, 96),
157
- "seagreen": (46, 139, 87),
158
- "seashell": (255, 245, 238),
159
- "sienna": (160, 82, 45),
160
- "silver": (192, 192, 192),
161
- "skyblue": (135, 206, 235),
162
- "slateblue": (106, 90, 205),
163
- "slategray": (112, 128, 144),
164
- "slategrey": (112, 128, 144),
165
- "snow": (255, 250, 250),
166
- "springgreen": (0, 255, 127),
167
- "steelblue": (70, 130, 180),
168
- "tan": (210, 180, 140),
169
- "teal": (0, 128, 128),
170
- "thistle": (216, 191, 216),
171
- "tomato": (255, 99, 71),
172
- "turquoise": (64, 224, 208),
173
- "violet": (238, 130, 238),
174
- "wheat": (245, 222, 179),
175
- "white": (255, 255, 255),
176
- "whitesmoke": (245, 245, 245),
177
- "yellow": (255, 255, 0),
178
- "yellowgreen": (154, 205, 50),
179
- }
180
-
181
- _PYDANTIC_COLOR_NAMES = frozenset(_COLORS_BY_NAME.keys())
182
-
183
- _RICH_COLOR_NAMES = frozenset(
184
- [
185
- "black",
186
- "red",
187
- "green",
188
- "yellow",
189
- "blue",
190
- "magenta",
191
- "cyan",
192
- "white",
193
- "bright_black",
194
- "bright_red",
195
- "bright_green",
196
- "bright_yellow",
197
- "bright_blue",
198
- "bright_magenta",
199
- "bright_cyan",
200
- "bright_white",
201
- "grey0",
202
- "navy_blue",
203
- "dark_blue",
204
- "blue3",
205
- "blue1",
206
- "dark_green",
207
- "deep_sky_blue4",
208
- "dodger_blue3",
209
- "dodger_blue2",
210
- "green4",
211
- "spring_green4",
212
- "turquoise4",
213
- "deep_sky_blue3",
214
- "dodger_blue1",
215
- "dark_cyan",
216
- "light_sea_green",
217
- "deep_sky_blue2",
218
- "deep_sky_blue1",
219
- "green3",
220
- "spring_green3",
221
- "cyan3",
222
- "dark_turquoise",
223
- "turquoise2",
224
- "green1",
225
- "spring_green2",
226
- "spring_green1",
227
- "medium_spring_green",
228
- "cyan2",
229
- "cyan1",
230
- "purple4",
231
- "purple3",
232
- "blue_violet",
233
- "grey37",
234
- "medium_purple4",
235
- "slate_blue3",
236
- "royal_blue1",
237
- "chartreuse4",
238
- "pale_turquoise4",
239
- "steel_blue",
240
- "steel_blue3",
241
- "cornflower_blue",
242
- "dark_sea_green4",
243
- "cadet_blue",
244
- "sky_blue3",
245
- "chartreuse3",
246
- "sea_green3",
247
- "aquamarine3",
248
- "medium_turquoise",
249
- "steel_blue1",
250
- "sea_green2",
251
- "sea_green1",
252
- "dark_slate_gray2",
253
- "dark_red",
254
- "dark_magenta",
255
- "orange4",
256
- "light_pink4",
257
- "plum4",
258
- "medium_purple3",
259
- "slate_blue1",
260
- "wheat4",
261
- "grey53",
262
- "light_slate_grey",
263
- "medium_purple",
264
- "light_slate_blue",
265
- "yellow4",
266
- "dark_sea_green",
267
- "light_sky_blue3",
268
- "sky_blue2",
269
- "chartreuse2",
270
- "pale_green3",
271
- "dark_slate_gray3",
272
- "sky_blue1",
273
- "chartreuse1",
274
- "light_green",
275
- "aquamarine1",
276
- "dark_slate_gray1",
277
- "deep_pink4",
278
- "medium_violet_red",
279
- "dark_violet",
280
- "purple",
281
- "medium_orchid3",
282
- "medium_orchid",
283
- "dark_goldenrod",
284
- "rosy_brown",
285
- "grey63",
286
- "medium_purple2",
287
- "medium_purple1",
288
- "dark_khaki",
289
- "navajo_white3",
290
- "grey69",
291
- "light_steel_blue3",
292
- "light_steel_blue",
293
- "dark_olive_green3",
294
- "dark_sea_green3",
295
- "light_cyan3",
296
- "light_sky_blue1",
297
- "green_yellow",
298
- "dark_olive_green2",
299
- "pale_green1",
300
- "dark_sea_green2",
301
- "pale_turquoise1",
302
- "red3",
303
- "deep_pink3",
304
- "magenta3",
305
- "dark_orange3",
306
- "indian_red",
307
- "hot_pink3",
308
- "hot_pink2",
309
- "orchid",
310
- "orange3",
311
- "light_salmon3",
312
- "light_pink3",
313
- "pink3",
314
- "plum3",
315
- "violet",
316
- "gold3",
317
- "light_goldenrod3",
318
- "tan",
319
- "misty_rose3",
320
- "thistle3",
321
- "plum2",
322
- "yellow3",
323
- "khaki3",
324
- "light_yellow3",
325
- "grey84",
326
- "light_steel_blue1",
327
- "yellow2",
328
- "dark_olive_green1",
329
- "dark_sea_green1",
330
- "honeydew2",
331
- "light_cyan1",
332
- "red1",
333
- "deep_pink2",
334
- "deep_pink1",
335
- "magenta2",
336
- "magenta1",
337
- "orange_red1",
338
- "indian_red1",
339
- "hot_pink",
340
- "medium_orchid1",
341
- "dark_orange",
342
- "salmon1",
343
- "light_coral",
344
- "pale_violet_red1",
345
- "orchid2",
346
- "orchid1",
347
- "orange1",
348
- "sandy_brown",
349
- "light_salmon1",
350
- "light_pink1",
351
- "pink1",
352
- "plum1",
353
- "gold1",
354
- "light_goldenrod2",
355
- "navajo_white1",
356
- "misty_rose1",
357
- "thistle1",
358
- "yellow1",
359
- "light_goldenrod1",
360
- "khaki1",
361
- "wheat1",
362
- "cornsilk1",
363
- "grey100",
364
- "grey3",
365
- "grey7",
366
- "grey11",
367
- "grey15",
368
- "grey19",
369
- "grey23",
370
- "grey27",
371
- "grey30",
372
- "grey35",
373
- "grey39",
374
- "grey42",
375
- "grey46",
376
- "grey50",
377
- "grey54",
378
- "grey58",
379
- "grey62",
380
- "grey66",
381
- "grey70",
382
- "grey74",
383
- "grey78",
384
- "grey82",
385
- "grey85",
386
- "grey89",
387
- "grey93",
388
- ]
389
- )
390
-
391
- _ALL_COLOR_NAMES = _PYDANTIC_COLOR_NAMES | _RICH_COLOR_NAMES
392
-
393
- _RichColorName: TypeAlias = Literal[
394
- "black",
395
- "red",
396
- "green",
397
- "yellow",
398
- "blue",
399
- "magenta",
400
- "cyan",
401
- "white",
402
- "bright_black",
403
- "bright_red",
404
- "bright_green",
405
- "bright_yellow",
406
- "bright_blue",
407
- "bright_magenta",
408
- "bright_cyan",
409
- "bright_white",
410
- "grey0",
411
- "navy_blue",
412
- "dark_blue",
413
- "blue3",
414
- "blue1",
415
- "dark_green",
416
- "deep_sky_blue4",
417
- "dodger_blue3",
418
- "dodger_blue2",
419
- "green4",
420
- "spring_green4",
421
- "turquoise4",
422
- "deep_sky_blue3",
423
- "dodger_blue1",
424
- "dark_cyan",
425
- "light_sea_green",
426
- "deep_sky_blue2",
427
- "deep_sky_blue1",
428
- "green3",
429
- "spring_green3",
430
- "cyan3",
431
- "dark_turquoise",
432
- "turquoise2",
433
- "green1",
434
- "spring_green2",
435
- "spring_green1",
436
- "medium_spring_green",
437
- "cyan2",
438
- "cyan1",
439
- "purple4",
440
- "purple3",
441
- "blue_violet",
442
- "grey37",
443
- "medium_purple4",
444
- "slate_blue3",
445
- "royal_blue1",
446
- "chartreuse4",
447
- "pale_turquoise4",
448
- "steel_blue",
449
- "steel_blue3",
450
- "cornflower_blue",
451
- "dark_sea_green4",
452
- "cadet_blue",
453
- "sky_blue3",
454
- "chartreuse3",
455
- "sea_green3",
456
- "aquamarine3",
457
- "medium_turquoise",
458
- "steel_blue1",
459
- "sea_green2",
460
- "sea_green1",
461
- "dark_slate_gray2",
462
- "dark_red",
463
- "dark_magenta",
464
- "orange4",
465
- "light_pink4",
466
- "plum4",
467
- "medium_purple3",
468
- "slate_blue1",
469
- "wheat4",
470
- "grey53",
471
- "light_slate_grey",
472
- "medium_purple",
473
- "light_slate_blue",
474
- "yellow4",
475
- "dark_sea_green",
476
- "light_sky_blue3",
477
- "sky_blue2",
478
- "chartreuse2",
479
- "pale_green3",
480
- "dark_slate_gray3",
481
- "sky_blue1",
482
- "chartreuse1",
483
- "light_green",
484
- "aquamarine1",
485
- "dark_slate_gray1",
486
- "deep_pink4",
487
- "medium_violet_red",
488
- "dark_violet",
489
- "purple",
490
- "medium_orchid3",
491
- "medium_orchid",
492
- "dark_goldenrod",
493
- "rosy_brown",
494
- "grey63",
495
- "medium_purple2",
496
- "medium_purple1",
497
- "dark_khaki",
498
- "navajo_white3",
499
- "grey69",
500
- "light_steel_blue3",
501
- "light_steel_blue",
502
- "dark_olive_green3",
503
- "dark_sea_green3",
504
- "light_cyan3",
505
- "light_sky_blue1",
506
- "green_yellow",
507
- "dark_olive_green2",
508
- "pale_green1",
509
- "dark_sea_green2",
510
- "pale_turquoise1",
511
- "red3",
512
- "deep_pink3",
513
- "magenta3",
514
- "dark_orange3",
515
- "indian_red",
516
- "hot_pink3",
517
- "hot_pink2",
518
- "orchid",
519
- "orange3",
520
- "light_salmon3",
521
- "light_pink3",
522
- "pink3",
523
- "plum3",
524
- "violet",
525
- "gold3",
526
- "light_goldenrod3",
527
- "tan",
528
- "misty_rose3",
529
- "thistle3",
530
- "plum2",
531
- "yellow3",
532
- "khaki3",
533
- "light_yellow3",
534
- "grey84",
535
- "light_steel_blue1",
536
- "yellow2",
537
- "dark_olive_green1",
538
- "dark_sea_green1",
539
- "honeydew2",
540
- "light_cyan1",
541
- "red1",
542
- "deep_pink2",
543
- "deep_pink1",
544
- "magenta2",
545
- "magenta1",
546
- "orange_red1",
547
- "indian_red1",
548
- "hot_pink",
549
- "medium_orchid1",
550
- "dark_orange",
551
- "salmon1",
552
- "light_coral",
553
- "pale_violet_red1",
554
- "orchid2",
555
- "orchid1",
556
- "orange1",
557
- "sandy_brown",
558
- "light_salmon1",
559
- "light_pink1",
560
- "pink1",
561
- "plum1",
562
- "gold1",
563
- "light_goldenrod2",
564
- "navajo_white1",
565
- "misty_rose1",
566
- "thistle1",
567
- "yellow1",
568
- "light_goldenrod1",
569
- "khaki1",
570
- "wheat1",
571
- "cornsilk1",
572
- "grey100",
573
- "grey3",
574
- "grey7",
575
- "grey11",
576
- "grey15",
577
- "grey19",
578
- "grey23",
579
- "grey27",
580
- "grey30",
581
- "grey35",
582
- "grey39",
583
- "grey42",
584
- "grey46",
585
- "grey50",
586
- "grey54",
587
- "grey58",
588
- "grey62",
589
- "grey66",
590
- "grey70",
591
- "grey74",
592
- "grey78",
593
- "grey82",
594
- "grey85",
595
- "grey89",
596
- "grey93",
597
- ]
598
-
599
- _PydanticColorName: TypeAlias = Literal[
600
- "aliceblue",
601
- "antiquewhite",
602
- "aqua",
603
- "aquamarine",
604
- "azure",
605
- "beige",
606
- "bisque",
607
- "black",
608
- "blanchedalmond",
609
- "blue",
610
- "blueviolet",
611
- "brown",
612
- "burlywood",
613
- "cadetblue",
614
- "chartreuse",
615
- "chocolate",
616
- "coral",
617
- "cornflowerblue",
618
- "cornsilk",
619
- "crimson",
620
- "cyan",
621
- "darkblue",
622
- "darkcyan",
623
- "darkgoldenrod",
624
- "darkgray",
625
- "darkgreen",
626
- "darkgrey",
627
- "darkkhaki",
628
- "darkmagenta",
629
- "darkolivegreen",
630
- "darkorange",
631
- "darkorchid",
632
- "darkred",
633
- "darksalmon",
634
- "darkseagreen",
635
- "darkslateblue",
636
- "darkslategray",
637
- "darkslategrey",
638
- "darkturquoise",
639
- "darkviolet",
640
- "deeppink",
641
- "deepskyblue",
642
- "dimgray",
643
- "dimgrey",
644
- "dodgerblue",
645
- "firebrick",
646
- "floralwhite",
647
- "forestgreen",
648
- "fuchsia",
649
- "gainsboro",
650
- "ghostwhite",
651
- "gold",
652
- "goldenrod",
653
- "gray",
654
- "green",
655
- "greenyellow",
656
- "grey",
657
- "honeydew",
658
- "hotpink",
659
- "indianred",
660
- "indigo",
661
- "ivory",
662
- "khaki",
663
- "lavender",
664
- "lavenderblush",
665
- "lawngreen",
666
- "lemonchiffon",
667
- "lightblue",
668
- "lightcoral",
669
- "lightcyan",
670
- "lightgoldenrodyellow",
671
- "lightgray",
672
- "lightgreen",
673
- "lightgrey",
674
- "lightpink",
675
- "lightsalmon",
676
- "lightseagreen",
677
- "lightskyblue",
678
- "lightslategray",
679
- "lightslategrey",
680
- "lightsteelblue",
681
- "lightyellow",
682
- "lime",
683
- "limegreen",
684
- "linen",
685
- "magenta",
686
- "maroon",
687
- "mediumaquamarine",
688
- "mediumblue",
689
- "mediumorchid",
690
- "mediumpurple",
691
- "mediumseagreen",
692
- "mediumslateblue",
693
- "mediumspringgreen",
694
- "mediumturquoise",
695
- "mediumvioletred",
696
- "midnightblue",
697
- "mintcream",
698
- "mistyrose",
699
- "moccasin",
700
- "navajowhite",
701
- "navy",
702
- "oldlace",
703
- "olive",
704
- "olivedrab",
705
- "orange",
706
- "orangered",
707
- "orchid",
708
- "palegoldenrod",
709
- "palegreen",
710
- "paleturquoise",
711
- "palevioletred",
712
- "papayawhip",
713
- "peachpuff",
714
- "peru",
715
- "pink",
716
- "plum",
717
- "powderblue",
718
- "purple",
719
- "red",
720
- "rosybrown",
721
- "royalblue",
722
- "saddlebrown",
723
- "salmon",
724
- "sandybrown",
725
- "seagreen",
726
- "seashell",
727
- "sienna",
728
- "silver",
729
- "skyblue",
730
- "slateblue",
731
- "slategray",
732
- "slategrey",
733
- "snow",
734
- "springgreen",
735
- "steelblue",
736
- "tan",
737
- "teal",
738
- "thistle",
739
- "tomato",
740
- "turquoise",
741
- "violet",
742
- "wheat",
743
- "white",
744
- "whitesmoke",
745
- "yellow",
746
- "yellowgreen",
747
- ]
748
-
749
- ColorName: TypeAlias = Union[_RichColorName, _PydanticColorName]
750
- ColorType: TypeAlias = Union[ColorName, HexColor, RGBColor]
751
-
752
- # ------------------------------------------------------------
753
- # Color
754
- # ------------------------------------------------------------
755
-
756
-
757
- @dataclass
758
- class Color:
759
- """Optimized color class with caching and fast lookups."""
760
-
761
- _value: Union[ColorName, HexColor, RGBColor]
762
- _rich_color: _RichColorClass | None = None
763
- _style: Style | None = None
764
- _cache: ClassVar[Dict[Union[str, RGBColor], "Color"]] = {}
765
-
766
- def __post_init__(self):
767
- """Initialize rich color immediately to avoid lazy loading."""
768
- if self._rich_color is None:
769
- if isinstance(self._value, str):
770
- # Prefer Rich color names to preserve the name in string representation
771
- if self._value in _RICH_COLOR_NAMES:
772
- self._rich_color = _RichColorClass.parse(self._value)
773
- elif self._value in _PYDANTIC_COLOR_NAMES:
774
- rgb = _COLORS_BY_NAME[self._value]
775
- self._rich_color = _RichColorClass.from_rgb(rgb[0], rgb[1], rgb[2])
776
- else:
777
- self._rich_color = _RichColorClass.parse(self._value)
778
- elif isinstance(self._value, tuple):
779
- self._rich_color = _RichColorClass.from_rgb(
780
- self._value[0], self._value[1], self._value[2]
781
- )
782
-
783
- @classmethod
784
- def create(cls, color: ColorType) -> Self:
785
- """Creates a new color instance by parsing a given input color
786
- object.
787
-
788
- Examples:
789
- >>> Color.create("blue")
790
- >>> Color.create("#0000FF")
791
- >>> Color.create((0, 0, 255))
792
-
793
- Args:
794
- color: The color to create an instance of.
795
-
796
- Returns:
797
- A new color instance.
798
- """
799
- # Check cache first
800
- cache_key = color if isinstance(color, (str, tuple)) else str(color)
801
- if cache_key in cls._cache:
802
- return cls._cache[cache_key]
803
-
804
- # Create new instance
805
- if isinstance(color, str):
806
- if color in _ALL_COLOR_NAMES:
807
- instance = cls.from_name(color)
808
- else:
809
- instance = cls.from_hex(color)
810
- elif isinstance(color, tuple):
811
- instance = cls.from_rgb(color)
812
- else:
813
- raise ValueError(f"Invalid color type: {type(color)}")
814
-
815
- # Cache and return
816
- cls._cache[cache_key] = instance
817
- return instance
818
-
819
- @classmethod
820
- def from_name(cls, name: ColorName) -> Self:
821
- """Creates a new color instance by parsing a given color name.
822
-
823
- Examples:
824
- >>> Color.from_name("blue")
825
- >>> Color.from_name("red")
826
- >>> Color.from_name("green")
827
- """
828
- # Prefer Rich color names to preserve the name in string representation
829
- if name in _RICH_COLOR_NAMES:
830
- rich_color = _RichColorClass.parse(name)
831
- elif name in _PYDANTIC_COLOR_NAMES:
832
- rgb = _COLORS_BY_NAME[name]
833
- rich_color = _RichColorClass.from_rgb(rgb[0], rgb[1], rgb[2])
834
- else:
835
- rich_color = _RichColorClass.parse(name)
836
-
837
- return cls(_value=name, _rich_color=rich_color)
838
-
839
- @classmethod
840
- def from_hex(cls, hex_color: HexColor) -> Self:
841
- """Direct hex parsing."""
842
- return cls(_value=hex_color, _rich_color=_RichColorClass.parse(hex_color))
843
-
844
- @classmethod
845
- def from_rgb(cls, rgb: RGBColor) -> Self:
846
- """Direct RGB conversion."""
847
- return cls(
848
- _value=rgb, _rich_color=_RichColorClass.from_rgb(rgb[0], rgb[1], rgb[2])
849
- )
850
-
851
- @property
852
- def rich_color(self) -> _RichColorClass:
853
- """Direct access to rich color."""
854
- return self._rich_color
855
-
856
- def wrap(
857
- self,
858
- message: Any,
859
- bold: Optional[bool] = None,
860
- dim: Optional[bool] = None,
861
- italic: Optional[bool] = None,
862
- underline: Optional[bool] = None,
863
- blink: Optional[bool] = None,
864
- blink2: Optional[bool] = None,
865
- reverse: Optional[bool] = None,
866
- conceal: Optional[bool] = None,
867
- strike: Optional[bool] = None,
868
- underline2: Optional[bool] = None,
869
- frame: Optional[bool] = None,
870
- encircle: Optional[bool] = None,
871
- overline: Optional[bool] = None,
872
- link: Optional[str] = None,
873
- **kwargs: Any,
874
- ) -> Text:
875
- """Creates a new tag with the given style.
876
-
877
- Examples:
878
- >>> color = Color.from_name("blue")
879
- >>> color.tag("Hello, World!")
880
- >>> color.tag("Hello, World!", bold=True)
881
- """
882
- # Create style key for caching
883
- style_key = (
884
- bold,
885
- dim,
886
- italic,
887
- underline,
888
- blink,
889
- blink2,
890
- reverse,
891
- conceal,
892
- strike,
893
- underline2,
894
- frame,
895
- encircle,
896
- overline,
897
- link,
898
- )
899
-
900
- # Check if we already have this style
901
- if self._style is None or style_key != getattr(self, "_last_style_key", None):
902
- self._style = Style(
903
- color=self._rich_color,
904
- bold=bold,
905
- dim=dim,
906
- italic=italic,
907
- underline=underline,
908
- blink=blink,
909
- blink2=blink2,
910
- reverse=reverse,
911
- conceal=conceal,
912
- strike=strike,
913
- underline2=underline2,
914
- frame=frame,
915
- encircle=encircle,
916
- overline=overline,
917
- link=link,
918
- )
919
- self._last_style_key = style_key
920
-
921
- return Text(text=str(message), style=self._style, **kwargs)
922
-
923
- def __str__(self) -> str:
924
- """Fast string conversion."""
925
- # Return appropriate string representation based on the original value type
926
- if isinstance(self._value, str):
927
- # If it's a rich color name, return the name
928
- if self._value in _RICH_COLOR_NAMES:
929
- return self._value
930
- # If it's a pydantic-only color name, return hex
931
- elif self._value in _PYDANTIC_COLOR_NAMES:
932
- return self._rich_color.get_truecolor().hex
933
- # If it's a hex string, return lowercase hex
934
- else:
935
- return self._value.lower()
936
- elif isinstance(self._value, tuple):
937
- # For RGB tuples, return hex representation
938
- return self._rich_color.get_truecolor().hex
939
- else:
940
- # Fallback to rich color string representation
941
- return str(self._rich_color)
942
-
943
-
944
- if __name__ == "__main__":
945
- from rich import print
946
-
947
- color = Color.from_name("blue")
948
- print(color.wrap("Hello, World!"))
949
-
950
- color2 = Color.create("blue")
951
- print(color2.wrap("Cached color!"))