diff frontends/src/jp/cmd_roster.py @ 1799:f1f4775f8cc0

jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 20:51:42 +0100
parents c5d58387d031
children 5f4d688d8b6e
line wrap: on
line diff
--- a/frontends/src/jp/cmd_roster.py	Wed Jan 13 19:46:43 2016 +0100
+++ b/frontends/src/jp/cmd_roster.py	Wed Jan 13 20:51:42 2016 +0100
@@ -157,8 +157,54 @@
         self.host.quit()
 
 
+class Get(base.CommandBase):
+
+    def __init__(self, host):
+        super(Get, self).__init__(host, 'get', help=_('Retrieve the roster contacts'))
+
+    def add_parser_options(self):
+        self.parser.add_argument("--subscriptions", action="store_true", help=_("Show the contacts' subscriptions"))
+        self.parser.add_argument("--groups", action="store_true", help=_("Show the contacts' groups"))
+        self.parser.add_argument("--name", action="store_true", help=_("Show the contacts' names"))
+
+    def connected(self):
+        self.need_loop = True
+        super(Get, self).connected()
+        self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
+
+    def error(self, failure):
+        print (_("Error while retrieving the contacts [%s]") % failure)
+        self.host.quit(1)
+
+    def gotContacts(self, contacts):
+        """Process the list of contacts.
+
+        @param contacts(list[tuple]): list of contacts with their attributes and groups
+        """
+        field_count = 1  # only display the contact by default
+        if self.args.subscriptions:
+            field_count += 3  # ask, from, to
+        if self.args.name:
+            field_count += 1
+        if self.args.groups:
+            field_count += 1
+        for contact, attrs, groups in contacts:
+            args = [contact]
+            if self.args.subscriptions:
+                args.append("ask" if C.bool(attrs["ask"]) else "")
+                args.append("from" if C.bool(attrs["from"]) else "")
+                args.append("to" if C.bool(attrs["to"]) else "")
+            if self.args.name:
+                args.append(unicode(attrs.get("name", "")))
+            if self.args.groups:
+                # FIXME: pipe character could also be in a group name
+                args.append(u"|".join(groups) if groups else "")
+            print u";".join(["{}"] * field_count).format(*args).encode("utf-8")
+        self.host.quit()
+
+
 class Roster(base.CommandBase):
-    subcommands = (Purge, Stats)
+    subcommands = (Get, Stats, Purge)
 
     def __init__(self, host):
         super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))