Source code

Download source archive ↓

src/openpyxl/styles/proxy.py

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