mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-13 04:03:16 +02:00
Move caching implementation to Python
Less code, more performant since the cache is used from Python. And we can make the Go code a pure image format conversion filter.
This commit is contained in:
@@ -34,7 +34,6 @@ from typing import (
|
||||
|
||||
from .constants import (
|
||||
appname,
|
||||
cache_dir,
|
||||
clear_handled_signals,
|
||||
config_dir,
|
||||
is_macos,
|
||||
@@ -1227,33 +1226,15 @@ def timed_debug_print(*a: Any, sep: str = ' ', end: str = '\n') -> None:
|
||||
_timed_debug_print(sep.join(map(str, a)) + end)
|
||||
|
||||
|
||||
def cached_rgba_file_descriptor_for_image_path(path: str) -> Tuple[int, int, int]:
|
||||
from hashlib import sha256
|
||||
try:
|
||||
path = os.path.realpath(path, strict=True)
|
||||
except TypeError:
|
||||
path = os.path.realpath(path)
|
||||
src_info = os.stat(path)
|
||||
output_name = sha256(path.encode()).hexdigest() + '.rgba'
|
||||
output_path = os.path.join(cache_dir(), 'rgba', output_name)
|
||||
def lock_file(f: BinaryIO) -> None:
|
||||
if not f.writable():
|
||||
raise ValueError('Cannot lock files not opened in writable mode')
|
||||
import fcntl
|
||||
fcntl.lockf(f, fcntl.LOCK_EX)
|
||||
|
||||
def read_data(f: BinaryIO) -> Tuple[int, int, int]:
|
||||
header = f.read(8)
|
||||
import struct
|
||||
width, height = struct.unpack('<II', header)
|
||||
return width, height, os.dup(f.fileno())
|
||||
|
||||
with suppress(OSError), open(output_path, 'rb') as f:
|
||||
dest_info = os.stat(f.fileno())
|
||||
if dest_info.st_size == src_info.st_size and dest_info.st_mtime >= src_info.st_mtime:
|
||||
return read_data(f)
|
||||
|
||||
import subprocess
|
||||
cp = subprocess.run([kitten_exe(), '__render_image__', path], capture_output=True)
|
||||
if cp.returncode != 0:
|
||||
raise ValueError(f'Failed to convert path to RGBA data with error: {cp.stderr.decode("utf-8", "replace")}')
|
||||
ans = cp.stdout.decode().strip()
|
||||
if ans != output_path:
|
||||
raise ValueError(f'The two cache name algorithms dont agree for path: {path}\n{output_path} != {ans}')
|
||||
with open(ans, 'rb') as f:
|
||||
return read_data(f)
|
||||
def unlock_file(f: BinaryIO) -> None:
|
||||
if not f.writable():
|
||||
raise ValueError('Cannot unlock files not opened in writable mode')
|
||||
import fcntl
|
||||
fcntl.lockf(f, fcntl.LOCK_UN)
|
||||
|
||||
Reference in New Issue
Block a user