<commit_before># -*- coding:utf-8 -*-
#--
# Copyright (c) 2012-2014 Net-ng.
# All rights reserved.
#
# This software is licensed under the BSD License, as described in
# the file LICENSE.txt, which you should have received as part of
# this distribution.
#--
from nagare import log
try:
    import ldap
except ImportError:
    print 'INFO: python_ldap not installed'
import sys
import types


def get_class(fullpath):
    mod, classname = fullpath.split(':')
    mod = sys.modules[mod]
    return getattr(mod, classname)


def toUTF8(v):
    if isinstance(v, unicode):
        return v.encode('utf-8')
    elif isinstance(v, (types.TupleType, types.ListType)):
        return [toUTF8(e) for e in v]
    elif isinstance(v, types.DictType):
        return dict([(toUTF8(k), toUTF8(v_)) for k, v_ in v.items()])
    else:
        return v


class LDAPAuth(object):
    def __init__(self, ldap_cfg):
        ldap_cfg = toUTF8(ldap_cfg)
        self.server = "ldap://" + ldap_cfg['server']
        self.users_base_dn = ldap_cfg['users_base_dn']

    def connect(self):
        """Connect to LDAP server

        Return:
            - a server connection
        """
        return ldap.initialize(self.server)

    def get_user_dn(self, uid):
        raise NotImplementedError()

    def check_password(self, uid, password):
        """Check if the specified couple user/password is correct

        In:
           - ``uid`` -- the user id
           - ``password`` -- the user password
        Return:
            - True if password is checked
        """
        c = self.connect()
        dn = self.get_user_dn(uid)
        # Try to authenticate
        try:
            c.simple_bind_s(dn, password.encode('UTF-8'))
            return True
        except ldap.INVALID_CREDENTIALS:
            log.info("Bad credentials for DN %r" % dn)
        except ldap.SERVER_DOWN:
            log.critical("LDAP server down")
        finally:
            c.unbind()

    def get_profile(self, uid, password):
        raise NotImplementedError()

<commit_msg>replace annoying "python_ldap not installed" message by an assert<commit_after># -*- coding:utf-8 -*-
#--
# Copyright (c) 2012-2014 Net-ng.
# All rights reserved.
#
# This software is licensed under the BSD License, as described in
# the file LICENSE.txt, which you should have received as part of
# this distribution.
#--
from nagare import log
try:
    import ldap
except ImportError:
    ldap = None
import sys
import types


def get_class(fullpath):
    mod, classname = fullpath.split(':')
    mod = sys.modules[mod]
    return getattr(mod, classname)


def toUTF8(v):
    if isinstance(v, unicode):
        return v.encode('utf-8')
    elif isinstance(v, (types.TupleType, types.ListType)):
        return [toUTF8(e) for e in v]
    elif isinstance(v, types.DictType):
        return dict([(toUTF8(k), toUTF8(v_)) for k, v_ in v.items()])
    else:
        return v


class LDAPAuth(object):
    def __init__(self, ldap_cfg):
        ldap_cfg = toUTF8(ldap_cfg)
        self.server = "ldap://" + ldap_cfg['server']
        self.users_base_dn = ldap_cfg['users_base_dn']

    def connect(self):
        """Connect to LDAP server

        Return:
            - a server connection
        """
        assert ldap, 'python_ldap not installed'
        return ldap.initialize(self.server)

    def get_user_dn(self, uid):
        raise NotImplementedError()

    def check_password(self, uid, password):
        """Check if the specified couple user/password is correct

        In:
           - ``uid`` -- the user id
           - ``password`` -- the user password
        Return:
            - True if password is checked
        """
        c = self.connect()
        dn = self.get_user_dn(uid)
        # Try to authenticate
        try:
            c.simple_bind_s(dn, password.encode('UTF-8'))
            return True
        except ldap.INVALID_CREDENTIALS:
            log.info("Bad credentials for DN %r" % dn)
        except ldap.SERVER_DOWN:
            log.critical("LDAP server down")
        finally:
            c.unbind()

    def get_profile(self, uid, password):
        raise NotImplementedError()


{'fraction_non_alphanumeric': 0.11073744895508048, 'fraction_numerical': 0.007686764352630314, 'mean_word_length': 2.2839116719242902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7306291', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1261, 'n_words': 372}