Allow to have more than one notification type in Service
[gregoa/zavai.git] / zavai / registry.py
index 86198fd1ecccbcd8a7514122045bdeec735b1752..ec0a4a2c4f3b29c9bc497de4c6b1bedecc63bf77 100644 (file)
@@ -47,11 +47,14 @@ class Registry(object):
         self.objects = dict()
         self.labels = dict()
 
-    def register(self, name, obj):
+    def register(self, obj, name=None):
         """Register an object at the given path.
 
         Name the path to this object, like "menu.gps.monitor".
         """
+        if name is None:
+            name = obj.props.name
+
         if name in self.objects:
             return KeyError("%s is already registered", name)
         zavai.info("Registering", name)
@@ -60,7 +63,7 @@ class Registry(object):
         if name.startswith("menu."):
             self.add_to_menu(name)
 
-    def register_factory(self, name, fac, label = None):
+    def register_factory(self, fac, name, label = None):
         """Register an object factory at the given path.
 
         Name the path to this object, like "menu.gps.monitor".
@@ -129,7 +132,7 @@ class Registry(object):
                 parent = get_parent(name)
 
             res = zavai.Menu(self, name, parent)
-            self.register(name, res)
+            self.register(res, name)
         return res
 
     def shutdown(self):
@@ -142,6 +145,9 @@ class Registry(object):
         self.objects.clear()
 
 class Resource(object):
+    def __init__(self):
+        super(Resource, self).__init__()
+
     def shutdown(self):
         """Shut down this resource.
 
@@ -152,3 +158,49 @@ class Resource(object):
         releasing a FSO resource, restoring mixer settings and so on.
         """
         pass
+
+class Service(Resource):
+    "Service that is activated only when someone is listening"
+    def __init__(self):
+        """
+        Initialise a service that can emit signals for the given event types
+        """
+        super(Service, self).__init__(types = [])
+        self.callbacks = dict()
+        for t in types:
+            self.callbacks[t] = set()
+
+    def shutdown(self):
+        self.stop()
+
+    def start(self):
+        "Activate the service"
+        pass
+
+    def stop(self):
+        "Deactivate the service"
+        pass
+
+    def notify(self, type, *args, **kw):
+        "Call all callbacks with the given parameters"
+        for cb in self.callbacks[type]:
+            cb(*args, **kw)
+
+    def has_callbacks(self):
+        for i in self.callbacks.values():
+            if i: return True
+        return False
+
+    def connect(self, type, callback):
+        "Connect a callback to this resource, activating it if needed"
+        do_start = not self.has_callbacks()
+        self.callbacks[type].add(callback)
+        if do_start:
+            self.start()
+
+    def disconnect(self, type, callback):
+        "Disconnect a callback to this resource, activating it if needed"
+        if not self.has_callbacks(): return
+        self.callbacks.discard(callback)
+        if not self.has_callbacks():
+            self.stop()