Use importlib to load the shaders

This commit is contained in:
Kovid Goyal
2021-02-16 10:23:18 +05:30
parent 74c4e94c3d
commit 7eb8e1df79
2 changed files with 16 additions and 12 deletions

View File

@@ -24,14 +24,14 @@ version: Version = Version(0, 19, 3)
str_version: str = '.'.join(map(str, version))
_plat = sys.platform.lower()
is_macos: bool = 'darwin' in _plat
base = os.path.dirname(os.path.abspath(__file__))
kitty_lib_dir = os.path.dirname(os.path.abspath(__file__))
@run_once
def kitty_exe() -> str:
rpath = sys._xoptions.get('bundle_exe_dir')
if not rpath:
items = os.environ.get('PATH', '').split(os.pathsep) + [os.path.join(base, 'launcher')]
items = os.environ.get('PATH', '').split(os.pathsep) + [os.path.join(kitty_lib_dir, 'launcher')]
seen: Set[str] = set()
for candidate in filter(None, items):
if candidate not in seen:
@@ -113,7 +113,7 @@ def wakeup() -> None:
b.child_monitor.wakeup()
base_dir = os.path.dirname(base)
base_dir = os.path.dirname(kitty_lib_dir)
terminfo_dir = os.path.join(base_dir, 'terminfo')
logo_png_file = os.path.join(base_dir, 'logo', 'kitty.png')
beam_cursor_data_file = os.path.join(base_dir, 'logo', 'beam-cursor.png')
@@ -126,7 +126,7 @@ except KeyError:
def glfw_path(module: str) -> str:
return os.path.join(base, 'glfw-{}.so'.format(module))
return os.path.join(kitty_lib_dir, 'glfw-{}.so'.format(module))
def detect_if_wayland_ok() -> bool:
@@ -186,3 +186,8 @@ def resolve_custom_file(path: str) -> str:
if not os.path.isabs(path):
path = os.path.join(config_dir, path)
return path
def read_kitty_resource(name: str) -> bytes:
from importlib.resources import read_binary
return read_binary('kitty', name)

View File

@@ -19,15 +19,14 @@ from typing import (
)
from .constants import (
appname, is_macos, is_wayland, shell_path, supports_primary_selection
appname, is_macos, is_wayland, read_kitty_resource, shell_path,
supports_primary_selection
)
from .options_stub import Options
from .rgb import Color, to_color
from .types import ConvertibleToNumbers, run_once
from .typing import AddressFamily, PopenType, Socket, StartupCtx
BASE = os.path.dirname(os.path.abspath(__file__))
def expandvars(val: str, env: Mapping[str, str] = {}, fallback_to_os_env: bool = True) -> str:
@@ -59,11 +58,11 @@ def platform_window_id(os_window_id: int) -> Optional[int]:
def load_shaders(name: str) -> Tuple[str, str]:
from .fast_data_types import GLSL_VERSION
with open(os.path.join(BASE, '{}_vertex.glsl'.format(name))) as f:
vert = f.read().replace('GLSL_VERSION', str(GLSL_VERSION), 1)
with open(os.path.join(BASE, '{}_fragment.glsl'.format(name))) as f:
frag = f.read().replace('GLSL_VERSION', str(GLSL_VERSION), 1)
return vert, frag
def load(which: str) -> str:
return read_kitty_resource(f'{name}_{which}.glsl').decode('utf-8').replace('GLSL_VERSION', str(GLSL_VERSION), 1)
return load('vertex'), load('fragment')
def safe_print(*a: Any, **k: Any) -> None: