diff --git a/README.rst b/README.rst index 33ab65b..3207b45 100644 --- a/README.rst +++ b/README.rst @@ -32,6 +32,25 @@ context manager with the *connection* method:: The connector returned by *connection* is a LDAPObject, that's binded to the server. See https://pypi.org/project/python-ldap/ for details on how to use a connector. +It is possible to check the state of the pool by representing the pool as a string:: + + from ldappool import ConnectionManager + + cm = ConnectionManager('ldap://localhost', size=2) + + .. do something with cm .. + + print(cm) + +This will result in output similar to this table:: + + +--------------+-----------+----------+------------------+--------------------+------------------------------+ + | Slot (2 max) | Connected | Active | URI | Lifetime (600 max) | Bind DN | + +--------------+-----------+----------+------------------+--------------------+------------------------------+ + | 1 | connected | inactive | ldap://localhost | 0.00496101379395 | uid=tuser,dc=example,dc=test | + | 2 | connected | inactive | ldap://localhost | 0.00532603263855 | uid=tuser,dc=example,dc=test | + +--------------+-----------+----------+------------------+--------------------+------------------------------+ + ConnectionManager options ::::::::::::::::::::::::: diff --git a/ldappool/__init__.py b/ldappool/__init__.py index 348c0dd..29ca665 100644 --- a/ldappool/__init__.py +++ b/ldappool/__init__.py @@ -43,6 +43,7 @@ import time import ldap from ldap.ldapobject import ReconnectLDAPObject +from prettytable import PrettyTable import re import six from six import PY2 @@ -423,3 +424,20 @@ class ConnectionManager(object): log.debug('Failure attempting to unbind on purge; ' 'should be harmless', exc_info=True) self._pool.remove(conn) + + def __str__(self): + table = PrettyTable() + table.field_names = ['Slot (%d max)' % self.size, + 'Connected', 'Active', 'URI', + 'Lifetime (%d max)' % self.max_lifetime, + 'Bind DN'] + + with self._pool_lock: + for slot, conn in enumerate(self._pool): + table.add_row([ + slot + 1, + 'connected' if conn.connected else 'not connected', + 'active' if conn.active else 'inactive', + conn._uri, conn.get_lifetime(), conn.who]) + + return str(table) diff --git a/lower-constraints.txt b/lower-constraints.txt index 337fd29..468138a 100644 --- a/lower-constraints.txt +++ b/lower-constraints.txt @@ -17,6 +17,7 @@ openstackdocstheme==1.18.1 pbr==2.0.0 pep257==0.7.0 pep8==1.5.7 +prettytable==0.7.2 pyflakes==0.8.1 Pygments==2.2.0 python-ldap==3.0.0 diff --git a/requirements.txt b/requirements.txt index d12620f..624fd17 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. python-ldap>=3.0.0 # PSF +PrettyTable<0.8,>=0.7.2