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.

PersistentSearch.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """
  2. """
  3. # Created on 2016.07.08
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2016 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. try:
  25. from queue import Empty
  26. except ImportError: # Python 2
  27. # noinspection PyUnresolvedReferences
  28. from Queue import Empty
  29. from ...core.exceptions import LDAPExtensionError
  30. from ...protocol.persistentSearch import persistent_search_control
  31. from ... import SEQUENCE_TYPES
  32. from ...utils.dn import safe_dn
  33. class PersistentSearch(object):
  34. def __init__(self,
  35. connection,
  36. search_base,
  37. search_filter,
  38. search_scope,
  39. dereference_aliases,
  40. attributes,
  41. size_limit,
  42. time_limit,
  43. controls,
  44. changes_only,
  45. events_type,
  46. notifications,
  47. streaming,
  48. callback
  49. ):
  50. if connection.strategy.sync:
  51. raise LDAPExtensionError('Persistent Search needs an asynchronous streaming connection')
  52. if connection.check_names and search_base:
  53. search_base = safe_dn(search_base)
  54. self.connection = connection
  55. self.changes_only = changes_only
  56. self.notifications = notifications
  57. self.message_id = None
  58. self.base = search_base
  59. self.filter = search_filter
  60. self.scope = search_scope
  61. self.dereference_aliases = dereference_aliases
  62. self.attributes = attributes
  63. self.size_limit = size_limit
  64. self.time_limit = time_limit
  65. self.connection.strategy.streaming = streaming
  66. if callback and callable(callback):
  67. self.connection.strategy.callback = callback
  68. elif callback:
  69. raise LDAPExtensionError('callback is not callable')
  70. if not isinstance(controls, SEQUENCE_TYPES):
  71. self.controls = []
  72. else:
  73. self.controls = controls
  74. self.controls.append(persistent_search_control(events_type, changes_only, notifications))
  75. self.start()
  76. def start(self):
  77. if self.message_id: # persistent search already started
  78. return
  79. if not self.connection.bound:
  80. self.connection.bind()
  81. with self.connection.strategy.async_lock:
  82. self.message_id = self.connection.search(search_base=self.base,
  83. search_filter=self.filter,
  84. search_scope=self.scope,
  85. dereference_aliases=self.dereference_aliases,
  86. attributes=self.attributes,
  87. size_limit=self.size_limit,
  88. time_limit=self.time_limit,
  89. controls=self.controls)
  90. self.connection.strategy.persistent_search_message_id = self.message_id
  91. def stop(self):
  92. self.connection.abandon(self.message_id)
  93. self.connection.unbind()
  94. if self.message_id in self.connection.strategy._responses:
  95. del self.connection.strategy._responses[self.message_id]
  96. if hasattr(self.connection.strategy, '_requests') and self.message_id in self.connection.strategy._requests: # asynchronous strategy has a dict of request that could be returned by get_response()
  97. del self.connection.strategy._requests[self.message_id]
  98. self.connection.strategy.persistent_search_message_id = None
  99. self.message_id = None
  100. def next(self):
  101. if not self.connection.strategy.streaming and not self.connection.strategy.callback:
  102. try:
  103. return self.connection.strategy.events.get_nowait()
  104. except Empty:
  105. return None
  106. raise LDAPExtensionError('Persistent search is not accumulating events in queue')