Dont use the deprecated importlib.resources API

This commit is contained in:
Kovid Goyal
2022-11-01 12:38:24 +05:30
parent 88b829fc9b
commit 3f28cf45d4
2 changed files with 22 additions and 9 deletions

View File

@@ -6,7 +6,7 @@ import os
import pwd import pwd
import sys import sys
from contextlib import suppress from contextlib import suppress
from typing import TYPE_CHECKING, Any, Iterable, NamedTuple, Optional, Set from typing import TYPE_CHECKING, Any, Iterator, NamedTuple, Optional, Set
from .types import run_once from .types import run_once
@@ -219,15 +219,24 @@ def running_in_kitty(set_val: Optional[bool] = None) -> bool:
return bool(getattr(running_in_kitty, 'ans', False)) return bool(getattr(running_in_kitty, 'ans', False))
def list_kitty_resources(package: str = 'kitty') -> Iterable[str]: def list_kitty_resources(package: str = 'kitty') -> Iterator[str]:
from importlib.resources import contents try:
return contents(package) from importlib.resources import files
except ImportError:
from importlib.resources import contents
return iter(contents(package))
else:
return (path.name for path in files(package).iterdir())
def read_kitty_resource(name: str, package_name: str = 'kitty') -> bytes: def read_kitty_resource(name: str, package_name: str = 'kitty') -> bytes:
from importlib.resources import read_binary try:
from importlib.resources import files
return read_binary(package_name, name) except ImportError:
from importlib.resources import read_binary
return read_binary(package_name, name)
else:
return (files(package_name) / name).read_bytes()
def website_url(doc_name: str = '', website: str = 'https://sw.kovidgoyal.net/kitty/') -> str: def website_url(doc_name: str = '', website: str = 'https://sw.kovidgoyal.net/kitty/') -> str:

View File

@@ -5,8 +5,12 @@ import importlib
import os import os
import shutil import shutil
import unittest import unittest
from importlib.resources import contents from importlib.resources import files
from typing import Callable, Generator, NoReturn, Sequence, Set from typing import Callable, Generator, NoReturn, Sequence, Set, Iterator
def contents(package: str) -> Iterator[str]:
return (path.name for path in files(package).iterdir())
def itertests(suite: unittest.TestSuite) -> Generator[unittest.TestCase, None, None]: def itertests(suite: unittest.TestSuite) -> Generator[unittest.TestCase, None, None]: