def default_language_from_metadata_and_ext(notebook, ext):
    """Return the default language for a notebook that was read
    from the given file extension"""
    default_from_ext = 'R' if ext == '.R' else 'python'

    return (notebook.metadata.get('language_info', {}).get('name')
            or notebook.metadata.get('main_language') or default_from_ext)