From d1f24bce17563ddf0644cfc53a50a623b8354cf9 Mon Sep 17 00:00:00 2001 From: Daniel Kramer Date: Thu, 31 Mar 2022 22:19:55 -0700 Subject: [PATCH] Added context manager support obsws should now be able to be created with a with statement so the user doesn't have to worry about connecting/disconnecting --- obswebsocket/core.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/obswebsocket/core.py b/obswebsocket/core.py index c309c5d..b8940c4 100644 --- a/obswebsocket/core.py +++ b/obswebsocket/core.py @@ -211,6 +211,23 @@ def unregister(self, func, event=None): self.eventmanager.unregister(func, event) + def __enter__(self): + """ + Entry for a with statement + + :return: This object + """ + self.connect() + return self + + def __exit__(self): + """ + Exit for a with statement + + :return: Nothing + """ + self.disconnect() + class RecvThread(threading.Thread): def __init__(self, core):