mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2024-12-02 13:29:11 -06:00
e27e39a8f3
server connection. The BaseDriver and BaseConnection are two abstract classes, which allows us to replace the existing driver with the currently used. The current implementation supports to connect the PostgreSQL and Postgres Plus Advanced Server using the psycopg2 driver.
85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
##########################################################################
|
|
#
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
#
|
|
# Copyright (C) 2013 - 2015, The pgAdmin Development Team
|
|
# This software is released under the PostgreSQL Licence
|
|
#
|
|
##########################################################################
|
|
from abc import ABCMeta
|
|
from flask.ext.babel import gettext
|
|
|
|
|
|
def _decorate_cls_name(module_name):
|
|
l = len(__package__) + 1
|
|
|
|
if len(module_name) > l and module_name.startswith(__package__):
|
|
return module_name[l:]
|
|
|
|
return module_name
|
|
|
|
|
|
class DriverRegistry(ABCMeta):
|
|
"""
|
|
class DriverRegistry(object)
|
|
Every Driver will be registered automatically by its module name.
|
|
|
|
This uses factory pattern to genreate driver object based on its name
|
|
automatically.
|
|
|
|
Class-level Methods:
|
|
----------- -------
|
|
* __init__(...)
|
|
- It will be used to register type of drivers. You don't need to call
|
|
this function explicitly. This will be automatically executed, whenever
|
|
we create class and inherit from BaseDriver, it will register it as
|
|
available driver in DriverRegistry. Because - the __metaclass__ for
|
|
BaseDriver is set it to DriverRegistry, and it will create new instance
|
|
of this DriverRegistry per class.
|
|
|
|
* create(type, *args, **kwargs)
|
|
- Create type of driver object for this server, from the available
|
|
driver list (if available, or raise exception).
|
|
|
|
* load_drivers():
|
|
- Use this function from init_app(...) to load all available drivers in
|
|
the registry.
|
|
"""
|
|
registry = dict()
|
|
drivers = dict()
|
|
|
|
def __init__(cls, name, bases, d):
|
|
|
|
# Register this type of driver, based on the module name
|
|
# Avoid registering the BaseDriver itself
|
|
|
|
if name != 'BaseDriver':
|
|
DriverRegistry.registry[_decorate_cls_name(d['__module__'])] = cls
|
|
|
|
ABCMeta.__init__(cls, name, bases, d)
|
|
|
|
@classmethod
|
|
def create(cls, name, **kwargs):
|
|
|
|
if name in DriverRegistry.drivers:
|
|
return DriverRegistry.drivers[name]
|
|
|
|
if name in DriverRegistry.registry:
|
|
DriverRegistry.drivers[name] = \
|
|
(DriverRegistry.registry[name])(**kwargs)
|
|
return DriverRegistry.drivers[name]
|
|
|
|
raise NotImplementedError(
|
|
gettext("Driver - '{0}' has not been implemented!").format(name)
|
|
)
|
|
|
|
@classmethod
|
|
def load_drivers(cls):
|
|
DriverRegistry.registry = dict()
|
|
|
|
from importlib import import_module
|
|
from werkzeug.utils import find_modules
|
|
|
|
for module_name in find_modules(__package__, True):
|
|
module = import_module(module_name)
|