Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. """
  2. ldap - base module
  3. See https://www.python-ldap.org/ for details.
  4. """
  5. # This is also the overall release version number
  6. from ldap.pkginfo import __version__, __author__, __license__
  7. import os
  8. import sys
  9. if __debug__:
  10. # Tracing is only supported in debugging mode
  11. import atexit
  12. import traceback
  13. _trace_level = int(os.environ.get("PYTHON_LDAP_TRACE_LEVEL", 0))
  14. _trace_file = os.environ.get("PYTHON_LDAP_TRACE_FILE")
  15. if _trace_file is None:
  16. _trace_file = sys.stderr
  17. else:
  18. _trace_file = open(_trace_file, 'a')
  19. atexit.register(_trace_file.close)
  20. _trace_stack_limit = None
  21. import _ldap
  22. assert _ldap.__version__==__version__, \
  23. ImportError('ldap %s and _ldap %s version mismatch!' % (__version__,_ldap.__version__))
  24. from _ldap import *
  25. # call into libldap to initialize it right now
  26. LIBLDAP_API_INFO = _ldap.get_option(_ldap.OPT_API_INFO)
  27. OPT_NAMES_DICT = {}
  28. for k,v in vars(_ldap).items():
  29. if k.startswith('OPT_'):
  30. OPT_NAMES_DICT[v]=k
  31. class DummyLock:
  32. """Define dummy class with methods compatible to threading.Lock"""
  33. def __init__(self):
  34. pass
  35. def acquire(self):
  36. pass
  37. def release(self):
  38. pass
  39. try:
  40. # Check if Python installation was build with thread support
  41. import thread
  42. except ImportError:
  43. LDAPLockBaseClass = DummyLock
  44. else:
  45. import threading
  46. LDAPLockBaseClass = threading.Lock
  47. class LDAPLock:
  48. """
  49. Mainly a wrapper class to log all locking events.
  50. Note that this cumbersome approach with _lock attribute was taken
  51. since threading.Lock is not suitable for sub-classing.
  52. """
  53. _min_trace_level = 3
  54. def __init__(self,lock_class=None,desc=''):
  55. """
  56. lock_class
  57. Class compatible to threading.Lock
  58. desc
  59. Description shown in debug log messages
  60. """
  61. self._desc = desc
  62. self._lock = (lock_class or LDAPLockBaseClass)()
  63. def acquire(self):
  64. if __debug__:
  65. global _trace_level
  66. if _trace_level>=self._min_trace_level:
  67. _trace_file.write('***%s.acquire() %s %s\n' % (self.__class__.__name__,repr(self),self._desc))
  68. return self._lock.acquire()
  69. def release(self):
  70. if __debug__:
  71. global _trace_level
  72. if _trace_level>=self._min_trace_level:
  73. _trace_file.write('***%s.release() %s %s\n' % (self.__class__.__name__,repr(self),self._desc))
  74. return self._lock.release()
  75. # Create module-wide lock for serializing all calls into underlying LDAP lib
  76. _ldap_module_lock = LDAPLock(desc='Module wide')
  77. from ldap.functions import initialize,get_option,set_option,escape_str,strf_secs,strp_secs
  78. from ldap.ldapobject import NO_UNIQUE_ENTRY, LDAPBytesWarning
  79. from ldap.dn import explode_dn,explode_rdn,str2dn,dn2str
  80. del str2dn
  81. del dn2str
  82. # More constants
  83. # For compatibility of 2.3 and 2.4 OpenLDAP API
  84. OPT_DIAGNOSTIC_MESSAGE = OPT_ERROR_STRING