changeset 2431:a9fac72825a2

memory (disco): added findServiceEntity helper method to get first service of a kind
author Goffi <goffi@goffi.org>
date Wed, 15 Nov 2017 08:54:32 +0100
parents 3faf18111d61
children c6d859df4b27
files src/core/sat_main.py src/memory/disco.py
diffstat 2 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/sat_main.py	Sat Nov 11 18:39:26 2017 +0100
+++ b/src/core/sat_main.py	Wed Nov 15 08:54:32 2017 +0100
@@ -604,6 +604,9 @@
     def getDiscoItems(self, *args, **kwargs):
         return self.memory.disco.getItems(*args, **kwargs)
 
+    def findServiceEntity(self, *args, **kwargs):
+        return self.memory.disco.findServiceEntity(*args, **kwargs)
+
     def findServiceEntities(self, *args, **kwargs):
         return self.memory.disco.findServiceEntities(*args, **kwargs)
 
--- a/src/memory/disco.py	Sat Nov 11 18:39:26 2017 +0100
+++ b/src/memory/disco.py	Wed Nov 15 08:54:32 2017 +0100
@@ -230,6 +230,16 @@
         log.warning(_(u"Error while requesting [%(jid)s]: %(error)s") % {'jid': entity_jid.full(),
                                                                     'error': failure_.getErrorMessage()})
 
+    def findServiceEntity(self, client, category, type_, jid_=None):
+        """Helper method to find first available entity from findServiceEntities
+
+        args are the same as for [findServiceEntities]
+        @return (jid.JID, None): found entity
+        """
+        d = self.host.findServiceEntities(client, "pubsub", "service")
+        d.addCallback(lambda entities: entities.pop() if entities else None)
+        return d
+
     def findServiceEntities(self, client, category, type_, jid_=None):
         """Return all available items of an entity which correspond to (category, type_)