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.

functions.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. functions.py - wraps functions of module _ldap
  3. See https://www.python-ldap.org/ for details.
  4. """
  5. from ldap import __version__
  6. __all__ = [
  7. 'open','initialize','init',
  8. 'explode_dn','explode_rdn',
  9. 'get_option','set_option',
  10. 'escape_str',
  11. 'strf_secs','strp_secs',
  12. ]
  13. import sys,pprint,time,_ldap,ldap
  14. from calendar import timegm
  15. from ldap import LDAPError
  16. from ldap.dn import explode_dn,explode_rdn
  17. from ldap.ldapobject import LDAPObject
  18. if __debug__:
  19. # Tracing is only supported in debugging mode
  20. import traceback
  21. # See _raise_byteswarning in ldapobject.py
  22. _LDAP_WARN_SKIP_FRAME = True
  23. def _ldap_function_call(lock,func,*args,**kwargs):
  24. """
  25. Wrapper function which locks and logs calls to function
  26. lock
  27. Instance of threading.Lock or compatible
  28. func
  29. Function to call with arguments passed in via *args and **kwargs
  30. """
  31. if lock:
  32. lock.acquire()
  33. if __debug__:
  34. if ldap._trace_level>=1:
  35. ldap._trace_file.write('*** %s.%s %s\n' % (
  36. '_ldap',func.__name__,
  37. pprint.pformat((args,kwargs))
  38. ))
  39. if ldap._trace_level>=9:
  40. traceback.print_stack(limit=ldap._trace_stack_limit,file=ldap._trace_file)
  41. try:
  42. try:
  43. result = func(*args,**kwargs)
  44. finally:
  45. if lock:
  46. lock.release()
  47. except LDAPError as e:
  48. if __debug__ and ldap._trace_level>=2:
  49. ldap._trace_file.write('=> LDAPError: %s\n' % (str(e)))
  50. raise
  51. if __debug__ and ldap._trace_level>=2:
  52. ldap._trace_file.write('=> result:\n%s\n' % (pprint.pformat(result)))
  53. return result
  54. def initialize(uri,trace_level=0,trace_file=sys.stdout,trace_stack_limit=None, bytes_mode=None):
  55. """
  56. Return LDAPObject instance by opening LDAP connection to
  57. LDAP host specified by LDAP URL
  58. Parameters:
  59. uri
  60. LDAP URL containing at least connection scheme and hostport,
  61. e.g. ldap://localhost:389
  62. trace_level
  63. If non-zero a trace output of LDAP calls is generated.
  64. trace_file
  65. File object where to write the trace output to.
  66. Default is to use stdout.
  67. bytes_mode
  68. Whether to enable :ref:`bytes_mode` for backwards compatibility under Py2.
  69. """
  70. return LDAPObject(uri,trace_level,trace_file,trace_stack_limit,bytes_mode)
  71. def get_option(option):
  72. """
  73. get_option(name) -> value
  74. Get the value of an LDAP global option.
  75. """
  76. return _ldap_function_call(None,_ldap.get_option,option)
  77. def set_option(option,invalue):
  78. """
  79. set_option(name, value)
  80. Set the value of an LDAP global option.
  81. """
  82. return _ldap_function_call(None,_ldap.set_option,option,invalue)
  83. def escape_str(escape_func,s,*args):
  84. """
  85. Applies escape_func() to all items of `args' and returns a string based
  86. on format string `s'.
  87. """
  88. return s % tuple(escape_func(v) for v in args)
  89. def strf_secs(secs):
  90. """
  91. Convert seconds since epoch to a string compliant to LDAP syntax GeneralizedTime
  92. """
  93. return time.strftime('%Y%m%d%H%M%SZ', time.gmtime(secs))
  94. def strp_secs(dt_str):
  95. """
  96. Convert LDAP syntax GeneralizedTime to seconds since epoch
  97. """
  98. return timegm(time.strptime(dt_str, '%Y%m%d%H%M%SZ'))