Skip to content

Commit dde34a4

Browse files
authored
Merge pull request #3416 from trailofbits/importlib-for-resource-loading
Switches to using importlib for resource loading
2 parents 077f957 + b98f9b9 commit dde34a4

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
os: [ubuntu-latest] # windows-latest, macos-latest,
18-
python-version: ["3.8", "3.9", "3.10", "3.11"]
18+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
1919

2020
runs-on: ${{ matrix.os }}
2121

polyfile/kaitai/parser.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
from abc import ABC, abstractmethod
22
from dataclasses import dataclass
33
from enum import Enum
4+
from importlib import resources
45
import importlib.util
56
import inspect
67
from io import BufferedReader, BytesIO
78
import json
89
from pathlib import Path
10+
import sys
911
from typing import Any, Dict, Iterator, List, Optional, Set, Type, Union
1012

13+
from . import parsers
1114
from .compiler import CompiledKSY
1215
from ..fileutils import FileStream
1316

1417
from kaitaistruct import KaitaiStruct
1518

16-
PARSER_DIR: Path = Path(__file__).absolute().parent / "parsers"
17-
MANIFEST_FILE: Path = PARSER_DIR / "manifest.json"
19+
with resources.path(parsers, "manifest.json") as manifest_path:
20+
PARSER_DIR: Path = manifest_path.parent
21+
if sys.version_info >= (3, 9):
22+
with (resources.files(parsers) / "manifest.json").open("r") as f:
23+
MANIFEST: Dict[str, Dict[str, Any]] = json.load(f)
24+
else:
25+
with resources.open_text(parsers, "manifest.json") as f:
26+
MANIFEST = json.load(f)
1827

19-
with open(MANIFEST_FILE, "r") as f:
20-
MANIFEST: Dict[str, Dict[str, Any]] = json.load(f)
2128
COMPILED_INFO_BY_KSY: Dict[str, CompiledKSY] = {
2229
ksy_path: CompiledKSY(
2330
class_name=component["class_name"],

polyfile/magic.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import csv
1414
from datetime import datetime
1515
from enum import Enum, IntFlag
16+
from importlib import resources
1617
from io import StringIO
1718
import json
1819
import logging
@@ -34,6 +35,8 @@
3435
from .logger import getStatusLogger, TRACE
3536
from .repl import ANSIColor, ANSIWriter
3637

38+
from . import magic_defs
39+
3740

3841
if sys.version_info < (3, 9):
3942
from typing import Pattern
@@ -43,12 +46,27 @@
4346

4447
log = getStatusLogger("libmagic")
4548

46-
DEFS_DIR: Path = Path(__file__).absolute().parent / "magic_defs"
49+
50+
if sys.version_info < (3, 11):
51+
def get_resource_path(name: str) -> Path:
52+
with resources.path(magic_defs, name) as path:
53+
return path
54+
55+
def get_resource_contents(package):
56+
return resources.contents(package)
57+
else:
58+
def get_resource_path(name: str) -> Path:
59+
with resources.as_file(resources.files(magic_defs).joinpath(name)) as f:
60+
return f
61+
62+
def get_resource_contents(package):
63+
return (resource.name for resource in resources.files(package).iterdir() if resource.is_file())
64+
4765

4866
MAGIC_DEFS: List[Path] = [
49-
path
50-
for path in DEFS_DIR.glob("*")
51-
if path.name not in ("COPYING", "magic.mgc") and not path.name.startswith(".")
67+
get_resource_path(resource_name)
68+
for resource_name in get_resource_contents(magic_defs)
69+
if resource_name not in ("COPYING", "magic.mgc", "__pycache__") and not resource_name.startswith(".")
5270
]
5371

5472

polyfile/magic_defs/__init__.py

Whitespace-only changes.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
long_description_content_type="text/markdown",
2121
url='https://github.com/trailofbits/polyfile',
2222
author='Trail of Bits',
23-
version="0.5.3",
23+
version="0.5.4",
2424
packages=find_packages(exclude=("tests",)),
2525
python_requires='>=3.8',
2626
install_requires=[

0 commit comments

Comments
 (0)