get_config[source]

get_config(settings_path)

patch_config[source]

patch_config(settings_path, lib_name)

Test helper functions

def setup_settings_file(settings_path):
    original_lib_name = "django_fileresponse"
    settings_content = f"""
    [DEFAULT]
    lib_name = {original_lib_name}
    """
    with settings_path.open("w") as f:
        f.write(settings_content)
    return settings_path, original_lib_name


def get_actual_lib_name(settings_path):
    config = get_config(settings_path)
    return config["DEFAULT"]["lib_name"]

Contextmanager patching settings.ini lib_name

patch_lib_name[source]

patch_lib_name(settings_path, lib_name)

Usage

settings_path, original_lib_name = setup_settings_file(Path.cwd() / "test.ini")
with patch_lib_name(settings_path, "fileresponse") as patched_config:
    patched_lib_name = get_actual_lib_name(settings_path)
    print("lib_name original vs patched: ", original_lib_name, patched_lib_name)
reset_lib_name = get_actual_lib_name(settings_path)
print("after leaving contextmanager, lib_name is back to original: ", reset_lib_name)
lib_name original vs patched:  django_fileresponse fileresponse
after leaving contextmanager, lib_name is back to original:  django_fileresponse
# arange
settings_path, original_lib_name = setup_settings_file(Path.cwd() / "test.ini")

# act and assert
patched_lib_name = "fileresponse"
with patch_lib_name(settings_path, patched_lib_name) as patched_config:
    actual_lib_name = get_actual_lib_name(settings_path)
    assert patched_lib_name == actual_lib_name

# assert
actual_lib_name = get_actual_lib_name(settings_path)
assert original_lib_name == actual_lib_name

Final Replacement for nbdev_build_docs

nbdev_build_docs_patched[source]

nbdev_build_docs_patched()