proxy.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (c) 2010-2024 openpyxl
  2. from copy import copy
  3. from openpyxl.compat import deprecated
  4. class StyleProxy:
  5. """
  6. Proxy formatting objects so that they cannot be altered
  7. """
  8. __slots__ = ('__target')
  9. def __init__(self, target):
  10. self.__target = target
  11. def __repr__(self):
  12. return repr(self.__target)
  13. def __getattr__(self, attr):
  14. return getattr(self.__target, attr)
  15. def __setattr__(self, attr, value):
  16. if attr != "_StyleProxy__target":
  17. raise AttributeError("Style objects are immutable and cannot be changed."
  18. "Reassign the style with a copy")
  19. super().__setattr__(attr, value)
  20. def __copy__(self):
  21. """
  22. Return a copy of the proxied object.
  23. """
  24. return copy(self.__target)
  25. def __add__(self, other):
  26. """
  27. Add proxied object to another instance and return the combined object
  28. """
  29. return self.__target + other
  30. @deprecated("Use copy(obj) or cell.obj = cell.obj + other")
  31. def copy(self, **kw):
  32. """Return a copy of the proxied object. Keyword args will be passed through"""
  33. cp = copy(self.__target)
  34. for k, v in kw.items():
  35. setattr(cp, k, v)
  36. return cp
  37. def __eq__(self, other):
  38. return self.__target == other
  39. def __ne__(self, other):
  40. return not self == other