Mercurial > libervia-backend
view 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 |
line wrap: on
line source
#! /usr/bin/python # -*- coding: utf-8 -*- # jp: a SAT command line tool # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org) # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import base from sat_frontends.jp.constants import Const as C from sat.core.i18n import _ from twisted.words.protocols.jabber import jid from collections import OrderedDict __commands__ = ["Roster"] class Purge(base.CommandBase): def __init__(self, host): super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription')) def add_parser_options(self): 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 super(Purge, 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 ask_confirmation(self, none, no_from, no_to): """Ask the confirmation before removing 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 """ 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) if not res or res.lower() == 'n': return False if res.lower() == 'y': return True def gotContacts(self, contacts): """Process the list of contacts. @param contacts(list[tuple]): list of contacts with their attributes and groups """ none, no_from, no_to = [], [], [] for contact, attrs, groups in contacts: 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(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() class Stats(base.CommandBase): def __init__(self, host): super(Stats, self).__init__(host, 'stats', help=_('Display show statistics about a roster')) def add_parser_options(self): pass def connected(self): self.need_loop = True super(Stats, 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 """ hosts = {} no_subscription = 0 no_from_subscription = 0 no_to_subscription = 0 no_group = 0 total_group_subscription = 0 for contact, attrs, groups in contacts: from_, to = C.bool(attrs["from"]), C.bool(attrs["to"]) if not from_: if not to: no_subscription += 1 else: no_from_subscription += 1 elif not to: no_to_subscription += 1 host = jid.JID(contact).host hosts.setdefault(host, 0) hosts[host] += 1 if groups: total_group_subscription += len(groups) if not groups: no_group += 1 hosts = OrderedDict(sorted(hosts.items(), key=lambda item:-item[1])) print print "Total number of contacts: %d" % len(contacts) print for host, count in hosts.iteritems(): print "Contacts on {host}: {count} ({rate:.1f}%)".format(host=host, count=count, rate=100 * float(count) / len(contacts)) print print "Contacts with no 'from' subscription: %d" % no_from_subscription print "Contacts with no 'to' subscription: %d" % no_to_subscription print "Contacts with no subscription at all: %d" % no_subscription print print "Contacts not assigned to any group: %d" % no_group print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts)) self.host.quit() class Roster(base.CommandBase): subcommands = (Purge, Stats) def __init__(self, host): super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))