@@ -0,0 +1,2539 @@
+from avalon import io, api%0A%0A%0Adef switch_item(container,%0A                asset_name=None,%0A                subset_name=None,%0A                representation_name=None):%0A    %22%22%22Switch container asset, subset or representation of a container by name.%0A%0A    It'll always switch to the latest version - of course a different%0A    approach could be implemented.%0A%0A    Args:%0A        container (dict): data of the item to switch with%0A        asset_name (str): name of the asset%0A        subset_name (str): name of the subset%0A        representation_name (str): name of the representation%0A%0A    Returns:%0A        dict%0A%0A    %22%22%22%0A%0A    if all(not x for x in %5Basset_name, subset_name, representation_name%5D):%0A        raise ValueError(%0A            %22Must have at least one change provided to switch.%22)%0A%0A    # Collect any of current asset, subset and representation if not provided%0A    # so we can use the original name from those.%0A    if any(not x for x in %5Basset_name, subset_name, representation_name%5D):%0A        _id = io.ObjectId(container%5B%22representation%22%5D)%0A        representation = io.find_one(%7B%22type%22: %22representation%22, %22_id%22: _id%7D)%0A        version, subset, asset, project = io.parenthood(representation)%0A%0A        if asset_name is None:%0A            asset_name = asset%5B%22name%22%5D%0A%0A        if subset_name is None:%0A            subset_name = subset%5B%22name%22%5D%0A%0A        if representation_name is None:%0A            representation_name = representation%5B%22name%22%5D%0A%0A    # Find the new one%0A    asset = io.find_one(%7B%22name%22: asset_name, %22type%22: %22asset%22%7D)%0A    assert asset, (%22Could not find asset in the database with the name %22%0A                   %22'%25s'%22 %25 asset_name)%0A%0A    subset = io.find_one(%7B%22name%22: subset_name,%0A                          %22type%22: %22subset%22,%0A                          %22parent%22: asset%5B%22_id%22%5D%7D)%0A    assert subset, (%22Could not find subset in the database with the name %22%0A                    %22'%25s'%22 %25 subset_name)%0A%0A    version = io.find_one(%7B%22type%22: %22version%22,%0A                           %22parent%22: subset%5B%22_id%22%5D%7D,%0A                          sort=%5B('name', -1)%5D)%0A%0A    assert version, %22Could not find a version for %7B%7D.%7B%7D%22.format(%0A        asset_name, subset_name%0A    )%0A%0A    representation = io.find_one(%7B%22name%22: representation_name,%0A                                  %22type%22: %22representation%22,%0A                                  %22parent%22: version%5B%22_id%22%5D%7D)%0A%0A    assert representation, (%0A            %22Could not find representation in the database with%22%0A            %22 the name '%25s'%22 %25 representation_name)%0A%0A    api.switch(container, representation)%0A%0A    return representation

Add lib for switch_item