Modify RecordObject to prefix all signal names with the record name
authorMichael Nolan <mtnolan2640@gmail.com>
Wed, 24 Jun 2020 20:03:10 +0000 (16:03 -0400)
committerMichael Nolan <mtnolan2640@gmail.com>
Wed, 24 Jun 2020 20:03:10 +0000 (16:03 -0400)
src/nmutil/iocontrol.py

index 853a1d040d9f6dcdfd48daee06674f94953b2f58..2bd78965b063c42d30fcf6fbf20af98bd49c1f11 100644 (file)
@@ -73,15 +73,33 @@ class Object:
         return list(self)
 
 
+def add_prefix_to_record_signals(prefix, record):
+    for key, val in record.fields.items():
+        if isinstance(val, Signal):
+            val.name = prefix + val.name
+        elif isinstance(val, Record):
+            add_prefix_to_record_signals(prefix, val)
+
 class RecordObject(Record):
     def __init__(self, layout=None, name=None):
         Record.__init__(self, layout=layout or [], name=name)
 
     def __setattr__(self, k, v):
+        print(f"RecordObject setattr({k}, {v})")
         #print (dir(Record))
         if (k.startswith('_') or k in ["fields", "name", "src_loc"] or
            k in dir(Record) or "fields" not in self.__dict__):
             return object.__setattr__(self, k, v)
+
+        # Prefix the signal name with the name of the recordobject
+        if isinstance(v, Signal):
+            v.name = self.name + "_" + v.name
+        elif isinstance(v, Record):
+            add_prefix_to_record_signals(self.name + "_", v)
+            print(f"Record: {v}")
+            print(v)
+
+
         self.fields[k] = v
         #print ("RecordObject setattr", k, v)
         if isinstance(v, Record):