freeze.py 818 Bytes
Newer Older
Daniel Brown's avatar
Daniel Brown committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import warnings

def canFreeze(cls):
    
    def _freeze(self): self.__dict__["____FROZEN____"] = True
    def _unfreeze(self): self.__dict__["____FROZEN____"] = False
    
    def frozensetattr(self, name, value):
        if "____FROZEN____" in self.__dict__ and self.__dict__["____FROZEN____"] and not hasattr(self, name):
            if hasattr(self, "name"):
                n = self.name
            elif hasattr(self, "__name"):
                n = self.__name
            else:
                n = self.__class__.__name__
                
            warnings.warn("'%s' does not have attribute called '%s'" % (n, name), stacklevel=2)
            
        super(cls, self).__setattr__(name, value)

    cls.__setattr__ = frozensetattr
    cls._freeze = _freeze
    cls._unfreeze = _unfreeze
    
    return cls