From aa6d03ade25d489fcd3d726a79c0f7179a8f02c3 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Tue, 18 Jan 2022 17:42:17 +1100 Subject: [PATCH] You can now reset and restore your API owner settings. --- .../ifcopenshell/api/owner/settings.py | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/owner/settings.py b/src/ifcopenshell-python/ifcopenshell/api/owner/settings.py index 811d7d09f0..ce7e7f5968 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/owner/settings.py +++ b/src/ifcopenshell-python/ifcopenshell/api/owner/settings.py @@ -2,10 +2,51 @@ def get_application(ifc): - applications = ifc.by_type("IfcApplication") or [None] - return applications[0] + return (ifc.by_type("IfcApplication") or [None])[0] def get_user(ifc): - users = ifc.by_type("IfcPersonAndOrganization") or [None] - return users[0] + return (ifc.by_type("IfcPersonAndOrganization") or [None])[0] + + +get_application_factory = get_application +get_application_backup = get_application +get_user_factory = get_user +get_user_backup = get_user + + +def factory_reset(): + """Reset the get_user and get_application functions to what came out of box + + When you are developing a custom application, you will typically override + the get_user and get_application function to your own needs. Sometimes you + want to reset it to before you monkey-patched it. This function does that + reset. + """ + global get_application_factory + global get_application_backup + global get_application + global get_user_factory + global get_user_backup + global get_user + get_application_backup = get_application + get_application = get_application_factory + get_user_backup = get_user + get_user = get_user_factory + + +def restore(): + """Restore the get_user and get_application function prior to a reset + + In case you want to restore the monkey-patched version of get_user and + get_application that existed before you applied a factory_reset(), this + function will do that. + """ + global get_application_factory + global get_application_backup + global get_application + global get_user_factory + global get_user_backup + global get_user + get_application = get_application_backup + get_user = get_user_backup