Skip to content

Commit e556ddb

Browse files
committed
Remove dependency on importlib_metadata
1 parent dcb4861 commit e556ddb

4 files changed

Lines changed: 38 additions & 35 deletions

File tree

klippy/printer.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import sys, os, gc, optparse, logging, time, collections
88
import importlib, importlib.util
9-
import importlib_metadata
109

1110
from . import compat
1211
from . import util, reactor, queuelogger, msgproto
@@ -147,18 +146,17 @@ def load_object(self, config, section, default=configfile.sentinel):
147146
plugins_spec = importlib.util.find_spec(
148147
f".{module_name}", "klippy.plugins"
149148
)
150-
entrypoints = importlib_metadata.entry_points(
151-
group="kalico.plugins", name=module_name
152-
)
153-
if entrypoints:
149+
entrypoints = util.find_entrypoints()
150+
if module_name in entrypoints:
154151
entrypoint = entrypoints[module_name]
152+
metadata = util.entrypoint_metadata(entrypoint)
155153
if extras_spec and not get_danger_options().allow_plugin_override:
156154
raise self.config_error(
157155
f"Module '{section}' found in both extras and "
158-
f"installed plugin {entrypoint.dist.name} (v{entrypoint.dist.version})"
156+
f"installed plugin {metadata['name']} (v{metadata['version']})"
159157
)
160158
logging.info(
161-
f"Loading '{module_name}' from plugin {entrypoint.dist.name} ({entrypoint.dist.version})"
159+
f"Loading '{module_name}' from plugin {metadata['name']} (v{metadata['version']})"
162160
)
163161
mod = entrypoint.load()
164162
elif plugins_spec:
@@ -558,10 +556,10 @@ def main():
558556
extra_git_desc += "\nRemote: %s" % (git_info["remote"])
559557
extra_git_desc += "\nTracked URL: %s" % (git_info["url"])
560558

561-
plugins = importlib_metadata.entry_points(group="kalico.plugins")
562-
for plugin in plugins:
563-
extra_git_desc += f"\nPlugin {plugin.dist.name}=={plugin.dist.version}"
564-
start_args["plugins"][plugin.dist.name] = plugin.dist.metadata.json
559+
for entrypoint in util.find_entrypoints().values():
560+
metadata = util.entrypoint_metadata(entrypoint)
561+
extra_git_desc += f"\nPlugin {metadata['name']}=={metadata['version']}"
562+
start_args["plugins"][metadata["name"]] = metadata
565563

566564
start_args["software_version"] = git_vers
567565
start_args["git_branch"] = git_info["branch"]

klippy/util.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# This file may be distributed under the terms of the GNU GPLv3 license.
66
import os, pty, fcntl, termios, signal, logging, json, time
7+
import functools
8+
import importlib.metadata
79
import subprocess
810
import traceback
911

@@ -103,6 +105,33 @@ def dump_mcu_build():
103105
dump_file_stats(build_dir, "out/klipper.elf")
104106

105107

108+
######################################################################
109+
# Plugin loading and metadata
110+
######################################################################
111+
112+
113+
@functools.cache
114+
def find_entrypoints() -> dict[str, importlib.metadata.EntryPoint]:
115+
entrypoints = importlib.metadata.entry_points()
116+
if hasattr(entrypoints, "select"):
117+
return {e.name: e for e in entrypoints.select(group="kalico.plugins")}
118+
else:
119+
return {e.name: e for e in entrypoints["kalico.plugins"]}
120+
121+
122+
@functools.cache
123+
def entrypoint_metadata(
124+
entrypoint: importlib.metadata.EntryPoint,
125+
) -> dict[str, str]:
126+
metadata = importlib.metadata.metadata(entrypoint.value)
127+
return {
128+
"name": metadata.get("name", entrypoint.name),
129+
"version": metadata.get("version", None),
130+
"summary": metadata.get("summary", None),
131+
"description": metadata.get_payload(),
132+
}
133+
134+
106135
######################################################################
107136
# General system and software information
108137
######################################################################

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ dependencies = [
1515
"pyserial==3.4",
1616
"python-can~=4.5",
1717
"setuptools==75.8.2 ; python_version >= '3.12'", # required by python-can < 4.3
18-
"importlib-metadata>=8.7.0",
1918
]
2019

2120
[tool.uv]

uv.lock

Lines changed: 0 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)