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

Cleaning up removing of components/commands/detectors and loose references. Fixing block remove

parent f16c95b6
...@@ -53,12 +53,25 @@ class Command(object): ...@@ -53,12 +53,25 @@ class Command(object):
for _ in self._putters_to_register: for _ in self._putters_to_register:
kat.registerVariable(_.name, _) kat.registerVariable(_.name, _)
def remove(self): def _on_kat_remove(self):
self._kat.remove(self)
self.__removed = True self.__removed = True
for _ in self._putters_to_register: for i in range(len(self._putters_to_register)):
kat.unregisterVariable(_.name) _ = self._putters_to_register[i]
self._kat.unregisterVariable(_.name)
_.clearPuts()
del self._putters_to_register[i]
del self._putters_to_register[:]
def remove(self):
if self.__removed:
raise pkex.BasePyKatException("{0} has already been marked as removed".format(self.name))
else:
self._kat.remove(self)
@property @property
def name(self): return self.__name def name(self): return self.__name
......
...@@ -162,7 +162,17 @@ class Component(object): ...@@ -162,7 +162,17 @@ class Component(object):
self._kat = kat self._kat = kat
kat.nodes.registerComponentNodes(self, self._requested_node_names, self.__on_node_change) kat.nodes.registerComponentNodes(self, self._requested_node_names, self.__on_node_change)
def _on_kat_remove(self):
# inform all parameters that we have removed its owner
# so that it can then warn about any puts/vars/xaxis
for p in self._params:
p._onOwnerRemoved()
del self._params[:]
self.__removed = True
def __on_node_change(self): def __on_node_change(self):
# need to update the node gauss parameter setter members # need to update the node gauss parameter setter members
self.__update_node_setters() self.__update_node_setters()
...@@ -237,16 +247,10 @@ class Component(object): ...@@ -237,16 +247,10 @@ class Component(object):
def __str__(self): return self.name def __str__(self): return self.name
def remove(self): def remove(self):
self._kat.remove(self) if self.__removed:
raise pkex.BasePyKatException("{0} has already been marked as removed".format(self.name))
# inform all parameters that we have removed its owner else:
# so that it can then warn about any puts/vars/xaxis self._kat.remove(self)
for p in self._params:
p._onOwnerRemoved()
del self._params[:]
self.__removed = True
def getOptivisParameterDict(self): def getOptivisParameterDict(self):
if len(self._params) == 0: if len(self._params) == 0:
......
...@@ -120,6 +120,9 @@ class BaseDetector(object) : ...@@ -120,6 +120,9 @@ class BaseDetector(object) :
if rn != None: if rn != None:
self._nodes.append(kat.nodes.createNode(rn)) self._nodes.append(kat.nodes.createNode(rn))
def _on_kat_remove(self):
self.__removed = True
def remove(self): def remove(self):
if self.__removed: if self.__removed:
raise pkex.BasePyKatException("{0} has already been marked as removed".format(self.name)) raise pkex.BasePyKatException("{0} has already been marked as removed".format(self.name))
......
...@@ -997,8 +997,8 @@ class kat(object): ...@@ -997,8 +997,8 @@ class kat(object):
sys.exit(1) sys.exit(1)
else: else:
return return
for o in self.__blocks[name].contents: for o in self.__blocks[name].contents.copy():
self.remove(o) self.remove(o)
del self.__blocks[name] del self.__blocks[name]
...@@ -1810,6 +1810,9 @@ class kat(object): ...@@ -1810,6 +1810,9 @@ class kat(object):
del nodes del nodes
if hasattr(obj, "_on_kat_remove"):
obj._on_kat_remove()
#import gc #import gc
#print (gc.get_referrers(obj)) #print (gc.get_referrers(obj))
......
...@@ -27,29 +27,30 @@ class putable(object): ...@@ -27,29 +27,30 @@ class putable(object):
@property @property
def isPutable(self): return self._isPutable def isPutable(self): return self._isPutable
def put(self, var, alt): def put(self, var, alt=False):
if not isinstance(var, putter): if var is not None and not isinstance(var, putter):
raise pkex.BasePyKatException("`%s` was not something that can be `put` to a parameter" % str(var)) raise pkex.BasePyKatException("`%s` was not something that can be `put` to a parameter" % str(var))
if self._putter is not None: # Remove existing puts
self._putter.put_count -= 1 if self._putter is not None: self._putter().unregister(self)
self._putter.putees.remove(self)
self._putter = var if var is not None:
self._putter = weakref.ref(var)
else:
self._putter = None
self._alt = alt self._alt = alt
if var is not None: if var is not None: self._putter().register(self)
self._putter.put_count += 1
self._putter.putees.append(self)
def _getPutFinesseText(self): def _getPutFinesseText(self):
rtn = [] rtn = []
if self._putter is not None: if self._putter is not None and self._putter() is not None:
putter_enabled = True putter_enabled = True
if hasattr(self._putter.owner, 'enabled'): if hasattr(self._putter().owner, 'enabled'):
putter_enabled = self._putter.owner.enabled putter_enabled = self._putter().owner.enabled
if putter_enabled: if putter_enabled:
if self._alt: if self._alt:
...@@ -58,7 +59,7 @@ class putable(object): ...@@ -58,7 +59,7 @@ class putable(object):
alt = '' alt = ''
# if something is being put to this # if something is being put to this
rtn.append("put{alt} {comp} {param} ${value}".format(alt=alt, comp=self._component_name, param=self._parameter_name, value=self._putter.put_name())) rtn.append("put{alt} {comp} {param} ${value}".format(alt=alt, comp=self._component_name, param=self._parameter_name, value=self._putter().put_name()))
return rtn return rtn
...@@ -73,11 +74,23 @@ class putter(object): ...@@ -73,11 +74,23 @@ class putter(object):
self.put_count = 0 self.put_count = 0
self._isPutter = isPutter self._isPutter = isPutter
self.putees = [] # list of params that this puts to self.putees = [] # list of params that this puts to
self.__owner = owner self.__owner = weakref.ref(owner)
assert(owner is not None) assert(owner is not None)
def clearPuts(self):
for _ in self.putees:
_.put(None)
def register(self, toput):
self.put_count += 1
self.putees.append(toput)
def unregister(self, item):
self.put_count -= 1
self.putees.remove(item)
@property @property
def owner(self): return self.__owner def owner(self): return self.__owner()
@property @property
def name(self): return self._put_name def name(self): return self._put_name
...@@ -114,7 +127,7 @@ class Param(putable, putter): ...@@ -114,7 +127,7 @@ class Param(putable, putter):
if var_name is None: if var_name is None:
var_name = "var_{0}_{1}".format(owner.name, name) var_name = "var_{0}_{1}".format(owner.name, name)
putter.__init__(self, var_name, isPutter) putter.__init__(self, var_name, owner, isPutter)
putable.__init__(self, owner.name, name, isPutable) putable.__init__(self, owner.name, name, isPutable)
......
Supports Markdown
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