示例#1
0
    def test_all_equal(self):
        self.assertTrue(util.all_equal([]))
        self.assertTrue(util.all_equal(x for x in []))

        self.assertTrue(util.all_equal(x for x in [1]))
        self.assertFalse(util.all_equal(x for x in [1, 2]))
        self.assertFalse(util.all_equal([1, 2]))
示例#2
0
def replace_tiles_mod(old_to_new_tile_dict, sync_tpcreated=True):
    """A layout mod replacing tiles. See :func:`place_tile` for a description of parameters."""

    from mqe import tpcreator

    if not old_to_new_tile_dict:
        return

    all_tiles = old_to_new_tile_dict.keys() + old_to_new_tile_dict.values()
    assert util.all_equal(t.dashboard_id for t in all_tiles if t)
    assert util.all_equal(t.owner_id for t in all_tiles if t)

    old_to_new_master_dict = {old: new for old, new in old_to_new_tile_dict.items()
                              if new and old.is_master_tile() and new.is_master_tile()}

    def do_replace_tiles(layout_mod):
        layout_dict = layout_mod.layout.layout_dict

        for old_tile, new_tile in old_to_new_tile_dict.items():
            if new_tile:
                layout_mod.tile_replacement[old_tile] = new_tile
            else:
                layout_mod.detached_tiles.append(old_tile)

        ### replace masters

        # skip replacing tpcreated which are replaced as regular tiles
        skip_replacements = set(t.tile_id for t in old_to_new_tile_dict)

        tpcreated_tile_replacement = {}
        for old_master, new_master in old_to_new_master_dict.items():
            tpcreated_tile_replacement.update(
                tpcreator.replace_tpcreated(layout_mod.layout, old_master, new_master, sync_tpcreated,
                                            skip_replacements=skip_replacements))

        layout_mod.tile_replacement.update(tpcreated_tile_replacement)

        ### replace ids

        all_tile_items = old_to_new_tile_dict.items() + tpcreated_tile_replacement.items()
        for old_tile, new_tile in all_tile_items:
            if old_tile.tile_id not in layout_mod.layout.layout_dict:
                # if any of the old tiles is not present in the layout dict,
                # the whole function fails
                raise LayoutModificationImpossible()

        for old_tile, new_tile in all_tile_items:
            if new_tile:
                layout_dict[new_tile.tile_id] = layout_dict[old_tile.tile_id]
            del layout_dict[old_tile.tile_id]

        # repack layout if deletes of master or tpcreated tiles were made,
        # pack upwards if regular deletes were made
        involves_deletes = any(not new_tile for old_tile, new_tile in all_tile_items)
        involves_tpcreator = any(not new_tile and (old_tile.is_master_tile() or \
                                                   old_tile.get_master_tile_id())
                                 for old_tile, new_tile in all_tile_items)
        if involves_tpcreator:
            repack_mod()(layout_mod)
        elif involves_deletes:
            pack_upwards_mod()(layout_mod)

    return do_replace_tiles