diff options
Diffstat (limited to 'grc/core/Element.py')
-rw-r--r-- | grc/core/Element.py | 96 |
1 files changed, 73 insertions, 23 deletions
diff --git a/grc/core/Element.py b/grc/core/Element.py index 67c36e12b4..f07bb113e1 100644 --- a/grc/core/Element.py +++ b/grc/core/Element.py @@ -1,28 +1,44 @@ -""" -Copyright 2008, 2009, 2015 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" +# Copyright 2008, 2009, 2015, 2016 Free Software Foundation, Inc. +# This file is part of GNU Radio +# +# GNU Radio Companion is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# GNU Radio Companion is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + +import weakref +import functools + + +class lazy_property(object): + + def __init__(self, func): + self.func = func + functools.update_wrapper(self, func) + + def __get__(self, instance, cls): + if instance is None: + return self + value = self.func(instance) + weak_value = weakref.proxy(value) if not weakref.ProxyType else value + setattr(instance, self.func.__name__, weak_value) + return weak_value class Element(object): def __init__(self, parent=None): - self._parent = parent - self._error_messages = list() + self._parent = weakref.ref(parent) if parent else lambda: None + self._error_messages = [] ################################################## # Element Validation API @@ -33,6 +49,7 @@ class Element(object): Call this base method before adding error messages in the subclass. """ del self._error_messages[:] + for child in self.get_children(): child.validate() @@ -64,7 +81,9 @@ class Element(object): a list of error message strings """ error_messages = list(self._error_messages) # Make a copy - for child in filter(lambda c: c.get_enabled() and not c.get_bypassed(), self.get_children()): + for child in self.get_children(): + if not child.get_enabled() or child.get_bypassed(): + continue for msg in child.get_error_messages(): error_messages.append("{}:\n\t{}".format(child, msg.replace("\n", "\n\t"))) return error_messages @@ -86,8 +105,39 @@ class Element(object): ############################################## # Tree-like API ############################################## - def get_parent(self): - return self._parent + @property + def parent(self): + return self._parent() + + def get_parent_by_type(self, cls): + parent = self.parent + if parent is None: + return None + elif isinstance(parent, cls): + return parent + else: + return parent.get_parent_by_type(cls) + + @lazy_property + def parent_platform(self): + from .Platform import Platform + return self.get_parent_by_type(Platform) + + @lazy_property + def parent_flowgraph(self): + from .FlowGraph import FlowGraph + return self.get_parent_by_type(FlowGraph) + + @lazy_property + def parent_block(self): + from .Block import Block + return self.get_parent_by_type(Block) + + def reset_parents_by_type(self): + """Reset all lazy properties""" + for name, obj in vars(Element): + if isinstance(obj, lazy_property): + delattr(self, name) def get_children(self): return list() |