summaryrefslogtreecommitdiffstats
path: root/pykolab/auth/ldap
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2019-10-25 13:16:55 +0200
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2019-10-25 13:16:55 +0200
commit28d9e703ea31a070a05589a2bd82b79bc7e6bed4 (patch)
treee7967f619425b94109c6c046075025a6f98aea0f /pykolab/auth/ldap
parent334366d52a91de1309df5fc0ce89a57d9b23d5b1 (diff)
downloadpykolab-28d9e703ea31a070a05589a2bd82b79bc7e6bed4.tar.gz
More linting and syntax issues resolved
Diffstat (limited to 'pykolab/auth/ldap')
-rw-r--r--pykolab/auth/ldap/__init__.py30
-rw-r--r--pykolab/auth/ldap/auth_cache.py88
-rw-r--r--pykolab/auth/ldap/cache.py147
3 files changed, 141 insertions, 124 deletions
diff --git a/pykolab/auth/ldap/__init__.py b/pykolab/auth/ldap/__init__.py
index df35cd1..c414118 100644
--- a/pykolab/auth/ldap/__init__.py
+++ b/pykolab/auth/ldap/__init__.py
@@ -20,14 +20,16 @@ from __future__ import print_function
import datetime
# Catch python-ldap-2.4 changes
from distutils import version
-import _ldap
-import ldap
-import ldap.controls
-import ldap.filter
import logging
import time
import traceback
+import ldap
+import ldap.controls
+import ldap.filter
+
+import _ldap
+
import pykolab
import pykolab.base
@@ -39,13 +41,18 @@ from pykolab.translate import _
import auth_cache
import cache
+# pylint: disable=invalid-name
log = pykolab.getLogger('pykolab.auth')
conf = pykolab.getConf()
if version.StrictVersion('2.4.0') <= version.StrictVersion(ldap.__version__):
LDAP_CONTROL_PAGED_RESULTS = ldap.CONTROL_PAGEDRESULTS
else:
- LDAP_CONTROL_PAGED_RESULTS = ldap.LDAP_CONTROL_PAGE_OID
+ if hasattr(ldap, 'LDAP_CONTROL_PAGE_OID'):
+ # pylint: disable=no-member
+ LDAP_CONTROL_PAGED_RESULTS = ldap.LDAP_CONTROL_PAGE_OID
+ else:
+ LDAP_CONTROL_PAGED_RESULTS = ldap.controls.SimplePagedResultsControl.controlType
try:
from ldap.controls import psearch
@@ -80,8 +87,8 @@ class SimplePagedResultsControl(ldap.controls.SimplePagedResultsControl):
def cookie(self):
if version.StrictVersion('2.4.0') <= version.StrictVersion(ldap.__version__):
return self.cookie
- else:
- return self.controlValue[1]
+
+ return self.controlValue[1]
def size(self):
if version.StrictVersion('2.4.0') <= version.StrictVersion(ldap.__version__):
@@ -237,7 +244,7 @@ class LDAP(pykolab.base.Base):
except ldap.SERVER_DOWN as errmsg:
log.error(_("LDAP server unavailable: %r") % (errmsg))
- log.error(_("%s") % (traceback.format_exc()))
+ log.error(traceback.format_exc())
self._disconnect()
return False
@@ -250,7 +257,7 @@ class LDAP(pykolab.base.Base):
except Exception as errmsg:
log.error(_("Exception occurred: %r") % (errmsg))
- log.error(_("%s") % (traceback.format_exc()))
+ log.error(traceback.format_exc())
self._disconnect()
return False
@@ -1336,7 +1343,7 @@ class LDAP(pykolab.base.Base):
)
import traceback
- log.error("%s" % (traceback.format_exc()))
+ log.error(traceback.format_exc())
def synchronize(self, mode=0, callback=None):
"""
@@ -3063,8 +3070,7 @@ class LDAP(pykolab.base.Base):
scope=scope,
filterstr=filterstr,
attrlist=attrlist,
- attrsonly=attrsonly,
- timeout=timeout
+ attrsonly=attrsonly
)
_results = []
diff --git a/pykolab/auth/ldap/auth_cache.py b/pykolab/auth/ldap/auth_cache.py
index 781f7b9..af65e40 100644
--- a/pykolab/auth/ldap/auth_cache.py
+++ b/pykolab/auth/ldap/auth_cache.py
@@ -25,30 +25,18 @@ from sqlalchemy import DateTime
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import String
-from sqlalchemy import Table
from sqlalchemy import Text
-from sqlalchemy import desc
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy.orm import mapper
-try:
- from sqlalchemy.orm import relationship
-except:
- from sqlalchemy.orm import relation as relationship
-
-try:
- from sqlalchemy.orm import sessionmaker
-except:
- from sqlalchemy.orm import create_session
+from sqlalchemy.orm import sessionmaker
import pykolab
-from pykolab import utils
from pykolab.constants import KOLAB_LIB_PATH
-from pykolab.translate import _
+# pylint: disable=invalid-name
conf = pykolab.getConf()
log = pykolab.getLogger('pykolab.auth_cache')
@@ -56,12 +44,19 @@ metadata = MetaData()
db = None
-##
-## Classes
-##
+try:
+ unicode('')
+except NameError:
+ unicode = str
+
+#
+# Classes
+#
DeclarativeBase = declarative_base()
+
+# pylint: disable=too-few-public-methods
class Entry(DeclarativeBase):
__tablename__ = 'entries'
@@ -78,38 +73,47 @@ class Entry(DeclarativeBase):
else:
self.value = value
-##
-## Functions
-##
+#
+# Functions
+#
+
def del_entry(key):
+ # pylint: disable=global-statement
+ global db
+
db = init_db()
try:
- _entries = db.query(Entry).filter_by(key=key).delete()
- except sqlalchemy.exc.OperationalError, errmsg:
+ db.query(Entry).filter_by(key=key).delete()
+ except sqlalchemy.exc.OperationalError:
db = init_db(reinit=True)
- except sqlalchemy.exc.InvalidRequest, errmsg:
+ except sqlalchemy.exc.InvalidRequest:
db = init_db(reinit=True)
finally:
- _entries = db.query(Entry).filter_by(key=key).delete()
+ db.query(Entry).filter_by(key=key).delete()
db.commit()
+
def get_entry(key):
+ # pylint: disable=global-statement
+ global db
+
db = init_db()
try:
_entries = db.query(Entry).filter_by(key=key).all()
- except sqlalchemy.exc.OperationalError, errmsg:
+ except sqlalchemy.exc.OperationalError:
db = init_db(reinit=True)
- except sqlalchemy.exc.InvalidRequest, errmsg:
+ except sqlalchemy.exc.InvalidRequest:
db = init_db(reinit=True)
finally:
_entries = db.query(Entry).filter_by(key=key).all()
- if len(_entries) == 0:
+ if _entries:
return None
+
if len(_entries) > 1:
return None
@@ -118,26 +122,23 @@ def get_entry(key):
return _entries[0].value.encode('utf-8', 'latin1')
+
def set_entry(key, value):
db = init_db()
+
try:
_entries = db.query(Entry).filter_by(key=key).all()
- except sqlalchemy.exc.OperationalError, errmsg:
+ except sqlalchemy.exc.OperationalError:
db = init_db(reinit=True)
- except sqlalchemy.exc.InvalidRequest, errmsg:
+ except sqlalchemy.exc.InvalidRequest:
db = init_db(reinit=True)
finally:
_entries = db.query(Entry).filter_by(key=key).all()
- if len(_entries) == 0:
- db.add(
- Entry(
- key,
- value
- )
- )
-
+ if not _entries:
+ db.add(Entry(key, value))
db.commit()
+
elif len(_entries) == 1:
if not isinstance(value, unicode):
value = unicode(value, 'utf-8')
@@ -148,21 +149,28 @@ def set_entry(key, value):
_entries[0].last_change = datetime.datetime.now()
db.commit()
+
def purge_entries(db):
- db.query(Entry).filter(Entry.last_change <= (datetime.datetime.now() - datetime.timedelta(1))).delete()
+ db.query(Entry).filter(
+ Entry.last_change <= (datetime.datetime.now() - datetime.timedelta(1))
+ ).delete()
+
db.commit()
+
def init_db(reinit=False):
"""
Returns a SQLAlchemy Session() instance.
"""
+ # pylint: disable=global-statement
global db
- if not db == None and not reinit:
+ if db is not None and not reinit:
return db
db_uri = conf.get('ldap', 'auth_cache_uri')
- if db_uri == None:
+
+ if db_uri is None:
db_uri = 'sqlite:///%s/auth_cache.db' % (KOLAB_LIB_PATH)
if reinit:
diff --git a/pykolab/auth/ldap/cache.py b/pykolab/auth/ldap/cache.py
index 23b4e07..02da350 100644
--- a/pykolab/auth/ldap/cache.py
+++ b/pykolab/auth/ldap/cache.py
@@ -18,6 +18,8 @@
import datetime
+from uuid import UUID
+
import sqlalchemy
from sqlalchemy import Column
@@ -31,24 +33,14 @@ from sqlalchemy import desc
from sqlalchemy import create_engine
from sqlalchemy.orm import mapper
-from uuid import UUID
-
-try:
- from sqlalchemy.orm import relationship
-except:
- from sqlalchemy.orm import relation as relationship
-
-try:
- from sqlalchemy.orm import sessionmaker
-except:
- from sqlalchemy.orm import create_session
+from sqlalchemy.orm import sessionmaker
import pykolab
-from pykolab import utils
from pykolab.constants import KOLAB_LIB_PATH
from pykolab.translate import _
+# pylint: disable=invalid-name
conf = pykolab.getConf()
log = pykolab.getLogger('pykolab.auth_cache')
@@ -56,11 +48,15 @@ metadata = MetaData()
db = {}
-##
-## Classes
-##
+#
+# Classes
+#
+
+
+# pylint: disable=too-few-public-methods
+class Entry:
+ last_change = None
-class Entry(object):
def __init__(self, uniqueid, result_attr, last_change):
self.uniqueid = uniqueid
self.result_attribute = result_attr
@@ -72,83 +68,82 @@ class Entry(object):
).replace('%%', '%')
self.last_change = datetime.datetime.strptime(
- last_change,
+ last_change,
modifytimestamp_format
)
-##
-## Tables
-##
+#
+# Tables
+#
+
entry_table = Table(
- 'entry', metadata,
- Column('id', Integer, primary_key=True),
- Column('uniqueid', String(128), nullable=False),
- Column('result_attribute', String(128), nullable=False),
- Column('last_change', DateTime),
- )
+ 'entry', metadata,
+ Column('id', Integer, primary_key=True),
+ Column('uniqueid', String(128), nullable=False),
+ Column('result_attribute', String(128), nullable=False),
+ Column('last_change', DateTime),
+)
-##
-## Table <-> Class Mappers
-##
+#
+# Table <-> Class Mappers
+#
mapper(Entry, entry_table)
-##
-## Functions
-##
+#
+# Functions
+#
+
def delete_entry(domain, entry):
- result_attribute = conf.get_raw('cyrus-sasl', 'result_attribute')
+ _db = init_db(domain)
+ _entry = _db.query(Entry).filter_by(uniqueid=entry['id']).first()
- db = init_db(domain)
- _entry = db.query(Entry).filter_by(uniqueid=entry['id']).first()
+ if _entry is not None:
+ _db.delete(_entry)
+ _db.commit()
- if not _entry == None:
- db.delete(_entry)
- db.commit()
def get_entry(domain, entry, update=True):
result_attribute = conf.get_raw('cyrus-sasl', 'result_attribute')
_entry = None
- db = init_db(domain)
+ _db = init_db(domain)
try:
_uniqueid = str(UUID(bytes_le=entry['id']))
- log.debug(_("Entry uniqueid was converted from binary form to string: %s") % _uniqueid, level=8)
+ log.debug(
+ _("Entry uniqueid was converted from binary form to string: %s") % _uniqueid,
+ level=8
+ )
+
except ValueError:
_uniqueid = entry['id']
try:
- _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()
- except sqlalchemy.exc.OperationalError, errmsg:
- db = init_db(domain,reinit=True)
- except sqlalchemy.exc.InvalidRequestError, errmsg:
- db = init_db(domain,reinit=True)
+ _entry = _db.query(Entry).filter_by(uniqueid=_uniqueid).first()
+ except sqlalchemy.exc.OperationalError:
+ _db = init_db(domain, reinit=True)
+ except sqlalchemy.exc.InvalidRequestError:
+ _db = init_db(domain, reinit=True)
finally:
- _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()
+ _entry = _db.query(Entry).filter_by(uniqueid=_uniqueid).first()
if not update:
return _entry
- if _entry == None:
+ if _entry is None:
log.debug(_("Inserting cache entry %r") % (_uniqueid), level=8)
- if not entry.has_key(result_attribute):
+ if result_attribute not in entry:
entry[result_attribute] = ''
- db.add(
- Entry(
- _uniqueid,
- entry[result_attribute],
- entry['modifytimestamp']
- )
- )
+ _db.add(Entry(_uniqueid, entry[result_attribute], entry['modifytimestamp']))
- db.commit()
- _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()
+ _db.commit()
+ _entry = _db.query(Entry).filter_by(uniqueid=_uniqueid).first()
else:
modifytimestamp_format = conf.get_raw(
'ldap',
@@ -158,24 +153,30 @@ def get_entry(domain, entry, update=True):
if not _entry.last_change.strftime(modifytimestamp_format) == entry['modifytimestamp']:
log.debug(_("Updating timestamp for cache entry %r") % (_uniqueid), level=8)
- last_change = datetime.datetime.strptime(entry['modifytimestamp'], modifytimestamp_format)
+ last_change = datetime.datetime.strptime(
+ entry['modifytimestamp'],
+ modifytimestamp_format
+ )
+
_entry.last_change = last_change
- db.commit()
- _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()
+ _db.commit()
+ _entry = _db.query(Entry).filter_by(uniqueid=_uniqueid).first()
- if entry.has_key(result_attribute):
+ if result_attribute in entry:
if not _entry.result_attribute == entry[result_attribute]:
log.debug(_("Updating result_attribute for cache entry %r") % (_uniqueid), level=8)
_entry.result_attribute = entry[result_attribute]
- db.commit()
- _entry = db.query(Entry).filter_by(uniqueid=_uniqueid).first()
+ _db.commit()
+ _entry = _db.query(Entry).filter_by(uniqueid=_uniqueid).first()
return _entry
-def init_db(domain,reinit=False):
+
+def init_db(domain, reinit=False):
"""
Returns a SQLAlchemy Session() instance.
"""
+ # pylint: disable=global-statement
global db
if domain in db and not reinit:
@@ -192,7 +193,7 @@ def init_db(domain,reinit=False):
try:
engine = create_engine(db_uri, echo=echo)
metadata.create_all(engine)
- except:
+ except Exception:
engine = create_engine('sqlite://')
metadata.create_all(engine)
@@ -201,6 +202,7 @@ def init_db(domain,reinit=False):
return db[domain]
+
def last_modify_timestamp(domain):
modifytimestamp_format = conf.get_raw(
'ldap',
@@ -209,12 +211,13 @@ def last_modify_timestamp(domain):
).replace('%%', '%')
try:
- db = init_db(domain)
- last_change = db.query(Entry).order_by(desc(Entry.last_change)).first()
+ _db = init_db(domain)
+ last_change = _db.query(Entry).order_by(desc(Entry.last_change)).first()
- if not last_change == None:
+ if last_change is not None:
return last_change.last_change.strftime(modifytimestamp_format)
- else:
- return datetime.datetime(1900, 01, 01, 00, 00, 00).strftime(modifytimestamp_format)
- except:
- return datetime.datetime(1900, 01, 01, 00, 00, 00).strftime(modifytimestamp_format)
+
+ return datetime.datetime(1900, 1, 1, 00, 00, 00).strftime(modifytimestamp_format)
+
+ except Exception:
+ return datetime.datetime(1900, 1, 1, 00, 00, 00).strftime(modifytimestamp_format)