Skip to content

Commit

Permalink
Added context manager capability to WBEMSubscriptionManager; correcte…
Browse files Browse the repository at this point in the history
…d DSP1054 references in its docs.
  • Loading branch information
andy-maier committed Nov 21, 2016
1 parent 09ca9bf commit 4fd2f1d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Enhancements
response processing and indication processing This is a significant reduction
in memory usage. See issue # 498.

* Added enter and exit methods to `WBEMSubscriptionManager` to enable using it
as a context manager, whose exit method automatically cleans up by calling
`remove_all_servers()`.


Bug fixes
^^^^^^^^^
Expand Down
30 changes: 28 additions & 2 deletions pywbem/_subscription_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ def main():
subscription_manager.add_subscription(server2_id, filter2_path)
The following example code briefly shows how to use a subscription manager
as a context manager, in order to get automatic cleanup::
with WBEMSubscriptionManager('fred') as subscription_manager:
server1_id = subscription_manager.add_server(server1)
# exit method automatically calls remove_all_servers()
Another more practical example is in the script
``examples/pegIndicationTest.py`` (when you clone the GitHub pywbem/pywbem
project).
Expand Down Expand Up @@ -169,6 +176,9 @@ class WBEMSubscriptionManager(object):
"""
A class for managing subscriptions for CIM indications in a WBEM server.
The class may be used as a context manager, whose
:meth:`~pywbem.WBEMSubscriptionManager.__exit__` method will automatically
clean up.
"""

def __init__(self, subscription_manager_id=None):
Expand Down Expand Up @@ -226,6 +236,22 @@ def __repr__(self):
self._servers, self._owned_subscription_paths,
self._owned_filter_paths, self._owned_destination_paths)

def __enter__(self):
"""
Enter method when the class is used as a context manager.
"""
pass

def __exit__(self, exc_type, exc_value, traceback):
"""
Exit method when the class is used as a context manager.
It cleans up by calling
:meth:`~pywbem.WBEMSubscriptionManager.remove_all_servers`.
"""
self.remove_all_servers()
return False # re-raise any exceptions

def _get_server(self, server_id):
"""
Internal method to get the server object given a server_id.
Expand Down Expand Up @@ -546,7 +572,7 @@ def remove_destinations(self, server_id, destination_paths):
This method verifies that there are not currently any subscriptions on
the specified listener destination, in order to handle server
implementations that do not ensure that on the server side as required
by :ref:`DSP1054`.
by :term:`DSP1054`.
Parameters:
Expand Down Expand Up @@ -763,7 +789,7 @@ def remove_filter(self, server_id, filter_path):
This method verifies that there are not currently any subscriptions on
the specified indication filter, in order to handle server
implementations that do not ensure that on the server side as required
by :ref:`DSP1054`.
by :term:`DSP1054`.
Parameters:
Expand Down
31 changes: 31 additions & 0 deletions testsuite/run_cim_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3652,6 +3652,37 @@ def test_both_indications(self):

my_listener.stop()

def test_subscription_context_manager(self):
"""Test that the WBEMSUbscriptionmanager class can be used as a
Python context manager and that its exit method cleans up.
"""

if self.is_pegasus_test_build():

server = WBEMServer(self.conn)

# First, verify the behavior of remove_all_servers() with a
# normal WBEMSubscriptionManager instance.

sub_mgr = WBEMSubscriptionManager(
subscription_manager_id='test_ctxt_mgr_1')

server_id = sub_mgr.add_server(server)
self.assertEqual(set(sub_mgr._servers.keys()), {server_id})

sub_mgr.remove_all_servers()
self.assertEqual(set(sub_mgr._servers.keys()), {})

# Now, perform the actual test of the context manager.

with WBEMSubscriptionManager(
subscription_manager_id='test_ctxt_mgr_2') as sub_mgr:

server_id = sub_mgr.add_server(server)
self.assertEqual(set(sub_mgr._servers.keys()), {server_id})

self.assertEqual(set(sub_mgr._servers.keys()), {})


#################################################################
# Main function
Expand Down

0 comments on commit 4fd2f1d

Please sign in to comment.