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.

asyncStream.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. """
  3. # Created on 2016.07.10
  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 Queue
  26. except ImportError: # Python 2
  27. # noinspection PyUnresolvedReferences
  28. from Queue import Queue
  29. from io import StringIO
  30. from os import linesep
  31. from ..protocol.rfc2849 import decode_persistent_search_control
  32. from ..strategy.asynchronous import AsyncStrategy
  33. from ..core.exceptions import LDAPLDIFError
  34. from ..utils.conv import prepare_for_stream
  35. from ..protocol.rfc2849 import persistent_search_response_to_ldif, add_ldif_header
  36. # noinspection PyProtectedMember
  37. class AsyncStreamStrategy(AsyncStrategy):
  38. """
  39. This strategy is asynchronous. It streams responses in a generator as they appear in the self._responses container
  40. """
  41. def __init__(self, ldap_connection):
  42. AsyncStrategy.__init__(self, ldap_connection)
  43. self.can_stream = True
  44. self.line_separator = linesep
  45. self.all_base64 = False
  46. self.stream = None
  47. self.order = dict()
  48. self._header_added = False
  49. self.persistent_search_message_id = None
  50. self.streaming = False
  51. self.callback = None
  52. self.events = Queue()
  53. del self._requests # remove _requests dict from Async Strategy
  54. def _start_listen(self):
  55. AsyncStrategy._start_listen(self)
  56. if self.streaming:
  57. if not self.stream or (isinstance(self.stream, StringIO) and self.stream.closed):
  58. self.set_stream(StringIO())
  59. def _stop_listen(self):
  60. AsyncStrategy._stop_listen(self)
  61. if self.streaming:
  62. self.stream.close()
  63. def accumulate_stream(self, message_id, change):
  64. if message_id == self.persistent_search_message_id:
  65. with self.async_lock:
  66. self._responses[message_id] = []
  67. if self.streaming:
  68. if not self._header_added and self.stream.tell() == 0:
  69. header = add_ldif_header(['-'])[0]
  70. self.stream.write(prepare_for_stream(header + self.line_separator + self.line_separator))
  71. ldif_lines = persistent_search_response_to_ldif(change)
  72. if self.stream and ldif_lines and not self.connection.closed:
  73. fragment = self.line_separator.join(ldif_lines)
  74. if not self._header_added and self.stream.tell() == 0:
  75. self._header_added = True
  76. header = add_ldif_header(['-'])[0]
  77. self.stream.write(prepare_for_stream(header + self.line_separator + self.line_separator))
  78. self.stream.write(prepare_for_stream(fragment + self.line_separator + self.line_separator))
  79. else: # strategy is not streaming, events are added to a queue
  80. notification = decode_persistent_search_control(change)
  81. if notification:
  82. change.update(notification)
  83. del change['controls']['2.16.840.1.113730.3.4.7']
  84. if not self.callback:
  85. self.events.put(change)
  86. else:
  87. self.callback(change)
  88. def get_stream(self):
  89. if self.streaming:
  90. return self.stream
  91. return None
  92. def set_stream(self, value):
  93. error = False
  94. try:
  95. if not value.writable():
  96. error = True
  97. except (ValueError, AttributeError):
  98. error = True
  99. if error:
  100. raise LDAPLDIFError('stream must be writable')
  101. self.stream = value
  102. self.streaming = True