From 6fe1a8637f80dde665c711c744caef52151ae567 Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Wed, 6 Mar 2024 17:27:38 -0600 Subject: [PATCH] Address semsimian being optional dependency --- src/oaklib/implementations/__init__.py | 57 ++++++++++++------- .../semsimian/semsimian_implementation.py | 6 +- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/oaklib/implementations/__init__.py b/src/oaklib/implementations/__init__.py index 0e972c1a6..5786a8430 100644 --- a/src/oaklib/implementations/__init__.py +++ b/src/oaklib/implementations/__init__.py @@ -41,9 +41,6 @@ OntoPortalImplementationBase, ) from oaklib.implementations.pronto.pronto_implementation import ProntoImplementation -from oaklib.implementations.semsimian.semsimian_implementation import ( - SemSimianImplementation, -) from oaklib.implementations.simpleobo.simple_obo_implementation import ( SimpleOboImplementation, ) @@ -65,6 +62,16 @@ ) from oaklib.interfaces import OntologyInterface +SEMSIMIAN_AVAILABLE = False +try: + from oaklib.implementations.semsimian.semsimian_implementation import ( + SemSimianImplementation, + ) + SEMSIMIAN_AVAILABLE = True +except ImportError: + pass + + __all__ = [ "get_implementation_resolver", # Concrete classes @@ -96,8 +103,7 @@ "UniprotImplementation", "TranslatorImplementation", "OakMetaModelImplementation", - "SemSimianImplementation", -] +] + (["SemSimianImplementation"] if SEMSIMIAN_AVAILABLE else []) @cache @@ -122,30 +128,37 @@ def get_implementation_resolver() -> ClassResolver[OntologyInterface]: :return: A ClassResolver capable of resolving an OntologyInterface implementation """ + skip_classes = { + OntoPortalImplementationBase, + BaseOlsImplementation, + } + + resolver_synonyms = { + "obolibrary": ProntoImplementation, + "prontolib": ProntoImplementation, + "simpleobo": SimpleOboImplementation, + "sqlite": SqlImplementation, + "rdflib": SparqlImplementation, + "oak": OakMetaModelImplementation, + "cx": CXImplementation, + "ndexbio": CXImplementation, + } + + if not SEMSIMIAN_AVAILABLE: + skip_classes.add('SemSimianImplementation') + resolver_synonyms['semsimian'] = SemSimianImplementation + implementation_resolver: ClassResolver[OntologyInterface] = ClassResolver.from_subclasses( OntologyInterface, suffix="Implementation", - skip={ - OntoPortalImplementationBase, - BaseOlsImplementation, - }, + skip=skip_classes, ) # if an implementation uses a shorthand name that is # different from the class name (minus Implementation), then # it should be added here - implementation_resolver.synonyms.update( - { - "obolibrary": ProntoImplementation, - "prontolib": ProntoImplementation, - "simpleobo": SimpleOboImplementation, - "sqlite": SqlImplementation, - "rdflib": SparqlImplementation, - "oak": OakMetaModelImplementation, - "cx": CXImplementation, - "ndexbio": CXImplementation, - "semsimian": SemSimianImplementation, - } - ) + + + implementation_resolver.synonyms.update(resolver_synonyms) # Plugins which want to register an implementation should use # the entrypoint group "oaklib.plugins". The name of the entry diff --git a/src/oaklib/implementations/semsimian/semsimian_implementation.py b/src/oaklib/implementations/semsimian/semsimian_implementation.py index 9b17c8a01..0cb6cc439 100644 --- a/src/oaklib/implementations/semsimian/semsimian_implementation.py +++ b/src/oaklib/implementations/semsimian/semsimian_implementation.py @@ -6,7 +6,11 @@ from dataclasses import dataclass, field from typing import ClassVar, Dict, Iterable, Iterator, List, Optional, Tuple, Union -from semsimian import Semsimian +try: + from semsimian import Semsimian +except ImportError: + raise ImportError("The 'semsimian' package is not installed. Please install it to continue.") + from oaklib.datamodels.similarity import ( BestMatch,