proxy.py 1.5 KB

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