Skip to content

Commit

Permalink
Work around deprecated importlib APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ESultanik committed Nov 29, 2023
1 parent 2e4c918 commit a33f574
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
12 changes: 7 additions & 5 deletions polyfile/kaitai/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from io import BufferedReader, BytesIO
import json
from pathlib import Path
import sys
from typing import Any, Dict, Iterator, List, Optional, Set, Type, Union

from . import parsers
Expand All @@ -17,11 +18,12 @@

with resources.path(parsers, "manifest.json") as manifest_path:
PARSER_DIR: Path = manifest_path.parent
MANIFEST_FILE = (resources.files(parsers) / "manifest.json")


with MANIFEST_FILE.open("r") as f:
MANIFEST: Dict[str, Dict[str, Any]] = json.load(f)
if sys.version_info >= (3, 9):
with (resources.files(parsers) / "manifest.json").open("r") as f:
MANIFEST: Dict[str, Dict[str, Any]] = json.load(f)
else:
with resources.open_text(parsers, "manifest.json") as f:
MANIFEST = json.load(f)

COMPILED_INFO_BY_KSY: Dict[str, CompiledKSY] = {
ksy_path: CompiledKSY(
Expand Down
18 changes: 14 additions & 4 deletions polyfile/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,24 @@
log = getStatusLogger("libmagic")


def get_resource_path(name: str) -> Path:
with resources.path(magic_defs, name) as path:
return path
if sys.version_info < (3, 11):
def get_resource_path(name: str) -> Path:
with resources.path(magic_defs, name) as path:
return path

def get_resource_contents(package):
return resources.contents(package)
else:
def get_resource_path(name: str) -> Path:
return resources.as_file(resources.files(magic_defs).joinpath(name))

def get_resource_contents(package):
return (resource.name for resource in resources.files(package).iterdir() if resource.is_file())


MAGIC_DEFS: List[Path] = [
get_resource_path(resource_name)
for resource_name in resources.contents(magic_defs)
for resource_name in get_resource_contents(magic_defs)
if resource_name not in ("COPYING", "magic.mgc", "__pycache__") and not resource_name.startswith(".")
]

Expand Down

0 comments on commit a33f574

Please sign in to comment.