datacut 0.1.0__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.
- datacut/__init__.py +121 -0
- datacut/__main__.py +122 -0
- datacut/animation/__init__.py +0 -0
- datacut/animation/animation.py +336 -0
- datacut/animation/composition.py +258 -0
- datacut/animation/creation.py +315 -0
- datacut/animation/fading.py +328 -0
- datacut/animation/growing.py +90 -0
- datacut/animation/indication.py +608 -0
- datacut/animation/movement.py +165 -0
- datacut/animation/numbers.py +92 -0
- datacut/animation/physics.py +453 -0
- datacut/animation/rotation.py +95 -0
- datacut/animation/specialized.py +956 -0
- datacut/animation/transform.py +505 -0
- datacut/animation/transform_matching_parts.py +343 -0
- datacut/animation/update.py +95 -0
- datacut/camera/__init__.py +0 -0
- datacut/camera/camera.py +795 -0
- datacut/camera/camera_frame.py +320 -0
- datacut/camera/cinematography.py +1166 -0
- datacut/config.py +479 -0
- datacut/constants.py +160 -0
- datacut/css/__init__.py +189 -0
- datacut/css/easing.py +328 -0
- datacut/css/engine.py +1727 -0
- datacut/css/gradients.py +678 -0
- datacut/css/motion.py +1615 -0
- datacut/css/parser.py +741 -0
- datacut/css/presets.py +143 -0
- datacut/css/shader_fill.py +703 -0
- datacut/css/values.py +650 -0
- datacut/default_config.yml +224 -0
- datacut/event_handler/__init__.py +6 -0
- datacut/event_handler/event_dispatcher.py +161 -0
- datacut/event_handler/event_listner.py +31 -0
- datacut/event_handler/event_type.py +11 -0
- datacut/event_handler/picking.py +111 -0
- datacut/event_keys.py +53 -0
- datacut/extract_scene.py +200 -0
- datacut/layout/__init__.py +39 -0
- datacut/layout/components.py +425 -0
- datacut/layout/containers.py +1520 -0
- datacut/logger.py +13 -0
- datacut/mobject/__init__.py +0 -0
- datacut/mobject/boolean_ops.py +168 -0
- datacut/mobject/changing.py +209 -0
- datacut/mobject/coordinate_systems.py +1056 -0
- datacut/mobject/fractals.py +348 -0
- datacut/mobject/frame.py +80 -0
- datacut/mobject/functions.py +147 -0
- datacut/mobject/geometry.py +1834 -0
- datacut/mobject/gltf.py +243 -0
- datacut/mobject/interactive.py +906 -0
- datacut/mobject/lights.py +366 -0
- datacut/mobject/materials.py +207 -0
- datacut/mobject/matrix.py +570 -0
- datacut/mobject/mesh3d.py +2288 -0
- datacut/mobject/mobject.py +2885 -0
- datacut/mobject/mobject_update_utils.py +123 -0
- datacut/mobject/model.py +543 -0
- datacut/mobject/number_line.py +517 -0
- datacut/mobject/numbers.py +441 -0
- datacut/mobject/probability.py +536 -0
- datacut/mobject/shape_matchers.py +161 -0
- datacut/mobject/subtitles.py +260 -0
- datacut/mobject/svg/__init__.py +0 -0
- datacut/mobject/svg/brace.py +356 -0
- datacut/mobject/svg/drawings.py +1041 -0
- datacut/mobject/svg/old_tex_mobject.py +335 -0
- datacut/mobject/svg/path_cache.py +155 -0
- datacut/mobject/svg/special_tex.py +216 -0
- datacut/mobject/svg/string_mobject.py +622 -0
- datacut/mobject/svg/svg_mobject.py +657 -0
- datacut/mobject/svg/svg_parse.py +437 -0
- datacut/mobject/svg/tex_mobject.py +499 -0
- datacut/mobject/svg/text_mobject.py +702 -0
- datacut/mobject/svg/typst_mobject.py +864 -0
- datacut/mobject/three_dimensions.py +686 -0
- datacut/mobject/types/__init__.py +0 -0
- datacut/mobject/types/dot_cloud.py +220 -0
- datacut/mobject/types/image_mobject.py +130 -0
- datacut/mobject/types/point_cloud_mobject.py +138 -0
- datacut/mobject/types/shader_mobject.py +268 -0
- datacut/mobject/types/surface.py +848 -0
- datacut/mobject/types/vectorized_mobject.py +1749 -0
- datacut/mobject/types/video_mobject.py +3902 -0
- datacut/mobject/value_tracker.py +110 -0
- datacut/mobject/vector_field.py +585 -0
- datacut/module_loader.py +176 -0
- datacut/reactive/__init__.py +25 -0
- datacut/reactive/signals.py +610 -0
- datacut/renderer/__init__.py +4 -0
- datacut/renderer/drawing.py +1066 -0
- datacut/renderer/gpu.py +800 -0
- datacut/renderer/material.py +88 -0
- datacut/renderer/pipeline.py +179 -0
- datacut/renderer/post_process.py +577 -0
- datacut/renderer/renderer.py +1285 -0
- datacut/renderer/shader_source.py +146 -0
- datacut/renderer/shared_buffer.py +185 -0
- datacut/renderer/texture.py +217 -0
- datacut/renderer/uniform_block.py +118 -0
- datacut/scene/__init__.py +0 -0
- datacut/scene/incremental.py +233 -0
- datacut/scene/interactive_scene.py +758 -0
- datacut/scene/performance.py +226 -0
- datacut/scene/presentation_scene.py +783 -0
- datacut/scene/recording.py +541 -0
- datacut/scene/safe_zones.py +323 -0
- datacut/scene/scene.py +2849 -0
- datacut/scene/scene_embed.py +277 -0
- datacut/scene/scene_file_writer.py +1014 -0
- datacut/scene/scene_task.py +183 -0
- datacut/scene/sfx.py +164 -0
- datacut/scene/sound.py +702 -0
- datacut/scene/sound_clip.py +580 -0
- datacut/scene/timeline.py +1689 -0
- datacut/scene/transcript.py +503 -0
- datacut/shaders/fill.wgsl +107 -0
- datacut/shaders/fill_depth.wgsl +198 -0
- datacut/shaders/image.wgsl +25 -0
- datacut/shaders/inserts/NOTE.md +16 -0
- datacut/shaders/inserts/clip_test.wgsl +14 -0
- datacut/shaders/inserts/complex_functions.wgsl +20 -0
- datacut/shaders/inserts/fill_color.wgsl +10 -0
- datacut/shaders/inserts/finalize_color.wgsl +44 -0
- datacut/shaders/inserts/frame_uniforms.wgsl +37 -0
- datacut/shaders/inserts/frame_units.wgsl +18 -0
- datacut/shaders/inserts/image_quad.wgsl +28 -0
- datacut/shaders/inserts/mobject_uniforms.wgsl +11 -0
- datacut/shaders/inserts/project_point.wgsl +44 -0
- datacut/shaders/inserts/quad_corners.wgsl +12 -0
- datacut/shaders/inserts/read_data.wgsl +33 -0
- datacut/shaders/inserts/surface_mesh.wgsl +106 -0
- datacut/shaders/mandelbrot_fractal.wgsl +100 -0
- datacut/shaders/mesh.wgsl +474 -0
- datacut/shaders/newton_fractal.wgsl +200 -0
- datacut/shaders/present.wgsl +62 -0
- datacut/shaders/stroke.wgsl +1141 -0
- datacut/shaders/stroke_depth.wgsl +1136 -0
- datacut/shaders/surface.wgsl +40 -0
- datacut/shaders/textured_surface.wgsl +59 -0
- datacut/shaders/true_dot.wgsl +78 -0
- datacut/shaders/video.wgsl +42 -0
- datacut/tex_templates.yml +736 -0
- datacut/transcribe.py +32 -0
- datacut/typing.py +412 -0
- datacut/utils/__init__.py +0 -0
- datacut/utils/bezier.py +467 -0
- datacut/utils/browser_audio.py +423 -0
- datacut/utils/browser_code_transform.py +1293 -0
- datacut/utils/browser_completions.py +2255 -0
- datacut/utils/browser_fonts.py +143 -0
- datacut/utils/browser_pathops.py +47 -0
- datacut/utils/browser_scheduler.py +18 -0
- datacut/utils/browser_typst.py +82 -0
- datacut/utils/cache.py +34 -0
- datacut/utils/cinematic_vfx.py +779 -0
- datacut/utils/color.py +203 -0
- datacut/utils/debug.py +46 -0
- datacut/utils/dict_ops.py +22 -0
- datacut/utils/directories.py +55 -0
- datacut/utils/editor_signatures.py +1806 -0
- datacut/utils/editor_thumbnails.py +769 -0
- datacut/utils/family_ops.py +48 -0
- datacut/utils/file_ops.py +65 -0
- datacut/utils/images.py +52 -0
- datacut/utils/iterables.py +216 -0
- datacut/utils/native.py +280 -0
- datacut/utils/paths.py +83 -0
- datacut/utils/rate_functions.py +154 -0
- datacut/utils/render_geometry.py +186 -0
- datacut/utils/simple_functions.py +99 -0
- datacut/utils/sounds.py +43 -0
- datacut/utils/space_ops.py +460 -0
- datacut/utils/structured_array.py +256 -0
- datacut/utils/svg_export.py +102 -0
- datacut/utils/tex.py +139 -0
- datacut/utils/tex_file_writing.py +164 -0
- datacut/utils/tex_to_symbol_count.py +186 -0
- datacut/utils/typst_file_writing.py +205 -0
- datacut/utils/vfx_presets.py +4543 -0
- datacut/utils/visual_css.py +737 -0
- datacut/utils/visual_editor.py +4292 -0
- datacut/utils/window_audio.py +760 -0
- datacut/window.py +910 -0
- datacut-0.1.0.dist-info/METADATA +226 -0
- datacut-0.1.0.dist-info/RECORD +193 -0
- datacut-0.1.0.dist-info/WHEEL +5 -0
- datacut-0.1.0.dist-info/entry_points.txt +2 -0
- datacut-0.1.0.dist-info/licenses/LICENSE.md +21 -0
- datacut-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import sys
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class BrowserFont:
|
|
10
|
+
name: str
|
|
11
|
+
family: str
|
|
12
|
+
path: str
|
|
13
|
+
style: str = "Regular"
|
|
14
|
+
weight: int = 400
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _font_metadata(path: str) -> dict[str, object]:
|
|
18
|
+
from fontTools.ttLib import TTFont
|
|
19
|
+
|
|
20
|
+
font = TTFont(path)
|
|
21
|
+
names = font["name"].names
|
|
22
|
+
|
|
23
|
+
def get_name(name_id: int, fallback: str = "") -> str:
|
|
24
|
+
for record in names:
|
|
25
|
+
if record.nameID != name_id:
|
|
26
|
+
continue
|
|
27
|
+
try:
|
|
28
|
+
return record.toUnicode()
|
|
29
|
+
except Exception:
|
|
30
|
+
try:
|
|
31
|
+
return record.string.decode(record.getEncoding(), errors="replace")
|
|
32
|
+
except Exception:
|
|
33
|
+
continue
|
|
34
|
+
return fallback
|
|
35
|
+
|
|
36
|
+
family = get_name(1) or get_name(16) or get_name(6) or path.rsplit("/", 1)[-1]
|
|
37
|
+
subfamily = get_name(2) or get_name(17) or "Regular"
|
|
38
|
+
full_name = get_name(4) or f"{family} {subfamily}".strip()
|
|
39
|
+
|
|
40
|
+
weight = 400
|
|
41
|
+
if "OS/2" in font:
|
|
42
|
+
weight = int(getattr(font["OS/2"], "usWeightClass", 400) or 400)
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
"name": full_name,
|
|
46
|
+
"family": family,
|
|
47
|
+
"style": subfamily,
|
|
48
|
+
"weight": weight,
|
|
49
|
+
"path": path,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class BrowserFontManager:
|
|
54
|
+
def __init__(self):
|
|
55
|
+
self._fonts: dict[str, BrowserFont] = {}
|
|
56
|
+
|
|
57
|
+
async def register(self, file_or_buffer, file_name: str = "browser-font.ttf") -> BrowserFont:
|
|
58
|
+
if sys.platform != "emscripten":
|
|
59
|
+
raise RuntimeError("BrowserFontManager is only available in Pyodide")
|
|
60
|
+
|
|
61
|
+
from js import Uint8Array, window
|
|
62
|
+
|
|
63
|
+
if not hasattr(window, "manimRegisterPangoFont"):
|
|
64
|
+
raise RuntimeError("PangoCairo browser font API is not initialized")
|
|
65
|
+
|
|
66
|
+
if hasattr(file_or_buffer, "to_py"):
|
|
67
|
+
data = file_or_buffer.to_py()
|
|
68
|
+
else:
|
|
69
|
+
data = file_or_buffer
|
|
70
|
+
|
|
71
|
+
result = await window.manimRegisterPangoFont(data, file_name)
|
|
72
|
+
pango_name = str(result.name)
|
|
73
|
+
pango_family = str(result.family)
|
|
74
|
+
|
|
75
|
+
import tempfile
|
|
76
|
+
with tempfile.NamedTemporaryFile(suffix=file_name.rsplit(".", 1)[-1], delete=False) as tmp:
|
|
77
|
+
tmp.write(bytes(data))
|
|
78
|
+
metadata_path = tmp.name
|
|
79
|
+
|
|
80
|
+
try:
|
|
81
|
+
metadata = _font_metadata(metadata_path)
|
|
82
|
+
finally:
|
|
83
|
+
import os
|
|
84
|
+
os.unlink(metadata_path)
|
|
85
|
+
|
|
86
|
+
font = BrowserFont(
|
|
87
|
+
name=str(metadata["name"]) or pango_name,
|
|
88
|
+
family=str(metadata["family"]) or pango_family,
|
|
89
|
+
path=str(metadata["path"]),
|
|
90
|
+
style=str(metadata["style"]),
|
|
91
|
+
weight=int(metadata["weight"]),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
compiler = getattr(window, "manimTypstCompiler", None)
|
|
95
|
+
if compiler is None:
|
|
96
|
+
raise RuntimeError("Typst browser compiler is not initialized")
|
|
97
|
+
|
|
98
|
+
buffer = Uint8Array.new(len(data))
|
|
99
|
+
buffer.assign(data)
|
|
100
|
+
await compiler.addFonts(buffer)
|
|
101
|
+
|
|
102
|
+
self._fonts[font.family] = font
|
|
103
|
+
return font
|
|
104
|
+
|
|
105
|
+
async def register_path(self, path: str) -> BrowserFont:
|
|
106
|
+
if sys.platform != "emscripten":
|
|
107
|
+
raise RuntimeError("BrowserFontManager is only available in Pyodide")
|
|
108
|
+
|
|
109
|
+
with open(path, "rb") as file:
|
|
110
|
+
data = file.read()
|
|
111
|
+
|
|
112
|
+
font = await self.register(data, path.rsplit("/", 1)[-1])
|
|
113
|
+
return BrowserFont(
|
|
114
|
+
name=font.name,
|
|
115
|
+
family=font.family,
|
|
116
|
+
path=path,
|
|
117
|
+
style=font.style,
|
|
118
|
+
weight=font.weight,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
def describe_path(self, path: str) -> dict[str, object]:
|
|
122
|
+
return _font_metadata(path)
|
|
123
|
+
|
|
124
|
+
def get(self, family: str) -> BrowserFont | None:
|
|
125
|
+
return self._fonts.get(family)
|
|
126
|
+
|
|
127
|
+
def families(self) -> tuple[str, ...]:
|
|
128
|
+
return tuple(self._fonts)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
browser_font_manager = BrowserFontManager()
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
async def register_browser_font(file_or_buffer, file_name: str = "browser-font.ttf") -> BrowserFont:
|
|
135
|
+
return await browser_font_manager.register(file_or_buffer, file_name)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
async def register_browser_font_path(path: str) -> BrowserFont:
|
|
139
|
+
return await browser_font_manager.register_path(path)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def describe_browser_font_path(path: str) -> str:
|
|
143
|
+
return json.dumps(browser_font_manager.describe_path(path), ensure_ascii=False)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
if sys.platform == "emscripten":
|
|
6
|
+
from js import window
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class BrowserPathOps:
|
|
10
|
+
def __init__(self):
|
|
11
|
+
self.pathkit = None
|
|
12
|
+
|
|
13
|
+
def set_pathkit(self, pathkit) -> None:
|
|
14
|
+
self.pathkit = pathkit
|
|
15
|
+
|
|
16
|
+
def require(self):
|
|
17
|
+
if self.pathkit is None:
|
|
18
|
+
try:
|
|
19
|
+
self.pathkit = window.PathKit
|
|
20
|
+
except Exception:
|
|
21
|
+
pass
|
|
22
|
+
if self.pathkit is None:
|
|
23
|
+
raise RuntimeError(
|
|
24
|
+
"PathKit is not initialized. Load pathkit-wasm and call "
|
|
25
|
+
"await initialize_browser_wasm() before constructing boolean mobjects."
|
|
26
|
+
)
|
|
27
|
+
return self.pathkit
|
|
28
|
+
|
|
29
|
+
def combine(self, paths: list[str], operation: str) -> str:
|
|
30
|
+
pk = self.require()
|
|
31
|
+
if not paths:
|
|
32
|
+
return ""
|
|
33
|
+
result = pk.FromSVGString(paths[0])
|
|
34
|
+
try:
|
|
35
|
+
op = getattr(pk.PathOp, operation)
|
|
36
|
+
for source in paths[1:]:
|
|
37
|
+
other = pk.FromSVGString(source)
|
|
38
|
+
try:
|
|
39
|
+
result.op(other, op)
|
|
40
|
+
finally:
|
|
41
|
+
other.delete()
|
|
42
|
+
return str(result.toSVGString())
|
|
43
|
+
finally:
|
|
44
|
+
result.delete()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
browser_pathops = BrowserPathOps()
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
if sys.platform == "emscripten":
|
|
6
|
+
from js import window
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def next_animation_frame() -> float:
|
|
10
|
+
if sys.platform != "emscripten":
|
|
11
|
+
raise RuntimeError("next_animation_frame() is only available in Pyodide")
|
|
12
|
+
next_frame = getattr(window, "__manimNextFrame", None)
|
|
13
|
+
if next_frame is not None:
|
|
14
|
+
timestamp = await next_frame()
|
|
15
|
+
else:
|
|
16
|
+
promise = window.Promise.new(lambda resolve, reject: window.requestAnimationFrame(resolve))
|
|
17
|
+
timestamp = await promise
|
|
18
|
+
return float(timestamp) / 1000.0
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import inspect
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
if sys.platform == "emscripten":
|
|
7
|
+
from js import window
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def typst_to_svg_async(content: str) -> str:
|
|
11
|
+
if sys.platform != "emscripten":
|
|
12
|
+
from datacut.utils.typst_file_writing import typst_to_svg
|
|
13
|
+
return typst_to_svg(content)
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
compiler = window.manimTypstCompiler
|
|
17
|
+
except Exception as exc:
|
|
18
|
+
raise RuntimeError("typst-wasm is not initialized. Call await initialize_browser_wasm() first.") from exc
|
|
19
|
+
|
|
20
|
+
# The browser bridge may expose either a native JS Promise or a Python
|
|
21
|
+
# awaitable depending on how the compiler was initialized. Resolve both,
|
|
22
|
+
# including a nested awaitable returned by result.output.
|
|
23
|
+
source_result = compiler.addSource("main.typ", content)
|
|
24
|
+
if inspect.isawaitable(source_result):
|
|
25
|
+
await source_result
|
|
26
|
+
|
|
27
|
+
result = compiler.compile({"main": "main.typ", "format": "svg"})
|
|
28
|
+
if inspect.isawaitable(result):
|
|
29
|
+
result = await result
|
|
30
|
+
|
|
31
|
+
# typst-wasm 1.0 returns a document result whose SVG artifacts live on
|
|
32
|
+
# individual pages: result.pages[0].output.
|
|
33
|
+
try:
|
|
34
|
+
pages = result.pages
|
|
35
|
+
except AttributeError as exc:
|
|
36
|
+
raise RuntimeError("Typst compiler returned no pages") from exc
|
|
37
|
+
if inspect.isawaitable(pages):
|
|
38
|
+
pages = await pages
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
page_count = len(pages)
|
|
42
|
+
except TypeError:
|
|
43
|
+
page_count = 0
|
|
44
|
+
if page_count == 0:
|
|
45
|
+
raise RuntimeError("Typst produced no pages")
|
|
46
|
+
|
|
47
|
+
page = pages[0]
|
|
48
|
+
if inspect.isawaitable(page):
|
|
49
|
+
page = await page
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
output = page.output
|
|
53
|
+
except AttributeError as exc:
|
|
54
|
+
raise RuntimeError("Typst first page contains no SVG output") from exc
|
|
55
|
+
if inspect.isawaitable(output):
|
|
56
|
+
output = await output
|
|
57
|
+
if output is None:
|
|
58
|
+
raise RuntimeError("Typst first page produced no SVG output")
|
|
59
|
+
|
|
60
|
+
# The API normally returns SVG text. Keep a small compatibility path for
|
|
61
|
+
# Uint8Array-like values so Pyodide can consume either representation.
|
|
62
|
+
if not isinstance(output, str):
|
|
63
|
+
try:
|
|
64
|
+
from js import TextDecoder
|
|
65
|
+
if hasattr(output, "byteLength"):
|
|
66
|
+
output = TextDecoder.new().decode(output)
|
|
67
|
+
except Exception:
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
svg = str(output)
|
|
71
|
+
if inspect.iscoroutine(svg):
|
|
72
|
+
raise RuntimeError("Typst compiler returned an unresolved coroutine instead of SVG text")
|
|
73
|
+
return svg
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
async def initialize_browser_typst() -> None:
|
|
77
|
+
if sys.platform != "emscripten":
|
|
78
|
+
return
|
|
79
|
+
try:
|
|
80
|
+
window.manimTypstCompiler
|
|
81
|
+
except Exception:
|
|
82
|
+
await window.manimInitializeBrowserWasm()
|
datacut/utils/cache.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from diskcache import Cache
|
|
5
|
+
from contextlib import contextmanager
|
|
6
|
+
from functools import wraps
|
|
7
|
+
|
|
8
|
+
from datacut.utils.directories import get_cache_dir
|
|
9
|
+
from datacut.utils.simple_functions import hash_string
|
|
10
|
+
|
|
11
|
+
from typing import TYPE_CHECKING
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
T = TypeVar('T')
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
CACHE_SIZE = 1e9 # 1 Gig
|
|
18
|
+
_cache = Cache(get_cache_dir(), size_limit=CACHE_SIZE)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def cache_on_disk(func: Callable[..., T]) -> Callable[..., T]:
|
|
22
|
+
@wraps(func)
|
|
23
|
+
def wrapper(*args, **kwargs):
|
|
24
|
+
key = hash_string(f"{func.__name__}{args}{kwargs}")
|
|
25
|
+
value = _cache.get(key)
|
|
26
|
+
if value is None:
|
|
27
|
+
value = func(*args, **kwargs)
|
|
28
|
+
_cache.set(key, value)
|
|
29
|
+
return value
|
|
30
|
+
return wrapper
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def clear_cache():
|
|
34
|
+
_cache.clear()
|