Skip to content

Commit

Permalink
Open and Close Functions (#38)
Browse files Browse the repository at this point in the history
adding open and close functions
  • Loading branch information
benJrohrer authored Oct 12, 2018
1 parent f740368 commit 37710c5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cirrus/google_cloud/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def __init__(self, project_id=None, creds=None, use_default=True):
)
creds = creds or config.GOOGLE_APPLICATION_CREDENTIALS
self.credentials = ServiceAccountCredentials.from_service_account_file(creds)
# allows for open()/close() to be called multiple times without calling
# start up and shutdown code more than once
self._open_count = 0

def __enter__(self):
"""
Expand Down Expand Up @@ -153,6 +156,30 @@ def __exit__(self, exception_type, exception_value, traceback):
self._admin_service = None
self._storage_client = None

def open(self):
"""
Run initialization code in __enter__, but do not return self
Used for initializing GCM without using `with` block
Meant to allow for multiple calls to open/close, with only
opening and closing once.
"""
if self._open_count == 0:
self.__enter__()
self._open_count += 1

def close(self):
"""
Run cleanup code in __exit__
Used for closing GCM without using `with` block
"""
if self._open_count > 0:
self._open_count -= 1
if self._open_count == 0:
self._authed_session.close()
self._authed_session = None
self._admin_service = None
self._storage_client = None

def create_proxy_group_for_user(self, user_id, username, prefix=""):
"""
Creates a proxy group for the given user
Expand Down

0 comments on commit 37710c5

Please sign in to comment.