def test_init(self): meta = Mock() s = StringArrayMeta() meta.elements = {"e1": s, "e2": s, "e3": s} t = Table(meta) self.assertEquals([], t.e1) self.assertEquals([], t.e2) self.assertEquals([], t.e3) self.assertEquals("malcolm:core/Table:1.0", t.typeid)
def move_child_block(self): self.assertEqual(self.b.layout.x, [0]) new_layout = Table(self.c.layout.meta) new_layout.name = ["part2"] new_layout.mri = ["P45-MRI"] new_layout.x = [10] new_layout.y = [20] new_layout.visible = [True] self.b.layout = new_layout self.assertEqual(self.b.layout.x, [10])
def setUp(self): meta = Mock() meta.elements = OrderedDict() meta.elements["e1"] = NumberArrayMeta("int32") meta.elements["e2"] = NumberArrayMeta("int32") meta.elements["e3"] = NumberArrayMeta("int32") self.t = Table(meta) self.t.e1 = [1] self.t.e2 = [2] self.t.e3 = [3]
def set_layout(self, value): """Set the layout table value. Called on attribute put""" # If it isn't a table, make it one if not isinstance(value, Table): value = Table(self.layout.meta, value) # Can't do this with changes_squashed as it will call update_modified # from another thread and deadlock part_info = self.run_hook( self.Layout, self.create_part_contexts(only_visible=False), self.port_info, value) with self.changes_squashed: layout_table = Table(self.layout.meta) layout_parts = LayoutInfo.filter_parts(part_info) for name, layout_infos in layout_parts.items(): assert len(layout_infos) == 1, \ "%s returned more than 1 layout infos" % name layout_parts[name] = layout_infos[0] layout_table.name = list(layout_parts) layout_table.mri = [i.mri for i in layout_parts.values()] layout_table.x = [i.x for i in layout_parts.values()] layout_table.y = [i.y for i in layout_parts.values()] layout_table.visible = [i.visible for i in layout_parts.values()] try: np.testing.assert_equal( layout_table.visible, self.layout.value.visible) except AssertionError: visibility_changed = True else: visibility_changed = False self.layout.set_value(layout_table) if self.saved_visibility is None: # First write of table, set layout and exports saves self.saved_visibility = layout_table.visible self.saved_exports = self.exports.value.to_dict() if visibility_changed: self.update_modified() self.update_exportable() # Part visibility changed, might have attributes or methods # that we need to hide or show self._update_block_endpoints()
def do_init(self): # This will do an initial poll of the exportable parts, # so don't update here super(ManagerController, self).do_init() # List the configDir and add to choices self._set_layout_names() # This will trigger all parts to report their layout, making sure # the layout table has a valid value. This will also call # self._update_block_endpoints() self.set_layout(Table(self.layout.meta)) # If given a default config, load this if self.params.initialDesign: self.do_load(self.params.initialDesign)
def do_reset(self): super(ManagerController, self).do_reset() # This will trigger all parts to report their layout, making sure the # layout table has a valid value self.set_layout(Table(self.layout.meta)) # List the configDir and add to choices self._set_layout_names() # If we have no load_structure (initial reset) define one if self.load_structure is None: if self.params.defaultConfig: self.load_layout(self.params.defaultConfig) else: self.load_structure = self._save_to_structure()
def test_init_with_dict(self): meta = Mock() meta.elements = { "e1": NumberArrayMeta("int32"), "e2": StringArrayMeta(), "e3": StringArrayMeta() } d = {"e1": [0, 1], "e3": ["value"]} t = Table(meta, d) assert [0, 1] == list(t.e1) assert () == t.e2 assert ("value", ) == t.e3 assert "malcolm:core/Table:1.0" == t.typeid
def test_init_with_dict(self): meta = Mock() meta.elements = { "e1": NumberArrayMeta("int32"), "e2": StringArrayMeta(), "e3": StringArrayMeta() } d = {"e1": [0, 1], "e3": ["value"]} t = Table(meta, d) self.assertEquals([0, 1], list(t.e1)) self.assertEquals((), t.e2) self.assertEquals(("value", ), t.e3) self.assertEquals("malcolm:core/Table:1.0", t.typeid)
def do_load(self, design): filename = self._validated_config_filename(design) with open(filename, "r") as f: text = f.read() structure = json_decode(text) # Set the layout table layout_table = Table(self.layout.meta) for part_name, part_structure in structure.get("layout", {}).items(): layout_table.append([ part_name, "", part_structure["x"], part_structure["y"], part_structure["visible"]]) self.set_layout(layout_table) # Set the exports table exports_table = Table(self.exports.meta) for name, export_name in structure.get("exports", {}).items(): exports_table.append([name, export_name]) self.exports.set_value(exports_table) # Run the load hook to get parts to load their own structure self.run_hook(self.Load, self.create_part_contexts(only_visible=False), structure) self._mark_clean(design)
def test_to_dict(self): columns = OrderedDict() columns["foo"] = StringArrayMeta(label="Foo") columns["bar"] = StringArrayMeta() meta = TableMeta(description="desc", tags=[], writeable=True, label="my label", columns=columns) value = Table(meta) value.foo = ["foo1", "foo2"] value.bar = ["bar1", "bar2"] o = meta.make_attribute(value) self.assertEqual(o.to_dict(), self.serialized)
def table_from_list(self, int_values): table = Table(self.meta) if self.fields: nconsume = self._calc_nconsume() for i in range(int(len(int_values) / nconsume)): int_value = 0 for c in range(nconsume): int_value += int_values[i*nconsume+c] << (32 * c) row = [] for name, (bits_hi, bits_lo) in self.fields.items(): field_value = (int_value & (2 ** (bits_hi + 1) - 1)) >> bits_lo row.append(field_value) table.append(row) return table
def test_to_dict(self): t = Table(self.meta) t.e1 = ["value"] t.e2 = [1, 2] t.e3 = [0] expected = OrderedDict() expected["typeid"] = "malcolm:core/Table:1.0" expected["e1"] = ["value"] expected["e2"] = [1, 2] expected["e3"] = [0] actual = t.to_dict() # numpy compare gets in the way... for k, v in actual.items(): if k != "typeid": actual[k] = list(v) assert expected == actual
def test_set_and_load_layout(self): new_layout = Table(self.c.layout.meta) new_layout.name = ["part2"] new_layout.mri = ["anything"] new_layout.x = [10] new_layout.y = [20] new_layout.visible = [False] self.c.set_layout(new_layout) assert self.c.parts['part2'].x == 10 assert self.c.parts['part2'].y == 20 assert self.c.parts['part2'].visible == False assert self.c.modified.value == True assert self.c.modified.alarm.message == "layout changed" # save the layout, modify and restore it call_with_params(self.c.save, design='testSaveLayout') assert self.c.modified.value == False self.check_expected_save(10.0, 20.0, "false") self.c.parts['part2'].x = 30 self.c.set_design('testSaveLayout') assert self.c.parts['part2'].x == 10
def test_set_and_load_layout(self): self.c.edit() self.checkState(self.sm.EDITABLE, child=False) new_layout = Table(self.c.layout.meta) new_layout.name = ["part2"] new_layout.mri = ["P45-MRI"] new_layout.x = [10] new_layout.y = [20] new_layout.visible = [True] self.b.layout = new_layout self.assertEqual(self.c.parts['part2'].x, 10) self.assertEqual(self.c.parts['part2'].y, 20) self.assertEqual(self.c.parts['part2'].visible, True) # save the layout, modify and restore it params = {'layoutName': 'testSaveLayout'} params = ManagerController.save.MethodMeta.prepare_input_map(**params) self.c.save(params) self.check_expected_save(10.0, 20.0, "true") self.c.parts['part2'].x = 30 self.b.layoutName = 'testSaveLayout' self.assertEqual(self.c.parts['part2'].x, 10)
def test_init(self): t = Table() assert not t.call_types
def test_init_with_none(self): meta = Mock() meta.elements = {"e1": StringArrayMeta()} t = Table(meta, None) self.assertEquals((), t.e1) self.assertEquals("malcolm:core/Table:1.0", t.typeid)
def test_init_with_none(self): meta = Mock() meta.elements = {"e1": StringArrayMeta()} t = Table(meta, None) assert () == t.e1 assert "malcolm:core/Table:1.0" == t.typeid
def do_reset(self): super(ManagerController, self).do_reset() self.set_layout(Table(layout_table_meta))