changeset 1797:40cda0c08727

jp (roster): add parameters "--no_from" and "--no_to" to command "roster purge" in order to remove the contacts with no from/to subscription
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 19:26:09 +0100
parents 314d2eb7fbaa
children c5d58387d031
files frontends/src/jp/cmd_roster.py
diffstat 1 files changed, 28 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_roster.py	Wed Jan 13 18:54:58 2016 +0100
+++ b/frontends/src/jp/cmd_roster.py	Wed Jan 13 19:26:09 2016 +0100
@@ -35,7 +35,8 @@
         super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription'))
 
     def add_parser_options(self):
-        pass
+        self.parser.add_argument("--no_from", action="store_true", help=_("Also purge contacts with no 'from' subscription"))
+        self.parser.add_argument("--no_to", action="store_true", help=_("Also purge contacts with no 'to' subscription"))
 
     def connected(self):
         self.need_loop = True
@@ -46,14 +47,23 @@
         print (_("Error while retrieving the contacts [%s]") % failure)
         self.host.quit(1)
 
-    def ask_confirmation(self, to_remove):
-        """Ask the confirmation before removing contacts without subscription.
+    def ask_confirmation(self, none, no_from, no_to):
+        """Ask the confirmation before removing contacts.
 
-        @param to_remove (list[unicode]): list of contacts
+        @param none (list[unicode]): list of contacts with no subscription
+        @param no_from (list[unicode]): list of contacts with no 'from' subscription
+        @param no_to (list[unicode]): list of contacts with no 'to' subscription
         @return bool
         """
-        print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile
-        print "    " + "\n    ".join(to_remove)
+        if none:
+            print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile
+            print "    " + "\n    ".join(none)
+        if no_from:
+            print "There's no 'from' subscription between profile [%s] and the following contacts:" % self.host.profile
+            print "    " + "\n    ".join(no_from)
+        if no_to:
+            print "There's no 'to' subscription between profile [%s] and the following contacts:" % self.host.profile
+            print "    " + "\n    ".join(no_to)
         message = "REMOVE them from profile [%s]'s roster" % self.host.profile
         while True:
             res = raw_input("%s (y/N)? " % message)
@@ -67,14 +77,20 @@
 
         @param contacts(list[tuple]): list of contacts with their attributes and groups
         """
-        to_remove = []
+        none, no_from, no_to = [], [], []
         for contact, attrs, groups in contacts:
-            if not C.bool(attrs["from"]) and not C.bool(attrs["to"]):
-                to_remove.append(contact)
-        if not to_remove:
+            from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
+            if not from_:
+                if not to:
+                    none.append(contact)
+                elif self.args.no_from:
+                    no_from.append(contact)
+            elif not to and self.args.no_to:
+                no_to.append(contact)
+        if not none and not no_from and not no_to:
             print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile
-        elif self.ask_confirmation(to_remove):
-            for contact in to_remove:
+        elif self.ask_confirmation(none, no_from, no_to):
+            for contact in none + no_from + no_to:
                 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None)
         self.host.quit()