Merge branch 'master' into autoadd

This commit is contained in:
Chase Sterling 2010-03-25 17:56:20 -04:00
commit 1cfc4f522e
2 changed files with 45 additions and 39 deletions

View File

@ -42,31 +42,43 @@ class ComponentAlreadyRegistered(Exception):
class Component(object): class Component(object):
""" """
Component objects are singletons managed by the `:class:ComponentRegistry`. Component objects are singletons managed by the :class:`ComponentRegistry`.
When a new Component object is instantiated, it will be automatically When a new Component object is instantiated, it will be automatically
registered with the `:class:ComponentRegistry`. registered with the :class:`ComponentRegistry`.
The ComponentRegistry has the ability to start, stop, pause and shutdown the The ComponentRegistry has the ability to start, stop, pause and shutdown the
components registered with it. components registered with it.
Events: **Events:**
start() - This method is called when the client has connected to a **start()** - This method is called when the client has connected to a
Deluge core. Deluge core.
stop() - This method is called when the client has disconnected from a **stop()** - This method is called when the client has disconnected from a
Deluge core. Deluge core.
update() - This method is called every 1 second by default while the **update()** - This method is called every 1 second by default while the
Componented is in a "Started" state. The interval can be Componented is in a *Started* state. The interval can be
specified during instantiation. The update() timer can be specified during instantiation. The update() timer can be
paused by instructing the `:class:ComponentRegistry` to pause paused by instructing the :class:`ComponentRegistry` to pause
this Component. this Component.
shutdown() - This method is called when the client is exiting. If the **shutdown()** - This method is called when the client is exiting. If the
Component is in a "Started" state when this is called, a Component is in a "Started" state when this is called, a
call to stop() will be issued prior to shutdown(). call to stop() will be issued prior to shutdown().
**States:**
A Component can be in one of these 3 states.
**Started** - The Component has been started by the :class:`ComponentRegistry`
and will have it's update timer started.
**Stopped** - The Component has either been stopped or has yet to be started.
**Paused** - The Component has had it's update timer stopped, but will
still be considered in a Started state.
""" """
def __init__(self, name, interval=1, depend=None): def __init__(self, name, interval=1, depend=None):
self._component_name = name self._component_name = name
@ -158,7 +170,7 @@ class Component(object):
class ComponentRegistry(object): class ComponentRegistry(object):
""" """
The ComponentRegistry holds a list of currently registered The ComponentRegistry holds a list of currently registered
`:class:Component` objects. It is used to manage the Components by :class:`Component` objects. It is used to manage the Components by
starting, stopping, pausing and shutting them down. starting, stopping, pausing and shutting them down.
""" """
def __init__(self): def __init__(self):
@ -172,8 +184,7 @@ class ComponentRegistry(object):
:param obj: the Component object :param obj: the Component object
:type obj: object :type obj: object
:raises ComponentAlreadyRegistered: if a component with the same name :raises ComponentAlreadyRegistered: if a component with the same name is already registered.
is already registered.
""" """
name = obj._component_name name = obj._component_name
@ -205,15 +216,14 @@ class ComponentRegistry(object):
def start(self, names=[]): def start(self, names=[]):
""" """
Starts Components that are currently in a Stopped state and their Starts Components that are currently in a Stopped state and their
dependencies. If `:param:names` is specified, will only start those dependencies. If *names* is specified, will only start those
Components and their dependencies and if not it will start all Components and their dependencies and if not it will start all
registered components. registered components.
:param names: a list of Components to start :param names: a list of Components to start
:type names: list :type names: list
:returns: a Deferred object that will fire once all Components have been :returns: a Deferred object that will fire once all Components have been sucessfully started
sucessfully started
:rtype: twisted.internet.defer.Deferred :rtype: twisted.internet.defer.Deferred
""" """
@ -242,30 +252,20 @@ class ComponentRegistry(object):
def stop(self, names=[]): def stop(self, names=[]):
""" """
Stops Components that are currently not in a Stopped state. If Stops Components that are currently not in a Stopped state. If
`:param:names` is specified, then it will only stop those Components, *names* is specified, then it will only stop those Components,
and if not it will stop all the registered Components. and if not it will stop all the registered Components.
:param names: a list of Components to start :param names: a list of Components to start
:type names: list :type names: list
:returns: a Deferred object that will fire once all Components have been :returns: a Deferred object that will fire once all Components have been sucessfully stopped
sucessfully stopped
:rtype: twisted.internet.defer.Deferred :rtype: twisted.internet.defer.Deferred
:raises KeyError: if a component name is not registered
""" """
if not names: if not names:
names = self.components.keys() names = self.components.keys()
elif isinstance(names, str): elif isinstance(names, str):
if names in self.components:
names = [names] names = [names]
else:
raise KeyError("%s is not a registered component!" % names)
for name in names:
if name not in self.components:
raise KeyError("%s is not a registered component!" % name)
deferreds = [] deferreds = []
@ -278,14 +278,13 @@ class ComponentRegistry(object):
def pause(self, names=[]): def pause(self, names=[]):
""" """
Pauses Components that are currently in a Started state. If Pauses Components that are currently in a Started state. If
`:param:names` is specified, then it will only pause those Components, *names* is specified, then it will only pause those Components,
and if not it will pause all the registered Components. and if not it will pause all the registered Components.
:param names: a list of Components to pause :param names: a list of Components to pause
:type names: list :type names: list
:returns: a Deferred object that will fire once all Components have been :returns: a Deferred object that will fire once all Components have been sucessfully paused
sucessfully paused
:rtype: twisted.internet.defer.Deferred :rtype: twisted.internet.defer.Deferred
""" """
@ -305,14 +304,13 @@ class ComponentRegistry(object):
def resume(self, names=[]): def resume(self, names=[]):
""" """
Resumes Components that are currently in a Paused state. If Resumes Components that are currently in a Paused state. If
`:param:names` is specified, then it will only resume those Components, *names* is specified, then it will only resume those Components,
and if not it will resume all the registered Components. and if not it will resume all the registered Components.
:param names: a list of Components to resume :param names: a list of Components to resume
:type names: list :type names: list
:returns: a Deferred object that will fire once all Components have been :returns: a Deferred object that will fire once all Components have been sucessfully resumed
sucessfully resumed
:rtype: twisted.internet.defer.Deferred :rtype: twisted.internet.defer.Deferred
""" """
@ -332,12 +330,11 @@ class ComponentRegistry(object):
def shutdown(self): def shutdown(self):
""" """
Shutdowns all Components regardless of state. This will call Shutdowns all Components regardless of state. This will call
`:meth:stop` on call the components prior to shutting down. This should :meth:`stop` on call the components prior to shutting down. This should
be called when the program is exiting to ensure all Components have a be called when the program is exiting to ensure all Components have a
chance to properly shutdown. chance to properly shutdown.
:returns: a Deferred object that will fire once all Components have been :returns: a Deferred object that will fire once all Components have been sucessfully resumed
sucessfully resumed
:rtype: twisted.internet.defer.Deferred :rtype: twisted.internet.defer.Deferred
""" """

View File

@ -4,13 +4,22 @@ import common
from deluge.core.alertmanager import AlertManager from deluge.core.alertmanager import AlertManager
from deluge.core.core import Core from deluge.core.core import Core
import deluge.component as component
class AlertManagerTestCase(unittest.TestCase): class AlertManagerTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.core = Core() self.core = Core()
self.am = AlertManager() self.am = component.get("AlertManager")
self.am.start() component.start(["AlertManager"])
def tearDown(self):
def on_shutdown(result):
component._ComponentRegistry.components = {}
del self.am
del self.core
return component.shutdown().addCallback(on_shutdown)
def test_register_handler(self): def test_register_handler(self):
def handler(alert): def handler(alert):