Skip to content

Commit

Permalink
Replace pkg_resources w importlib
Browse files Browse the repository at this point in the history
  • Loading branch information
a-hurst committed Dec 28, 2023
1 parent 07cc0aa commit 2c62d62
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
6 changes: 3 additions & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import sys
import os
import shlex
import pkg_resources
from importlib.metadata import version as pkg_version
import sphinx_readable_theme

# If extensions (or modules to document with autodoc) are in another directory,
Expand Down Expand Up @@ -63,15 +63,15 @@

# General information about the project.
project = u'klibs'
copyright = u'2018, Austin Hurst & Jonathan Mulle'
copyright = u'2024, Austin Hurst & Jonathan Mulle'
author = u'Austin Hurst & Jonathan Mulle'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The full version, including alpha/beta/rc tags.
release = pkg_resources.require("klibs")[0].version
release = pkg_version('klibs')
# The short X.Y version.
version = release[:3]

Expand Down
13 changes: 7 additions & 6 deletions klibs/KLParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

__author__ = 'Jonathan Mulle & Austin Hurst'

from os.path import join
from os.path import join, dirname

# TODO: Try making the Params "P" an object or AttributeDict? Could set attributes
# dynamically but also allow for sanity checks and renaming variables w/o breaking
Expand Down Expand Up @@ -234,7 +234,7 @@ def initialize_runtime(exp_name, randseed):
"""
import random
import tempfile
from pkg_resources import resource_filename, resource_string
from importlib.util import find_spec

global random_seed
global klibs_commit
Expand All @@ -252,8 +252,9 @@ def initialize_runtime(exp_name, randseed):
database_local_path = join(tempfile.gettempdir(), database_local_filename)

# Load extra resources from KLibs package
klibs_commit_raw = resource_string('klibs', 'resources/current_commit.txt')
klibs_commit = str(klibs_commit_raw.decode('utf-8'))
logo_file_path = resource_filename('klibs', 'resources/splash.png')
font_dirs = [exp_font_dir, resource_filename('klibs', 'resources/font')]
klibs_root = dirname(find_spec("klibs").origin)
klibs_commit_path = join(klibs_root, 'resources', 'current_commit.txt')
klibs_commit = open(klibs_commit_path, mode='r').read()
logo_file_path = join(klibs_root, 'resources', 'splash.png')
font_dirs = [exp_font_dir, join(klibs_root, 'resources', 'font')]

5 changes: 3 additions & 2 deletions klibs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def create(name, path):
from random import choice
from os.path import join
from tempfile import mkdtemp
from pkg_resources import resource_filename
from importlib.util import find_spec

template_files = [
("schema.sql", ["ExpAssets", "Config"]),
Expand Down Expand Up @@ -216,7 +216,8 @@ def create(name, path):
ensure_directory_structure(tmp_path, create_missing=True)
cso(" <cyan>...Project template folders successfully created.</cyan>")

source_path = resource_filename('klibs', 'resources/template')
klibs_root = os.path.dirname(find_spec("klibs").origin)
source_path = os.path.join(klibs_root, 'resources', 'template')
for tf in template_files: # replace generic file names with project-specific names
filename = tf[0] if tf[0] in [".gitignore", "experiment.py"] else "{0}_{1}".format(name, tf[0])
template_f_path = join(source_path, tf[0] if tf[0] != ".gitignore" else "gitignore.txt")
Expand Down
13 changes: 10 additions & 3 deletions klibs/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-

import os
import tempfile
from importlib.util import find_spec

import sdl2
import pytest
import tempfile
from pkg_resources import resource_filename

from klibs import P

Expand All @@ -20,6 +22,11 @@ def _init_params_pytest():
P.screen_x, P.screen_y, P.refresh_rate = (1920, 1080, 60.0)


def get_resource_path(resource):
klibs_root = os.path.dirname(find_spec("klibs").origin)
return os.path.join(klibs_root, 'resources', resource)


@pytest.fixture(scope='module')
def with_sdl():
sdl2.SDL_ClearError()
Expand All @@ -33,7 +40,7 @@ def with_sdl():
def with_txtm(with_sdl):
import klibs.KLEnvironment as env
from klibs.KLText import TextManager
P.font_dirs = [resource_filename('klibs', 'resources/font')]
P.font_dirs = [get_resource_path('font')]
P.exp_font_dir = tempfile.gettempdir()
env.txtm = TextManager()
yield
Expand Down
5 changes: 2 additions & 3 deletions klibs/tests/test_KLDatabase.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import os
import tempfile
import pytest
from pkg_resources import resource_filename

import klibs
from klibs import KLDatabase as kldb
from klibs.KLRuntimeInfo import runtime_info_init

from conftest import _init_params_pytest
from conftest import _init_params_pytest, get_resource_path


schema_path = resource_filename('klibs', 'resources/template/schema.sql')
schema_path = get_resource_path('template/schema.sql')

@pytest.fixture
def db_test_path():
Expand Down
9 changes: 5 additions & 4 deletions klibs/tests/test_KLExperiment.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import pytest
import mock
import os
from pkg_resources import resource_filename
import mock
import pytest

import klibs
from klibs.KLJSON_Object import AttributeDict

from conftest import get_resource_path


@pytest.fixture
def experiment():
from klibs.KLExperiment import Experiment
from klibs import P
template_path = resource_filename('klibs', 'resources/template')
template_path = get_resource_path('template')
P.ind_vars_file_path = os.path.join(template_path, "independent_variables.py")
P.ind_vars_file_local_path = os.path.join(template_path, "doesnt_exist.py")
P.manual_trial_generation = True
Expand Down
5 changes: 3 additions & 2 deletions klibs/tests/test_KLNumpySurface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import numpy as np
from PIL import Image, ImageDraw
from aggdraw import Draw
from pkg_resources import resource_filename
from klibs.KLGraphics import KLDraw as kld
from klibs.KLGraphics import NumpySurface, aggdraw_to_numpy_surface

from conftest import get_resource_path


def maketestsurface():
testarr = np.zeros((100, 100, 4), dtype=np.uint8)
Expand Down Expand Up @@ -71,7 +72,7 @@ def test_init_aggdraw(self):
assert surf.content[0][0][0] == 255

def test_init_file(self):
logo_file_path = resource_filename('klibs', 'resources/splash.png')
logo_file_path = get_resource_path('splash.png')
surf = NumpySurface(logo_file_path)
assert surf.height == 123 and surf.width == 746
assert surf.content[0][0][0] == 0
Expand Down

0 comments on commit 2c62d62

Please sign in to comment.