in_memory.py 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from .base import Clipboard, ClipboardData
  2. from collections import deque
  3. __all__ = (
  4. 'InMemoryClipboard',
  5. )
  6. class InMemoryClipboard(Clipboard):
  7. """
  8. Default clipboard implementation.
  9. Just keep the data in memory.
  10. This implements a kill-ring, for Emacs mode.
  11. """
  12. def __init__(self, data=None, max_size=60):
  13. assert data is None or isinstance(data, ClipboardData)
  14. assert max_size >= 1
  15. self.max_size = max_size
  16. self._ring = deque()
  17. if data is not None:
  18. self.set_data(data)
  19. def set_data(self, data):
  20. assert isinstance(data, ClipboardData)
  21. self._ring.appendleft(data)
  22. while len(self._ring) > self.max_size:
  23. self._ring.pop()
  24. def get_data(self):
  25. if self._ring:
  26. return self._ring[0]
  27. else:
  28. return ClipboardData()
  29. def rotate(self):
  30. if self._ring:
  31. # Add the very first item at the end.
  32. self._ring.append(self._ring.popleft())