From 0cbd8756091ea6428f7c8b0e92aac210f8f1546d Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Sat, 4 Jan 2025 15:10:35 -0500 Subject: [PATCH 1/6] startup: remove unused sys import --- ldmudefuns/startup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ldmudefuns/startup.py b/ldmudefuns/startup.py index a984202..b271808 100644 --- a/ldmudefuns/startup.py +++ b/ldmudefuns/startup.py @@ -8,7 +8,7 @@ def startup(): name_of_the_efun = off """ - import traceback, sys, os, configparser + import traceback, os, configparser import ldmud try: From 0b12222590073331d87c3bdf73438f4a2a74b2fc Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Mon, 10 Feb 2025 13:10:13 -0500 Subject: [PATCH 2/6] lift out shared config helper Reduce a bit of duplication by lifting out a `get_config()` helper function to a new `internal` package. --- ldmudefuns/help.py | 9 +++------ ldmudefuns/internal.py | 9 +++++++++ ldmudefuns/reload.py | 9 ++++----- ldmudefuns/startup.py | 9 ++++----- 4 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 ldmudefuns/internal.py diff --git a/ldmudefuns/help.py b/ldmudefuns/help.py index 4310023..bb403e0 100644 --- a/ldmudefuns/help.py +++ b/ldmudefuns/help.py @@ -1,5 +1,7 @@ import ldmud +from .internal import get_config + def format_docstring(efun) -> str: doc = getattr(efun, '__doc__', None) if doc: @@ -47,8 +49,6 @@ def python_efun_help(efunname: str) -> str: return format_docstring(efun) else: - import os, configparser - try: import importlib.metadata as metadata except ModuleNotFoundError: @@ -65,10 +65,7 @@ def python_efun_help(efunname: str) -> str: SEE ALSO python_reload(E) """ - config = configparser.ConfigParser() - config['efuns'] = {} - config.read(os.path.expanduser('~/.ldmud-efuns')) - efunconfig = config['efuns'] + efunconfig = get_config()['efuns'] if not efunconfig.getboolean(efunname, True): return None diff --git a/ldmudefuns/internal.py b/ldmudefuns/internal.py new file mode 100644 index 0000000..3f0ed64 --- /dev/null +++ b/ldmudefuns/internal.py @@ -0,0 +1,9 @@ +import os +import configparser + +def get_config(): + config = configparser.ConfigParser() + config['efuns'] = {} + config['types'] = {} + config.read(os.path.expanduser('~/.ldmud-efuns')) + return config diff --git a/ldmudefuns/reload.py b/ldmudefuns/reload.py index c65196c..8e2dc8e 100644 --- a/ldmudefuns/reload.py +++ b/ldmudefuns/reload.py @@ -1,4 +1,4 @@ -import importlib, sys, os, configparser +import importlib, sys import ldmud try: @@ -6,6 +6,8 @@ except ModuleNotFoundError: import importlib_metadata as metadata +from .internal import get_config + def reload_modules(): """ SYNOPSIS @@ -26,10 +28,7 @@ def reload_modules(): reloaded = set() eps = metadata.entry_points() - config = configparser.ConfigParser() - config['efuns'] = {} - config['types'] = {} - config.read(os.path.expanduser('~/.ldmud-efuns')) + config = get_config() ep_types = [] if hasattr(ldmud, 'register_type'): diff --git a/ldmudefuns/startup.py b/ldmudefuns/startup.py index b271808..00c230a 100644 --- a/ldmudefuns/startup.py +++ b/ldmudefuns/startup.py @@ -1,3 +1,5 @@ +from .internal import get_config + def startup(): """ Loads all registered packages that offer the ldmud_efun entry point. @@ -8,7 +10,7 @@ def startup(): name_of_the_efun = off """ - import traceback, os, configparser + import traceback import ldmud try: @@ -16,10 +18,7 @@ def startup(): except ModuleNotFoundError: import importlib_metadata as metadata - config = configparser.ConfigParser() - config['efuns'] = {} - config['types'] = {} - config.read(os.path.expanduser('~/.ldmud-efuns')) + config = get_config() ep_types = [] if hasattr(ldmud, 'register_type'): From 32eae235cbf76a2369b77b6ce2f0739e2c3d89af Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Mon, 10 Feb 2025 13:15:52 -0500 Subject: [PATCH 3/6] lift out registration types helper Removes some duplication between `startup.py` and `reload.py` with respect to processing the config for `ep_types`. --- ldmudefuns/internal.py | 10 ++++++++++ ldmudefuns/reload.py | 11 ++--------- ldmudefuns/startup.py | 13 ++----------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/ldmudefuns/internal.py b/ldmudefuns/internal.py index 3f0ed64..02c14b9 100644 --- a/ldmudefuns/internal.py +++ b/ldmudefuns/internal.py @@ -1,5 +1,6 @@ import os import configparser +import ldmud def get_config(): config = configparser.ConfigParser() @@ -7,3 +8,12 @@ def get_config(): config['types'] = {} config.read(os.path.expanduser('~/.ldmud-efuns')) return config + +def get_registration_types(): + ep_types = [] + config = get_config() + if hasattr(ldmud, 'register_type'): + ep_types.append(('ldmud_type', 'type', config['types'], ldmud.register_type)) + if hasattr(ldmud, 'register_efun'): + ep_types.append(('ldmud_efun', 'efun', config['efuns'], ldmud.register_efun)) + return ep_types diff --git a/ldmudefuns/reload.py b/ldmudefuns/reload.py index 8e2dc8e..ad042df 100644 --- a/ldmudefuns/reload.py +++ b/ldmudefuns/reload.py @@ -6,7 +6,7 @@ except ModuleNotFoundError: import importlib_metadata as metadata -from .internal import get_config +from .internal import get_registration_types def reload_modules(): """ @@ -27,14 +27,7 @@ def reload_modules(): modules = dict(sys.modules) reloaded = set() eps = metadata.entry_points() - - config = get_config() - - ep_types = [] - if hasattr(ldmud, 'register_type'): - ep_types.append(('ldmud_type', 'type', config['types'], ldmud.register_type,)) - if hasattr(ldmud, 'register_efun'): - ep_types.append(('ldmud_efun', 'efun', config['efuns'], ldmud.register_efun,)) + ep_types = get_registration_types() for ep_name, ep_desc, ep_config, ep_register in ep_types: for entry_point in eps.get(ep_name,()): diff --git a/ldmudefuns/startup.py b/ldmudefuns/startup.py index 00c230a..f7564c9 100644 --- a/ldmudefuns/startup.py +++ b/ldmudefuns/startup.py @@ -1,4 +1,4 @@ -from .internal import get_config +from .internal import get_registration_types def startup(): """ Loads all registered packages that offer the ldmud_efun entry point. @@ -11,24 +11,15 @@ def startup(): """ import traceback - import ldmud try: import importlib.metadata as metadata except ModuleNotFoundError: import importlib_metadata as metadata - config = get_config() - - ep_types = [] - if hasattr(ldmud, 'register_type'): - ep_types.append(('ldmud_type', 'type', config['types'], ldmud.register_type,)) - if hasattr(ldmud, 'register_efun'): - ep_types.append(('ldmud_efun', 'efun', config['efuns'], ldmud.register_efun,)) - eps = metadata.entry_points() - for ep_name, ep_desc, ep_config, ep_register in ep_types: + for ep_name, ep_desc, ep_config, ep_register in get_registration_types(): for entry_point in eps.get(ep_name,()): if ep_config.getboolean(entry_point.name, True): try: From 9f81b43c5f733fcb44efc9d1c64f6cbcbfb92b35 Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Mon, 10 Feb 2025 13:19:43 -0500 Subject: [PATCH 4/6] lift out entrypoints helper, metadata import This centralizes the handling of importing the correct `importlib` metadata, and the logic for finding the entry points for a specific entrypoint group name. --- ldmudefuns/help.py | 10 ++-------- ldmudefuns/internal.py | 8 ++++++++ ldmudefuns/reload.py | 12 +++--------- ldmudefuns/startup.py | 11 ++--------- 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/ldmudefuns/help.py b/ldmudefuns/help.py index bb403e0..8e7bda8 100644 --- a/ldmudefuns/help.py +++ b/ldmudefuns/help.py @@ -1,6 +1,6 @@ import ldmud -from .internal import get_config +from .internal import get_config, get_entry_points def format_docstring(efun) -> str: doc = getattr(efun, '__doc__', None) @@ -49,11 +49,6 @@ def python_efun_help(efunname: str) -> str: return format_docstring(efun) else: - try: - import importlib.metadata as metadata - except ModuleNotFoundError: - import importlib_metadata as metadata - def python_efun_help(efunname: str) -> str: """ SYNOPSIS @@ -70,8 +65,7 @@ def python_efun_help(efunname: str) -> str: if not efunconfig.getboolean(efunname, True): return None - eps = metadata.entry_points() - for entry_point in eps.get('ldmud_efun', ()): + for entry_point in get_entry_points('ldmud_efun'): if entry_point.name != efunname: continue diff --git a/ldmudefuns/internal.py b/ldmudefuns/internal.py index 02c14b9..f96a4f0 100644 --- a/ldmudefuns/internal.py +++ b/ldmudefuns/internal.py @@ -2,6 +2,11 @@ import configparser import ldmud +try: + import importlib.metadata as metadata +except ModuleNotFoundError: + import importlib_metadata as metadata + def get_config(): config = configparser.ConfigParser() config['efuns'] = {} @@ -17,3 +22,6 @@ def get_registration_types(): if hasattr(ldmud, 'register_efun'): ep_types.append(('ldmud_efun', 'efun', config['efuns'], ldmud.register_efun)) return ep_types + +def get_entry_points(group_name): + return metadata.entry_points().get(group_name, ()) diff --git a/ldmudefuns/reload.py b/ldmudefuns/reload.py index ad042df..a6ac3e6 100644 --- a/ldmudefuns/reload.py +++ b/ldmudefuns/reload.py @@ -1,12 +1,7 @@ import importlib, sys import ldmud -try: - import importlib.metadata as metadata -except ModuleNotFoundError: - import importlib_metadata as metadata - -from .internal import get_registration_types +from .internal import get_registration_types, get_entry_points, metadata def reload_modules(): """ @@ -26,11 +21,10 @@ def reload_modules(): importlib.reload(metadata) modules = dict(sys.modules) reloaded = set() - eps = metadata.entry_points() ep_types = get_registration_types() for ep_name, ep_desc, ep_config, ep_register in ep_types: - for entry_point in eps.get(ep_name,()): + for entry_point in get_entry_points(ep_name): if ep_config.getboolean(entry_point.name, True): # Remove the corresponding modules from sys.modules # so they will be reloaded. @@ -49,7 +43,7 @@ def reload_modules(): print("Reload module", module) for ep_name, ep_desc, ep_config, ep_register in ep_types: - for entry_point in eps.get(ep_name,()): + for entry_point in get_entry_points(ep_name): if ep_config.getboolean(entry_point.name, True): try: print("Re-registering Python", ep_desc, entry_point.name) diff --git a/ldmudefuns/startup.py b/ldmudefuns/startup.py index f7564c9..20c7ffb 100644 --- a/ldmudefuns/startup.py +++ b/ldmudefuns/startup.py @@ -1,4 +1,4 @@ -from .internal import get_registration_types +from .internal import get_registration_types, get_entry_points def startup(): """ Loads all registered packages that offer the ldmud_efun entry point. @@ -12,15 +12,8 @@ def startup(): import traceback - try: - import importlib.metadata as metadata - except ModuleNotFoundError: - import importlib_metadata as metadata - - eps = metadata.entry_points() - for ep_name, ep_desc, ep_config, ep_register in get_registration_types(): - for entry_point in eps.get(ep_name,()): + for entry_point in get_entry_points(ep_name): if ep_config.getboolean(entry_point.name, True): try: print("Registering Python", ep_desc, entry_point.name) From f117bf91518549af0f47842a2dc04e7522aa2ec6 Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Mon, 10 Feb 2025 13:20:23 -0500 Subject: [PATCH 5/6] fix compatibility w/ python 3.12 & modern importlib MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Quoting the upstream docs: > Changed in version 3.12: The “selectable” entry points were introduced > in importlib_metadata 3.6 and Python 3.10. Prior to those changes, > entry_points accepted no parameters and always returned a dictionary of > entry points, keyed by group. With importlib_metadata 5.0 and Python > 3.12, entry_points always returns an EntryPoints object. Rather than try to gate the updated logic on both the `importlib` version and the Python version we instead update `get_entry_points()` to check whether it has a `dict` or not after calling `entry_points()` and act accordingly. --- ldmudefuns/internal.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ldmudefuns/internal.py b/ldmudefuns/internal.py index f96a4f0..18f8f24 100644 --- a/ldmudefuns/internal.py +++ b/ldmudefuns/internal.py @@ -24,4 +24,14 @@ def get_registration_types(): return ep_types def get_entry_points(group_name): - return metadata.entry_points().get(group_name, ()) + eps = metadata.entry_points() + + # Prior to importlib_metadata 5.0 and Python 3.12 entry_points() + # returned a dictionary of entry points keyed to group. + if isinstance(eps, dict): + return eps.get(group_name, ()) + + # For modern versions of importlib_metadata and Python 3.12 + # entry_points() returns an EntryPoints object that can be + # queried with select(). + return eps.select(group=group_name) From 55fb7ea96bfcc0a0be95ba9ba19fce93ab37f2cb Mon Sep 17 00:00:00 2001 From: Alexander Motzkau Date: Mon, 10 Feb 2025 19:54:23 +0100 Subject: [PATCH 6/6] Update version to 0.2.4 for Python 3.12 support release --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 26ac81e..76762fa 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="ldmud-efuns", - version="0.2.3", + version="0.2.4", author="LDMud Team", author_email="ldmud-dev@UNItopia.DE", description="Python Efun management package for LDMud",