Skip to content

Commit

Permalink
deprecate importlib and py37
Browse files Browse the repository at this point in the history
  • Loading branch information
dromer committed Oct 27, 2023
1 parent f77c6b4 commit ce3136f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v3
Expand Down
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License
Operating System :: OS Independent
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12

[options]
include_package_data = True
package_dir =
= src
packages = find:
python_requires = >=3.7
python_requires = >=3.8
install_requires =
Jinja2>=2.11
[options.packages.find]
Expand Down
18 changes: 11 additions & 7 deletions src/json2daisy/json2daisy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import jinja2
import json
import os
import pkg_resources

try:
from importlib_resources import files as resource_files
except ImportError:
from importlib.resources import files as resource_files

from typing import Optional

Expand All @@ -11,7 +15,7 @@
# helper for loading and processing the definitions, component list, etc
def map_load(pair: list):
# load the default components
comp_string = pkg_resources.resource_string(__name__, json_defs_file)
comp_string = resource_files('json2daisy').joinpath(json_defs_file).read_text()
component_defs = json.loads(comp_string)

pair[1]['name'] = pair[0]
Expand Down Expand Up @@ -232,7 +236,7 @@ def generate_header(board_description_dict: dict) -> 'tuple[str, dict]':
components, 'component', ['AnalogControl', 'AnalogControlBipolar'],
'map_init', key_exclude='default', match_exclude=True)

comp_string = pkg_resources.resource_string(__name__, json_defs_file)
comp_string = resource_files('json2daisy').joinpath(json_defs_file).read_text()
definitions_dict = json.loads(comp_string)

for name in definitions_dict:
Expand Down Expand Up @@ -270,7 +274,7 @@ def generate_header(board_description_dict: dict) -> 'tuple[str, dict]':
replacements['hidupdaterates'] = filter_map_template(
components, 'updaterate', key_exclude='default', match_exclude=True)

license_string = pkg_resources.resource_string(__package__, 'resources/LICENSE').decode('utf-8')
license_string = resource_files('json2daisy').joinpath('resources/LICENSE').read_text()
replacements['license'] = '/*\n * ' + '\n * '.join([line for line in license_string.split('\n')]) + '\n */'

component_declarations = list(filter(lambda x: not x.get('default', False), components))
Expand All @@ -296,12 +300,12 @@ def generate_header(board_description_dict: dict) -> 'tuple[str, dict]':
# rendered_header = env.get_template('daisy.h').render(replacements)

# This following works, but is really annoying
header_str = pkg_resources.resource_string(__name__, os.path.join('templates', 'daisy.h'))
header_str = resource_files('json2daisy').joinpath(os.path.join('templates', 'daisy.h')).read_text()
header_env = jinja2.Environment(
loader=jinja2.BaseLoader(),
trim_blocks=True,
lstrip_blocks=True
).from_string(header_str.decode('utf-8'))
).from_string(header_str)

rendered_header = header_env.render(replacements)

Expand Down Expand Up @@ -360,7 +364,7 @@ def generate_header_from_name(board_name: str) -> 'tuple[str, dict]':

try:
description_file = os.path.join('resources', f'{board_name}.json')
daisy_description = pkg_resources.resource_string(__name__, description_file)
daisy_description = resource_files('json2daisy').joinpath(description_file).read_text()
daisy_description_dict = json.loads(daisy_description)
except FileNotFoundError:
raise FileNotFoundError(f'Unknown Daisy board "{board_name}"')
Expand Down

0 comments on commit ce3136f

Please sign in to comment.