Skip to content

Commit

Permalink
Remove use of pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
sumpfork committed Oct 17, 2024
1 parent 9f8b9a9 commit fccb62c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
35 changes: 14 additions & 21 deletions src/domdiv/db.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import copy
import functools
import gzip
import importlib.resources
import json
import os

import pkg_resources
from loguru import logger

from . import config_options, db
from . import config_options, db, resource_handling
from .cards import Card, CardType

EXPANSION_EXTRA_POSTFIX = " extras"
Expand All @@ -21,33 +20,27 @@
@functools.lru_cache()
def get_languages(path="card_db"):
languages = []
for name in pkg_resources.resource_listdir("domdiv", path):
for name in importlib.resources.files(f"domdiv").joinpath(path).iterdir():
dir_path = os.path.join(path, name)
if pkg_resources.resource_isdir("domdiv", dir_path):
if importlib.resources.files("domdiv").joinpath(dir_path).is_dir():
cards_file = os.path.join(dir_path, f"cards_{name}.json.gz")
sets_file = os.path.join(dir_path, f"sets_{name}.json.gz")
types_file = os.path.join(dir_path, f"types_{name}.json.gz")
if (
pkg_resources.resource_exists("domdiv", cards_file)
and pkg_resources.resource_exists("domdiv", sets_file)
and pkg_resources.resource_exists("domdiv", types_file)
resource_handling.resource_exists(cards_file)
and resource_handling.resource_exists(sets_file)
and resource_handling.resource_exists(types_file)
):
languages.append(name)
if LANGUAGE_XX in languages:
languages.remove(LANGUAGE_XX)
return languages


def get_resource_stream(path):
return gzip.GzipFile(
fileobj=pkg_resources.resource_stream("domdiv", path),
)


@functools.lru_cache()
def get_expansions():
set_db_filepath = os.path.join("card_db", "sets_db.json.gz")
with get_resource_stream(set_db_filepath) as setfile:
with resource_handling.get_resource_stream(set_db_filepath) as setfile:
set_file = json.loads(setfile.read().decode("utf-8"))
assert set_file, "Could not load any sets from database"

Expand All @@ -69,7 +62,7 @@ def get_expansions():
@functools.lru_cache()
def get_global_groups():
type_db_filepath = os.path.join("card_db", "types_db.json.gz")
with get_resource_stream(type_db_filepath) as typefile:
with resource_handling.get_resource_stream(type_db_filepath) as typefile:
type_file = json.loads(typefile.read().decode("utf-8"))
assert type_file, "Could not load any card types from database"

Expand All @@ -90,7 +83,7 @@ def get_types(language=LANGUAGE_DEFAULT):
# get a list of valid types
language = language.lower()
type_text_filepath = os.path.join("card_db", language, f"types_{language}.json.gz")
with get_resource_stream(type_text_filepath) as type_text_file:
with resource_handling.get_resource_stream(type_text_filepath) as type_text_file:
type_text = json.loads(type_text_file.read().decode("utf-8"))
assert type_text, "Could not load type file for %r" % language

Expand All @@ -105,7 +98,7 @@ def get_label_data():
label_choices = []
label_keys = []
label_selections = []
with get_resource_stream(labels_db_filepath) as labelfile:
with resource_handling.get_resource_stream(labels_db_filepath) as labelfile:
label_info = json.loads(labelfile.read().decode("utf-8"))
assert label_info, "Could not load label information from database"
for label in label_info:
Expand Down Expand Up @@ -151,7 +144,7 @@ def find_index_of_object(lst=None, attributes=None):
def read_card_data(options):
# Read in the card types
types_db_filepath = os.path.join("card_db", "types_db.json.gz")
with db.get_resource_stream(types_db_filepath) as typefile:
with resource_handling.get_resource_stream(types_db_filepath) as typefile:
Card.types = json.loads(
typefile.read().decode("utf-8"), object_hook=CardType.decode_json
)
Expand All @@ -171,14 +164,14 @@ def read_card_data(options):

# Read in the card database
card_db_filepath = os.path.join("card_db", "cards_db.json.gz")
with get_resource_stream(card_db_filepath) as cardfile:
with resource_handling.get_resource_stream(card_db_filepath) as cardfile:
cards = json.loads(
cardfile.read().decode("utf-8"), object_hook=Card.decode_json
)
assert cards, "Could not load any cards from database"

set_db_filepath = os.path.join("card_db", "sets_db.json.gz")
with get_resource_stream(set_db_filepath) as setfile:
with resource_handling.get_resource_stream(set_db_filepath) as setfile:
Card.sets = json.loads(setfile.read().decode("utf-8"))
assert Card.sets, "Could not load any sets from database"
new_sets = {}
Expand Down
20 changes: 8 additions & 12 deletions src/domdiv/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import re

import pkg_resources
from loguru import logger
from PIL import Image, ImageEnhance
from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY, TA_LEFT
Expand All @@ -16,6 +15,7 @@
from reportlab.pdfgen import canvas
from reportlab.platypus import Paragraph, XPreformatted

from . import resource_handling
from .cards import Card


Expand Down Expand Up @@ -476,10 +476,6 @@ def __init__(self, options=None):
self.pages = None
self.options = options

@staticmethod
def get_image_filepath(fname):
return pkg_resources.resource_filename("domdiv", os.path.join("images", fname))

