Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pkg_resources dependency #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion sr/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
__import__('pkg_resources').declare_namespace(__name__)
8 changes: 5 additions & 3 deletions sr/tools/bom/parts_db.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
"""Student Robotics parts database access library."""
import csv

import pkg_resources
import six
from pathlib import Path

from sr.tools.bom import digikey, farnell, mouser, rs

module_dir = Path(__file__).resolve().parent


def get_db():
"""Get the parts DB that is embedded with this library."""
file = pkg_resources.resource_stream('sr.tools.bom', 'component_lib.csv')
return Db(six.StringIO(file.read().decode('UTF-8')))
file = module_dir / 'component_lib.csv'
Copy link
Member

@PeterJCLaw PeterJCLaw Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a dependency on the package being on the filesystem in the same layout as the source, which the previous spelling (I think) did not. Is there no equivalent package resource lookup mechanism in the new world?

(also elsewhere)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like importlib.resources might be what we want here? Not sure though.

return Db(six.StringIO(file.read_text()))


class Part(dict):
Expand Down
6 changes: 2 additions & 4 deletions sr/tools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import importlib
import sys
import traceback

import pkg_resources

from importlib.metadata import version as get_metadata_version
from sr.tools import __description__
from sr.tools.inventory.inventory import NotAnInventoryError

Expand Down Expand Up @@ -58,7 +56,7 @@


def get_version():
version = pkg_resources.get_distribution('sr.tools').version
version = get_metadata_version("sr.tools")
return "Student Robotics Tools {} (Python {}.{}.{})".format(
version,
*sys.version_info[0:3],
Expand Down
9 changes: 6 additions & 3 deletions sr/tools/cli/create_bom.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from pathlib import Path

res = 150 # Image resolution in DPI

module_dir = Path(__file__).resolve().parent


def html_header(f, names=None, image=None, xy=None):
import base64

import pkg_resources
from six.moves import reduce

header_file = pkg_resources.resource_stream('sr.tools.cli', 'bom_header.html')
header = header_file.read().decode('UTF-8')
header_file = module_dir / 'bom_header.html'
header = header_file.read_text()

title = ""
if names is not None:
Expand Down
16 changes: 9 additions & 7 deletions sr/tools/cli/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def command(args):
import tempfile
import zipfile

import pkg_resources
from pathlib import Path

module_dir = Path(__file__).resolve().parent

source = args.source.read()

Expand All @@ -53,8 +55,8 @@ def command(args):
r'\subsection*',
)

prefix_file = pkg_resources.resource_stream('sr.tools.cli', 'document_prefix.tex')
suffix_file = pkg_resources.resource_stream('sr.tools.cli', 'document_suffix.tex')
prefix_file = (module_dir / 'document_prefix.tex').read_text()
suffix_file = (module_dir / 'document_suffix.tex').read_text()
prefix = prefix_file.read().decode('UTF-8')
suffix = suffix_file.read().decode('UTF-8')

Expand Down Expand Up @@ -83,10 +85,10 @@ def command(args):
with open(main_file, 'w') as f:
f.write(total)

file = pkg_resources.resource_stream('sr.tools.cli', 'latex-assets.zip')
with zipfile.ZipFile(file) as zf:
for name in ('ecs.png', 'moto.png', 'bitbox.png', 'sr-logo.pdf'):
zf.extract(name, temp_dir)
with (module_dir / 'latex-assets.zip').open(mode="rb") as file:
with zipfile.ZipFile(file) as zf:
for name in ('ecs.png', 'moto.png', 'bitbox.png', 'sr-logo.pdf'):
zf.extract(name, temp_dir)

cmdline = ['pdflatex', '-interaction=nonstopmode', 'main.tex']
pdflatex_proc = subprocess.Popen(
Expand Down