mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Refactor the registry class logic and remove duplicate code for the same functionality.
This commit is contained in:
committed by
Akshay Joshi
parent
a53be65ab0
commit
9574f43f87
@@ -223,7 +223,7 @@ def get_auth_sources(type):
|
||||
if type in auth_sources:
|
||||
return auth_sources[type]
|
||||
|
||||
auth_source = AuthSourceRegistry.create(type)
|
||||
auth_source = AuthSourceRegistry.get(type)
|
||||
|
||||
if auth_source is not None:
|
||||
auth_sources[type] = auth_source
|
||||
@@ -236,7 +236,7 @@ def init_app(app):
|
||||
auth_sources = dict()
|
||||
|
||||
setattr(app, '_pgadmin_auth_sources', auth_sources)
|
||||
AuthSourceRegistry.load_auth_sources()
|
||||
AuthSourceRegistry.load_modules(app)
|
||||
|
||||
return auth_sources
|
||||
|
||||
|
||||
@@ -10,56 +10,9 @@
|
||||
"""External Authentication Registry."""
|
||||
|
||||
|
||||
from flask_babelex import gettext
|
||||
from abc import ABCMeta
|
||||
from pgadmin.utils.dynamic_registry import create_registry_metaclass
|
||||
|
||||
|
||||
def _decorate_cls_name(module_name):
|
||||
length = len(__package__) + 1
|
||||
|
||||
if len(module_name) > length and module_name.startswith(__package__):
|
||||
return module_name[length:]
|
||||
|
||||
return module_name
|
||||
|
||||
|
||||
class AuthSourceRegistry(ABCMeta):
|
||||
registry = None
|
||||
auth_sources = dict()
|
||||
|
||||
def __init__(self, name, bases, d):
|
||||
|
||||
# Register this type of auth_sources, based on the module name
|
||||
# Avoid registering the BaseAuthentication itself
|
||||
|
||||
AuthSourceRegistry.registry[_decorate_cls_name(d['__module__'])] = self
|
||||
ABCMeta.__init__(self, name, bases, d)
|
||||
|
||||
@classmethod
|
||||
def create(cls, name, **kwargs):
|
||||
|
||||
if name in AuthSourceRegistry.auth_sources:
|
||||
return AuthSourceRegistry.auth_sources[name]
|
||||
|
||||
if name in AuthSourceRegistry.registry:
|
||||
AuthSourceRegistry.auth_sources[name] = \
|
||||
(AuthSourceRegistry.registry[name])(**kwargs)
|
||||
return AuthSourceRegistry.auth_sources[name]
|
||||
|
||||
raise NotImplementedError(
|
||||
gettext(
|
||||
"Authentication source '{0}' has not been implemented."
|
||||
).format(name)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def load_auth_sources(cls):
|
||||
# Initialize the registry only if it has not yet been initialized
|
||||
if AuthSourceRegistry.registry is None:
|
||||
AuthSourceRegistry.registry = dict()
|
||||
|
||||
from importlib import import_module
|
||||
from werkzeug.utils import find_modules
|
||||
|
||||
for module_name in find_modules(__package__, True):
|
||||
import_module(module_name)
|
||||
AuthSourceRegistry = create_registry_metaclass(
|
||||
"AuthSourceRegistry", __package__, decorate_as_module=True
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user