def draw(self, cards=None, options=None):
if cards is None:
cards = []
Expand Down Expand Up @@ -581,7 +577,7 @@ def registerFonts(self):
fontpaths[font] = (fpath, True)
break
fpath = os.path.join("fonts", fname)
if pkg_resources.resource_exists("domdiv", fpath):
if resource_handling.resource_exists(fpath):
fontpaths[font] = (fpath, False)
break
logger.trace(fontpaths)
Expand Down Expand Up @@ -694,7 +690,7 @@ def registerFonts(self):
(
fontpath
if is_local
else pkg_resources.resource_filename("domdiv", fontpath)
else resource_handling.get_resource_filepath(fontpath)
),
)
)
Expand Down Expand Up @@ -1163,7 +1159,7 @@ def replace_image_tag(
)
replace = font_replace + replace
replace = replace.format(
fpath=DividerDrawer.get_image_filepath(fname),
fpath=resource_handling.get_image_filepath(fname),
width=fontsize * fontsize_multiplier,
height_percent=height_percent,
)
Expand Down Expand Up @@ -1258,7 +1254,7 @@ def drawCardCount(self, card, x, y, offset=-1):
width += 16
x -= 16
self.canvas.drawImage(
DividerDrawer.get_image_filepath("card.png"),
resource_handling.get_image_filepath("card.png"),
x,
countHeight,
16,
Expand Down Expand Up @@ -1363,7 +1359,7 @@ def drawCostText(text, x, y, color=None):
self.canvas.restoreState()

def scaleImage(name, x, y, h, mask="auto"):
path = DividerDrawer.get_image_filepath(name)
path = resource_handling.get_image_filepath(name)
with Image.open(path) as img:
w0, h0 = img.size
scale = h / h0
Expand Down Expand Up @@ -1395,7 +1391,7 @@ def scaleImage(name, x, y, h, mask="auto"):
def drawSetIcon(self, setImage, x, y):
# set image
size = self.SET_ICON_SIZE
path = DividerDrawer.get_image_filepath(setImage)
path = resource_handling.get_image_filepath(setImage)
self.canvas.drawImage(
path, x, y, size, size, mask="auto", preserveAspectRatio=True
)
Expand Down Expand Up @@ -1456,7 +1452,7 @@ def prepArtwork(image, w, h, resolution, opacity):
from io import BytesIO

# Get the original image.
artwork = DividerDrawer.get_image_filepath(image)
artwork = resource_handling.get_image_filepath(image)

# Make any optional adjustments.
if resolution != 0 or opacity != 1.0:
Expand Down
12 changes: 7 additions & 5 deletions src/domdiv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from loguru import logger
from reportlab.lib.units import cm

from . import config_options, db
from . import config_options, db, resource_handling
from .cards import Card
from .draw import DividerDrawer

Expand Down Expand Up @@ -147,7 +147,7 @@ def add_card_text(cards, language="en_us"):
card_text_filepath = os.path.join(
"card_db", language, "cards_" + language.lower() + ".json.gz"
)
with db.get_resource_stream(card_text_filepath) as card_text_file:
with resource_handling.get_resource_stream(card_text_filepath) as card_text_file:
card_text = json.loads(card_text_file.read().decode("utf-8"))
assert language, "Could not load card text for %r" % language

Expand All @@ -167,7 +167,7 @@ def add_set_text(options, sets, language="en_us"):
language = language.lower()
# Read in the set text and store for later
set_text_filepath = os.path.join("card_db", language, f"sets_{language}.json.gz")
with db.get_resource_stream(set_text_filepath) as set_text_file:
with resource_handling.get_resource_stream(set_text_filepath) as set_text_file:
set_text = json.loads(set_text_file.read().decode("utf-8"))
assert set_text, "Could not load set text for %r" % language

Expand All @@ -185,7 +185,7 @@ def add_type_text(types=None, language="en_us"):
language = language.lower()
# Read in the type text and store for later
type_text_filepath = os.path.join("card_db", language, f"types_{language}.json.gz")
with db.get_resource_stream(type_text_filepath) as type_text_file:
with resource_handling.get_resource_stream(type_text_filepath) as type_text_file:
type_text = json.loads(type_text_file.read().decode("utf-8"))
assert type_text, "Could not load type text for %r" % language

Expand All @@ -209,7 +209,9 @@ def add_bonus_regex(options, language="en_us"):
bonus_regex_filepath = os.path.join(
"card_db", language, f"bonuses_{language}.json.gz"
)
with db.get_resource_stream(bonus_regex_filepath) as bonus_regex_file:
with resource_handling.get_resource_stream(
bonus_regex_filepath
) as bonus_regex_file:
bonus_regex = json.loads(bonus_regex_file.read().decode("utf-8"))
assert bonus_regex, "Could not load bonus keywords for %r" % language

Expand Down
28 changes: 28 additions & 0 deletions src/domdiv/resource_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import atexit
import contextlib
import gzip
import importlib.resources
import os


@contextlib.contextmanager
def get_resource_stream(path):
ref = importlib.resources.files("domdiv").joinpath(path)
with ref.open("rb") as f:
yield gzip.GzipFile(fileobj=f)


def get_resource_filepath(fpath):
file_manager = contextlib.ExitStack()
atexit.register(file_manager.close)
ref = importlib.resources.files("domdiv") / fpath
path = file_manager.enter_context(importlib.resources.as_file(ref))
return path


def get_image_filepath(fname):
return get_resource_filepath(os.path.join("images", fname))


def resource_exists(fpath):
return importlib.resources.files("domdiv").joinpath(fpath).is_file()

0 comments on commit fccb62c

Please sign in to comment.