comparison frontends/src/jp/cmd_roster.py @ 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
comparison
equal deleted inserted replaced
1796:314d2eb7fbaa 1797:40cda0c08727
33 33
34 def __init__(self, host): 34 def __init__(self, host):
35 super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription')) 35 super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription'))
36 36
37 def add_parser_options(self): 37 def add_parser_options(self):
38 pass 38 self.parser.add_argument("--no_from", action="store_true", help=_("Also purge contacts with no 'from' subscription"))
39 self.parser.add_argument("--no_to", action="store_true", help=_("Also purge contacts with no 'to' subscription"))
39 40
40 def connected(self): 41 def connected(self):
41 self.need_loop = True 42 self.need_loop = True
42 super(Purge, self).connected() 43 super(Purge, self).connected()
43 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error) 44 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
44 45
45 def error(self, failure): 46 def error(self, failure):
46 print (_("Error while retrieving the contacts [%s]") % failure) 47 print (_("Error while retrieving the contacts [%s]") % failure)
47 self.host.quit(1) 48 self.host.quit(1)
48 49
49 def ask_confirmation(self, to_remove): 50 def ask_confirmation(self, none, no_from, no_to):
50 """Ask the confirmation before removing contacts without subscription. 51 """Ask the confirmation before removing contacts.
51 52
52 @param to_remove (list[unicode]): list of contacts 53 @param none (list[unicode]): list of contacts with no subscription
54 @param no_from (list[unicode]): list of contacts with no 'from' subscription
55 @param no_to (list[unicode]): list of contacts with no 'to' subscription
53 @return bool 56 @return bool
54 """ 57 """
55 print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile 58 if none:
56 print " " + "\n ".join(to_remove) 59 print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile
60 print " " + "\n ".join(none)
61 if no_from:
62 print "There's no 'from' subscription between profile [%s] and the following contacts:" % self.host.profile
63 print " " + "\n ".join(no_from)
64 if no_to:
65 print "There's no 'to' subscription between profile [%s] and the following contacts:" % self.host.profile
66 print " " + "\n ".join(no_to)
57 message = "REMOVE them from profile [%s]'s roster" % self.host.profile 67 message = "REMOVE them from profile [%s]'s roster" % self.host.profile
58 while True: 68 while True:
59 res = raw_input("%s (y/N)? " % message) 69 res = raw_input("%s (y/N)? " % message)
60 if not res or res.lower() == 'n': 70 if not res or res.lower() == 'n':
61 return False 71 return False
65 def gotContacts(self, contacts): 75 def gotContacts(self, contacts):
66 """Process the list of contacts. 76 """Process the list of contacts.
67 77
68 @param contacts(list[tuple]): list of contacts with their attributes and groups 78 @param contacts(list[tuple]): list of contacts with their attributes and groups
69 """ 79 """
70 to_remove = [] 80 none, no_from, no_to = [], [], []
71 for contact, attrs, groups in contacts: 81 for contact, attrs, groups in contacts:
72 if not C.bool(attrs["from"]) and not C.bool(attrs["to"]): 82 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
73 to_remove.append(contact) 83 if not from_:
74 if not to_remove: 84 if not to:
85 none.append(contact)
86 elif self.args.no_from:
87 no_from.append(contact)
88 elif not to and self.args.no_to:
89 no_to.append(contact)
90 if not none and not no_from and not no_to:
75 print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile 91 print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile
76 elif self.ask_confirmation(to_remove): 92 elif self.ask_confirmation(none, no_from, no_to):
77 for contact in to_remove: 93 for contact in none + no_from + no_to:
78 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None) 94 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None)
79 self.host.quit() 95 self.host.quit()
80 96
81 97
82 class Stats(base.CommandBase): 98 class Stats(base.CommandBase):