From 6689fd253f7bd9ef3ca54011da6d15827ef7077c Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Thu, 12 Aug 2021 10:00:45 +0200 Subject: [PATCH] cache `eps()` --- aiida/plugins/entry_point.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/aiida/plugins/entry_point.py b/aiida/plugins/entry_point.py index 5b7c22abc9..7591531b4d 100644 --- a/aiida/plugins/entry_point.py +++ b/aiida/plugins/entry_point.py @@ -17,7 +17,7 @@ # but was then updated in python 3.10 to use an improved API. # So for now we use the backport importlib_metadata package. from importlib_metadata import EntryPoint, EntryPoints -from importlib_metadata import entry_points as eps +from importlib_metadata import entry_points as _eps from aiida.common.exceptions import MissingEntryPointError, MultipleEntryPointError, LoadingEntryPointError @@ -27,6 +27,11 @@ ENTRY_POINT_STRING_SEPARATOR = ':' +@functools.lru_cache(maxsize=1) +def eps(): + return _eps() + + class EntryPointFormat(enum.Enum): """ Enum to distinguish between the various possible entry point string formats. An entry point string @@ -194,7 +199,6 @@ def load_entry_point_from_string(entry_point_string: str) -> Any: return load_entry_point(group, name) -@functools.lru_cache(maxsize=100) def load_entry_point(group: str, name: str) -> Any: """ Load the class registered under the entry point for a given name and group @@ -218,7 +222,6 @@ def load_entry_point(group: str, name: str) -> Any: return loaded_entry_point -@functools.lru_cache(maxsize=None) def get_entry_point_groups() -> Set[str]: """ Return a list of all the recognized entry point groups @@ -237,7 +240,6 @@ def get_entry_point_names(group: str, sort: bool = True) -> List[str]: return group_names -@functools.lru_cache(maxsize=None) def get_entry_points(group: str) -> EntryPoints: """ Return a list of all the entry points within a specific group @@ -248,7 +250,6 @@ def get_entry_points(group: str) -> EntryPoints: return eps().select(group=group) -@functools.lru_cache(maxsize=None) def get_entry_point(group: str, name: str) -> EntryPoint: """ Return an entry point with a given name within a specific group @@ -267,6 +268,7 @@ def get_entry_point(group: str, name: str) -> EntryPoint: return found[name] +@functools.lru_cache(maxsize=100) def get_entry_point_from_class(class_module: str, class_name: str) -> Tuple[Optional[str], Optional[EntryPoint]]: """ Given the module and name of a class, attempt to obtain the corresponding entry point if it exists @@ -330,7 +332,7 @@ def is_valid_entry_point_string(entry_point_string: str) -> bool: return group in ENTRY_POINT_GROUP_TO_MODULE_PATH_MAP -@functools.lru_cache(maxsize=None) +@functools.lru_cache(maxsize=100) def is_registered_entry_point(class_module: str, class_name: str, groups: Optional[Sequence[str]] = None) -> bool: """Verify whether the class with the given module and class name is a registered entry point.