From ac2babe9d2a7992f5fcfc97d8606329e76c1666e Mon Sep 17 00:00:00 2001
From: Sebastian Koslowski <koslowski@kit.edu>
Date: Tue, 26 Nov 2013 14:01:20 +0100
Subject: grc: rewrite of nports code (WIP)

---
 grc/python/Port.py | 48 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 7 deletions(-)

(limited to 'grc/python/Port.py')

diff --git a/grc/python/Port.py b/grc/python/Port.py
index 5f1247ce45..f664204e85 100644
--- a/grc/python/Port.py
+++ b/grc/python/Port.py
@@ -92,6 +92,7 @@ class Port(_Port, _GUIPort):
         """
         self._n = n
         if n['type'] == 'msg': n['key'] = 'msg'
+        if n['type'] == 'message': n['key'] = n['name']
         if dir == 'source' and not n.find('key'):
             n['key'] = str(block._source_count)
             block._source_count += 1
@@ -109,6 +110,7 @@ class Port(_Port, _GUIPort):
         self._nports = n.find('nports') or ''
         self._vlen = n.find('vlen') or ''
         self._optional = bool(n.find('optional'))
+        self._clones = []  # references to cloned ports (for nports > 1)
 
     def get_types(self): return Constants.TYPE_TO_SIZEOF.keys()
 
@@ -183,8 +185,8 @@ class Port(_Port, _GUIPort):
             the number of ports or 1
         """
         nports = self.get_parent().resolve_dependencies(self._nports)
-        #return blank if nports is blank
-        if not nports: return ''
+        if not nports:  # return blank if nports is blank
+            return ''
         try:
             return max(1, int(self.get_parent().get_parent().evaluate(nports)))
         except:
@@ -215,9 +217,41 @@ class Port(_Port, _GUIPort):
             return '#%.2x%.2x%.2x'%(r, g, b)
         except: return _Port.get_color(self)
 
-    def copy(self, new_key=None):
+    def get_clones(self):
+        """
+        Get clones of this master port
+        """
+        return self._clones
+
+    def add_clone(self):
+        """
+        Create a clone of this (master) port and store it internally
+        This clone will have the same key. The name get an index
+        If this is the first clone, this (master) port will get a 0 appended to its name
+
+        Returns:
+            the newly create clone
+        """
+        if not self._clones:  # add index to master port name
+            self._name = self._n['name'] + '0'
+            if not self._key.isdigit():
+                self._n['key'] = self._name
+
+        # Prepare a copy of the odict for the clone
         n = self._n.copy()
-        #remove nports from the key so the copy cannot be a duplicator
-        if n.has_key('nports'): n.pop('nports')
-        if new_key: n['key'] = new_key
-        return self.__class__(self.get_parent(), n, self._dir)
+        if 'nports' in n: n.pop('nports')  # remove nports from the key so the copy cannot be a duplicator
+        n['name'] = self._n['name'] + str(len(self._clones) + 1)
+        n['key'] = '99999' if self._key.isdigit() else n['name']
+
+        port = self.__class__(self.get_parent(), n, self._dir)
+        self._clones.append(port)
+        return port
+
+    def remove_duplicate(self, port):
+        """
+        Remove a cloned port from the internal list
+        Remove to index 0 of the master port name if there are no more clones
+        """
+        self._clones.remove(port)
+        if not self._clones:
+            self._name = self._n['key'] = self._n['name']
-- 
cgit v1.2.3