Commit e4409568 authored by Daniel Brown's avatar Daniel Brown
Browse files

further work for freezing components

parent 0b5db2fb
Pipeline #1771 passed with stage
in 19 seconds
...@@ -43,31 +43,25 @@ class Command(object): ...@@ -43,31 +43,25 @@ class Command(object):
When deep copying a kat object we need to take into account When deep copying a kat object we need to take into account
the instance specific properties. the instance specific properties.
""" """
cls = self.__class__ cls = self.__class__
result = cls.__new__(cls) result = cls.__new__(cls)
result._unfreeze()
result.__dict__ = copy.deepcopy(self.__dict__, memo) result.__dict__ = copy.deepcopy(self.__dict__, memo)
for _ in result._putters: for _ in result._putters:
_._updateOwner(result) _._updateOwner(result)
result._freeze()
return result return result
def _freeze(self): self.__dict__["____FROZEN____"] = True def _freeze(self): self.__dict__["____FROZEN____"] = True
def _unfreeze(self): self.__dict__["____FROZEN____"] = False def _unfreeze(self): self.__dict__["____FROZEN____"] = False
def __setattr__(self, name, value): def __setattr__(self, name, value):
if self.__dict__["____FROZEN____"] and not hasattr(self, name): if "____FROZEN____" in self.__dict__ and self.__dict__["____FROZEN____"] and not hasattr(self, name):
warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2) warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2)
if hasattr(self, name) and hasattr(self.__class__, name): super(Command, self).__setattr__(name, value)
prop = getattr(self.__class__, name)
if isinstance(prop, property):
prop.fset(self, value)
return
self.__dict__[name] = value
def getFinesseText(self): def getFinesseText(self):
""" Base class for individual finesse optical components """ """ Base class for individual finesse optical components """
......
...@@ -100,7 +100,8 @@ class Component(object): ...@@ -100,7 +100,8 @@ class Component(object):
cnew = type(cnew_name, (cls,), {}) cnew = type(cnew_name, (cls,), {})
return object.__new__(cnew) o = object.__new__(cnew)
return o
def __init__(self, name=None): def __init__(self, name=None):
self._unfreeze() self._unfreeze()
...@@ -125,32 +126,26 @@ class Component(object): ...@@ -125,32 +126,26 @@ class Component(object):
def _unfreeze(self): self.__dict__["____FROZEN____"] = False def _unfreeze(self): self.__dict__["____FROZEN____"] = False
def __setattr__(self, name, value): def __setattr__(self, name, value):
if self.__dict__["____FROZEN____"] and not hasattr(self, name): if "____FROZEN____" in self.__dict__ and self.__dict__["____FROZEN____"] and not hasattr(self, name):
warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2) warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2)
if hasattr(self, name) and hasattr(self.__class__, name): super(Component, self).__setattr__(name, value)
prop = getattr(self.__class__, name)
if isinstance(prop, property):
prop.fset(self, value)
return
self.__dict__[name] = value
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
""" """
When deep copying a kat object we need to take into account When deep copying a kat object we need to take into account
the instance specific properties. the instance specific properties.
""" """
# Here we create a copy of this object based of the base class # Here we create a copy of this object based of the base class
# of this one, otherwise we're making a copy of a copy of a copy... # of this one, otherwise we're making a copy of a copy of a copy...
result = self.__class__.__new__(self.__class__.__base__) result = self.__class__.__new__(self.__class__.__base__)
result._unfreeze()
result.__dict__ = copy.deepcopy(self.__dict__, memo) result.__dict__ = copy.deepcopy(self.__dict__, memo)
for _ in result._params: for _ in result._params:
_._updateOwner(result) _._updateOwner(result)
result._freeze()
return result return result
def _register_param(self, param): def _register_param(self, param):
......
...@@ -51,11 +51,12 @@ class BaseDetector(object) : ...@@ -51,11 +51,12 @@ class BaseDetector(object) :
cnew = type(cnew_name, (cls,), {}) cnew = type(cnew_name, (cls,), {})
return object.__new__(cnew) o = object.__new__(cnew)
return o
def __init__(self, name, nodes=None, max_nodes=1): def __init__(self, name, nodes=None, max_nodes=1):
self.__dict__["____FROZEN____"] = False self._unfreeze()
self.__name = name self.__name = name
self._svgItem = None self._svgItem = None
...@@ -103,17 +104,10 @@ class BaseDetector(object) : ...@@ -103,17 +104,10 @@ class BaseDetector(object) :
def _unfreeze(self): self.__dict__["____FROZEN____"] = False def _unfreeze(self): self.__dict__["____FROZEN____"] = False
def __setattr__(self, name, value): def __setattr__(self, name, value):
if self.__dict__["____FROZEN____"] and not hasattr(self, name): if "____FROZEN____" in self.__dict__ and self.__dict__["____FROZEN____"] and not hasattr(self, name):
warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2) warnings.warn("'%s' does not have attribute called '%s'" % (self.__name, name), stacklevel=2)
if hasattr(self, name) and hasattr(self.__class__, name): super(BaseDetector, self).__setattr__(name, value)
prop = getattr(self.__class__, name)
if isinstance(prop, property):
prop.fset(self, value)
return
self.__dict__[name] = value
def __deepcopy__(self, memo): def __deepcopy__(self, memo):
""" """
...@@ -124,8 +118,10 @@ class BaseDetector(object) : ...@@ -124,8 +118,10 @@ class BaseDetector(object) :
# Here we create a copy of this object based of the base class # Here we create a copy of this object based of the base class
# of this one, otherwise we're making a copy of a copy of a copy... # of this one, otherwise we're making a copy of a copy of a copy...
result = self.__class__.__new__(self.__class__.__base__) result = self.__class__.__new__(self.__class__.__base__)
result._unfreeze()
result.__dict__ = copy.deepcopy(self.__dict__, memo) result.__dict__ = copy.deepcopy(self.__dict__, memo)
result._freeze()
return result return result
def _register_param(self, param): def _register_param(self, param):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment