Skip to content

Commit

Permalink
Merge pull request #327 from jodal/importlib-resources
Browse files Browse the repository at this point in the history
Use `importlib.resources` to load data files
  • Loading branch information
jodal authored Oct 3, 2024
2 parents 165509b + e48a757 commit f7ee4f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/biip/gs1/_application_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from __future__ import annotations

import json
import pathlib
from dataclasses import dataclass, field
from importlib import resources

from biip import ParseError

Expand Down Expand Up @@ -90,9 +90,8 @@ def __str__(self) -> str:


_GS1_APPLICATION_IDENTIFIERS_FILE = (
pathlib.Path(__file__).parent / "_application_identifiers.json"
resources.files("biip") / "gs1" / "_application_identifiers.json"
)

_GS1_APPLICATION_IDENTIFIERS = {
entry.ai: entry
for entry in [
Expand Down
13 changes: 6 additions & 7 deletions src/biip/gs1/_prefixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

import json
import lzma
import pathlib
from dataclasses import dataclass
from importlib import resources
from typing import Optional, Union

from biip import ParseError
Expand Down Expand Up @@ -142,16 +142,15 @@ class _GS1PrefixRange:
usage: str


_GS1_PREFIX_RANGES_FILE = pathlib.Path(__file__).parent / "_prefix_ranges.json"

_GS1_PREFIX_RANGES_FILE = resources.files("biip") / "gs1" / "_prefix_ranges.json"
_GS1_PREFIX_RANGES = [
_GS1PrefixRange(**kwargs)
for kwargs in json.loads(_GS1_PREFIX_RANGES_FILE.read_text())
]

_GS1_COMPANY_PREFIX_TRIE_FILE = (
pathlib.Path(__file__).parent / "_company_prefix_trie.json.lzma"
resources.files("biip") / "gs1" / "_company_prefix_trie.json.lzma"
)
_GS1_COMPANY_PREFIX_TRIE: _TrieNode = json.loads(
lzma.decompress(_GS1_COMPANY_PREFIX_TRIE_FILE.read_bytes()).decode()
)

with lzma.open(_GS1_COMPANY_PREFIX_TRIE_FILE) as fh:
_GS1_COMPANY_PREFIX_TRIE: _TrieNode = json.load(fh)

0 comments on commit f7ee4f1

Please sign in to comment.