Handle deprecation of importlib.resources APIs
This commit is contained in:
parent
38f1fe7742
commit
7f2ce045ab
@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Any, Dict, FrozenSet, Generator, List, cast
|
||||
|
||||
from kitty.types import run_once
|
||||
|
||||
|
||||
aliases = {'url_hints': 'hints'}
|
||||
if TYPE_CHECKING:
|
||||
from kitty.conf.types import Definition
|
||||
@ -139,12 +140,9 @@ def run_kitten(kitten: str, run_name: str = '__main__') -> None:
|
||||
|
||||
@run_once
|
||||
def all_kitten_names() -> FrozenSet[str]:
|
||||
try:
|
||||
from importlib.resources import contents
|
||||
except ImportError:
|
||||
from importlib_resources import contents # type: ignore
|
||||
from kitty.constants import list_kitty_resources
|
||||
ans = []
|
||||
for name in contents('kittens'):
|
||||
for name in list_kitty_resources('kittens'):
|
||||
if '__' not in name and '.' not in name and name != 'tui':
|
||||
ans.append(name)
|
||||
return frozenset(ans)
|
||||
|
||||
@ -7,7 +7,7 @@ import os
|
||||
import pwd
|
||||
import sys
|
||||
from contextlib import suppress
|
||||
from typing import NamedTuple, Optional, Set, TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Iterable, NamedTuple, Optional, Set
|
||||
|
||||
from .types import run_once
|
||||
|
||||
@ -74,8 +74,8 @@ def _get_config_dir() -> str:
|
||||
return q
|
||||
|
||||
def make_tmp_conf() -> None:
|
||||
import tempfile
|
||||
import atexit
|
||||
import tempfile
|
||||
ans = tempfile.mkdtemp(prefix='kitty-conf-')
|
||||
|
||||
def cleanup() -> None:
|
||||
@ -181,12 +181,37 @@ def resolve_custom_file(path: str) -> str:
|
||||
return path
|
||||
|
||||
|
||||
def read_kitty_resource(name: str) -> bytes:
|
||||
try:
|
||||
def list_kitty_resources(package: str = 'kitty') -> Iterable[str]:
|
||||
if sys.version_info >= (3, 9):
|
||||
from importlib.resources import files
|
||||
|
||||
def contents(package: str) -> Iterable[str]:
|
||||
return (path.name for path in files(package).iterdir())
|
||||
elif sys.version_info < (3, 7):
|
||||
from importlib_resources import files
|
||||
|
||||
def contents(package: str) -> Iterable[str]:
|
||||
return (path.name for path in files(package).iterdir())
|
||||
else:
|
||||
from importlib.resources import contents
|
||||
return contents(package)
|
||||
|
||||
|
||||
def read_kitty_resource(name: str, package_name: str = 'kitty') -> bytes:
|
||||
if sys.version_info >= (3, 9):
|
||||
from importlib.resources import files
|
||||
|
||||
def read_binary(package: str, resource: str) -> bytes:
|
||||
return (files(package) / resource).read_bytes()
|
||||
elif sys.version_info < (3, 7):
|
||||
from importlib_resources import files
|
||||
|
||||
def read_binary(package: str, resource: str) -> bytes:
|
||||
return (files(package) / resource).read_bytes()
|
||||
else:
|
||||
from importlib.resources import read_binary
|
||||
except ImportError:
|
||||
from importlib_resources import read_binary # type: ignore
|
||||
return read_binary('kitty', name)
|
||||
|
||||
return read_binary(package_name, name)
|
||||
|
||||
|
||||
def website_url(doc_name: str = '') -> str:
|
||||
|
||||
@ -10,7 +10,7 @@ from typing import (
|
||||
|
||||
from kitty.cli import get_defaults_from_seq, parse_args, parse_option_spec
|
||||
from kitty.cli_stub import RCOptions as R
|
||||
from kitty.constants import appname, running_in_kitty
|
||||
from kitty.constants import appname, list_kitty_resources, running_in_kitty
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from kitty.boss import Boss as B
|
||||
@ -227,13 +227,9 @@ def command_for_name(cmd_name: str) -> RemoteCommand:
|
||||
|
||||
|
||||
def all_command_names() -> FrozenSet[str]:
|
||||
try:
|
||||
from importlib.resources import contents
|
||||
except ImportError:
|
||||
from importlib_resources import contents # type:ignore
|
||||
|
||||
def ok(name: str) -> bool:
|
||||
root, _, ext = name.rpartition('.')
|
||||
return bool(ext in ('py', 'pyc', 'pyo') and root and root not in ('base', '__init__'))
|
||||
|
||||
return frozenset({x.rpartition('.')[0] for x in filter(ok, contents('kitty.rc'))})
|
||||
return frozenset({x.rpartition('.')[0] for x in filter(ok, list_kitty_resources('kitty.rc'))})
|
||||
|
||||
@ -8,12 +8,8 @@ import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from functools import partial
|
||||
try:
|
||||
from importlib.resources import read_binary
|
||||
except ImportError:
|
||||
from importlib_resources import read_binary
|
||||
|
||||
from kitty.constants import is_macos
|
||||
from kitty.constants import is_macos, read_kitty_resource
|
||||
from kitty.fast_data_types import (
|
||||
DECAWM, get_fallback_font, sprite_map_set_layout, sprite_map_set_limits,
|
||||
test_render_line, test_sprite_position_for, wcwidth
|
||||
@ -88,7 +84,7 @@ class Rendering(BaseTest):
|
||||
if name not in font_path_cache:
|
||||
with open(os.path.join(self.tdir, name), 'wb') as f:
|
||||
font_path_cache[name] = f.name
|
||||
data = read_binary(__name__.rpartition('.')[0], name)
|
||||
data = read_kitty_resource(name, __name__.rpartition('.')[0])
|
||||
f.write(data)
|
||||
return font_path_cache[name]
|
||||
|
||||
|
||||
@ -6,11 +6,21 @@ import importlib
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
try:
|
||||
from typing import Callable, Generator, NoReturn, Sequence, Set, Iterable
|
||||
|
||||
|
||||
if sys.version_info >= (3, 9):
|
||||
from importlib.resources import files
|
||||
|
||||
def contents(package: str) -> Iterable[str]:
|
||||
return (path.name for path in files(package).iterdir())
|
||||
elif sys.version_info < (3, 7):
|
||||
from importlib_resources import files
|
||||
|
||||
def contents(package: str) -> Iterable[str]:
|
||||
return (path.name for path in files(package).iterdir())
|
||||
else:
|
||||
from importlib.resources import contents
|
||||
except Exception:
|
||||
from importlib_resources import contents
|
||||
from typing import Callable, Generator, NoReturn, Sequence, Set
|
||||
|
||||
|
||||
def itertests(suite: unittest.TestSuite) -> Generator[unittest.TestCase, None, None]:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user