Mercurial > libervia-backend
annotate 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 |
rev | line source |
---|---|
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
1 #! /usr/bin/python |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
3 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
4 # jp: a SAT command line tool |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
6 # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org) |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
7 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
8 # This program is free software: you can redistribute it and/or modify |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
9 # it under the terms of the GNU Affero General Public License as published by |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
10 # the Free Software Foundation, either version 3 of the License, or |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
11 # (at your option) any later version. |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
12 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
13 # This program is distributed in the hope that it will be useful, |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
16 # GNU Affero General Public License for more details. |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
17 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
18 # You should have received a copy of the GNU Affero General Public License |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
20 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
21 import base |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
22 from sat_frontends.jp.constants import Const as C |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
23 from sat.core.i18n import _ |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
24 |
1796
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
25 from twisted.words.protocols.jabber import jid |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
26 from collections import OrderedDict |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
27 |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
28 __commands__ = ["Roster"] |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
29 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
30 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
31 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
32 class Purge(base.CommandBase): |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
33 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
34 def __init__(self, host): |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
35 super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription')) |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
36 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
37 def add_parser_options(self): |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
38 self.parser.add_argument("--no_from", action="store_true", help=_("Also purge contacts with no 'from' subscription")) |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
39 self.parser.add_argument("--no_to", action="store_true", help=_("Also purge contacts with no 'to' subscription")) |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
40 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
41 def connected(self): |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
42 self.need_loop = True |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
43 super(Purge, self).connected() |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
44 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error) |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
45 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
46 def error(self, failure): |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
47 print (_("Error while retrieving the contacts [%s]") % failure) |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
48 self.host.quit(1) |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
49 |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
50 def ask_confirmation(self, none, no_from, no_to): |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
51 """Ask the confirmation before removing contacts. |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
52 |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
53 @param none (list[unicode]): list of contacts with no subscription |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
54 @param no_from (list[unicode]): list of contacts with no 'from' subscription |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
55 @param no_to (list[unicode]): list of contacts with no 'to' subscription |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
56 @return bool |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
57 """ |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
58 if none: |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
59 print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
60 print " " + "\n ".join(none) |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
61 if no_from: |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
62 print "There's no 'from' subscription between profile [%s] and the following contacts:" % self.host.profile |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
63 print " " + "\n ".join(no_from) |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
64 if no_to: |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
65 print "There's no 'to' subscription between profile [%s] and the following contacts:" % self.host.profile |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
66 print " " + "\n ".join(no_to) |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
67 message = "REMOVE them from profile [%s]'s roster" % self.host.profile |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
68 while True: |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
69 res = raw_input("%s (y/N)? " % message) |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
70 if not res or res.lower() == 'n': |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
71 return False |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
72 if res.lower() == 'y': |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
73 return True |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
74 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
75 def gotContacts(self, contacts): |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
76 """Process the list of contacts. |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
77 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
78 @param contacts(list[tuple]): list of contacts with their attributes and groups |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
79 """ |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
80 none, no_from, no_to = [], [], [] |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
81 for contact, attrs, groups in contacts: |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
82 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"]) |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
83 if not from_: |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
84 if not to: |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
85 none.append(contact) |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
86 elif self.args.no_from: |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
87 no_from.append(contact) |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
88 elif not to and self.args.no_to: |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
89 no_to.append(contact) |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
90 if not none and not no_from and not no_to: |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
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 |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
92 elif self.ask_confirmation(none, no_from, no_to): |
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
souliane <souliane@mailoo.org>
parents:
1796
diff
changeset
|
93 for contact in none + no_from + no_to: |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
94 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None) |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
95 self.host.quit() |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
96 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
97 |
1796
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
98 class Stats(base.CommandBase): |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
99 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
100 def __init__(self, host): |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
101 super(Stats, self).__init__(host, 'stats', help=_('Display show statistics about a roster')) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
102 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
103 def add_parser_options(self): |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
104 pass |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
105 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
106 def connected(self): |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
107 self.need_loop = True |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
108 super(Stats, self).connected() |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
109 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
110 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
111 def error(self, failure): |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
112 print (_("Error while retrieving the contacts [%s]") % failure) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
113 self.host.quit(1) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
114 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
115 def gotContacts(self, contacts): |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
116 """Process the list of contacts. |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
117 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
118 @param contacts(list[tuple]): list of contacts with their attributes and groups |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
119 """ |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
120 hosts = {} |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
121 no_subscription = 0 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
122 no_from_subscription = 0 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
123 no_to_subscription = 0 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
124 no_group = 0 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
125 total_group_subscription = 0 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
126 for contact, attrs, groups in contacts: |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
127 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"]) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
128 if not from_: |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
129 if not to: |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
130 no_subscription += 1 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
131 else: |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
132 no_from_subscription += 1 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
133 elif not to: |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
134 no_to_subscription += 1 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
135 host = jid.JID(contact).host |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
136 hosts.setdefault(host, 0) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
137 hosts[host] += 1 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
138 if groups: |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
139 total_group_subscription += len(groups) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
140 if not groups: |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
141 no_group += 1 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
142 hosts = OrderedDict(sorted(hosts.items(), key=lambda item:-item[1])) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
143 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
144 print |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
145 print "Total number of contacts: %d" % len(contacts) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
146 print |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
147 for host, count in hosts.iteritems(): |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
148 print "Contacts on {host}: {count} ({rate:.1f}%)".format(host=host, count=count, rate=100 * float(count) / len(contacts)) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
149 print |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
150 print "Contacts with no 'from' subscription: %d" % no_from_subscription |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
151 print "Contacts with no 'to' subscription: %d" % no_to_subscription |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
152 print "Contacts with no subscription at all: %d" % no_subscription |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
153 print |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
154 print "Contacts not assigned to any group: %d" % no_group |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
155 print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts)) |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
156 self.host.quit() |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
157 |
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
158 |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
159 class Roster(base.CommandBase): |
1796
314d2eb7fbaa
jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents:
1793
diff
changeset
|
160 subcommands = (Purge, Stats) |
1793
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
161 |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
162 def __init__(self, host): |
f39ca2832774
jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
163 super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster")) |