def package_api_to_dict(api1_dict, context):
    package = context.get('package')
    api_version = context.get('api_version')
    assert api_version, 'No api_version supplied in context'
    dictized = {}
    for (key, value) in api1_dict.iteritems():
        new_value = value
        if (key == 'tags'):
            if isinstance(value, string_types):
                new_value = [{'name': item} for item in value.split()]
            else:
                new_value = [{'name': item} for item in value]
        if (key == 'extras'):
            updated_extras = {}
            if package:
                updated_extras.update(package.extras)
            updated_extras.update(value)
            new_value = []
            for (extras_key, extras_value) in updated_extras.iteritems():
                new_value.append({'key': extras_key, 'value': extras_value})
        if ((key == 'groups') and len(value)):
            if (api_version == 1):
                new_value = [{'name': item} for item in value]
            else:
                new_value = [{'id': item} for item in value]
        dictized[key] = new_value
    download_url = dictized.pop('download_url', None)
    if (download_url and (not dictized.get('resources'))):
        dictized['resources'] = [{'url': download_url}]
    download_url = dictized.pop('download_url', None)
    return dictized
def package_api_to_dict(api1_dict, context):
    package = context.get('package')
    api_version = context.get('api_version')
    assert api_version, 'No api_version supplied in context'
    dictized = {}
    for (key, value) in api1_dict.iteritems():
        new_value = value
        if (key == 'tags'):
            if isinstance(value, string_types):
                new_value = [{'name': item} for item in value.split()]
            else:
                new_value = [{'name': item} for item in value]
        if (key == 'extras'):
            updated_extras = {}
            if package:
                updated_extras.update(package.extras)
            updated_extras.update(value)
            new_value = []
            for (extras_key, extras_value) in updated_extras.iteritems():
                new_value.append({'key': extras_key, 'value': extras_value})
        if ((key == 'groups') and len(value)):
            if (api_version == 1):
                new_value = [{'name': item} for item in value]
            else:
                new_value = [{'id': item} for item in value]
        dictized[key] = new_value
    download_url = dictized.pop('download_url', None)
    if (download_url and (not dictized.get('resources'))):
        dictized['resources'] = [{'url': download_url}]
    download_url = dictized.pop('download_url', None)
    return dictized