annotate frontends/src/jp/cmd_roster.py @ 1862:6d9c87bdc452

jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
author Goffi <goffi@goffi.org>
date Mon, 29 Feb 2016 16:52:51 +0100
parents 5f4d688d8b6e
children 96ba685162f6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
50 def ask_confirmation(self, no_sub, no_from, no_to):
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
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
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
53 @param no_sub (list[unicode]): list of contacts with no subscription
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
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 """
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
58 if no_sub:
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
59 print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
60 print " " + "\n ".join(no_sub)
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
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 """
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
80 no_sub, 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:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
85 no_sub.append(contact)
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
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)
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
90 if not no_sub 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
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
92 elif self.ask_confirmation(no_sub, no_from, no_to):
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
93 for contact in no_sub + 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):
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
101 super(Stats, self).__init__(host, 'stats', help=_('Show statistics about a roster'))
1796
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 = {}
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
121 unique_groups = set()
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
122 no_sub, no_from, no_to, no_group, total_group_subscription = 0, 0, 0, 0, 0
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
123 for contact, attrs, groups in contacts:
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
124 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
125 if not from_:
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
126 if not to:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
127 no_sub += 1
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
128 else:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
129 no_from += 1
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
130 elif not to:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
131 no_to += 1
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
132 host = jid.JID(contact).host
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
133 hosts.setdefault(host, 0)
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
134 hosts[host] += 1
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
135 if groups:
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
136 unique_groups.update(groups)
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
137 total_group_subscription += len(groups)
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
138 if not groups:
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
139 no_group += 1
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
140 hosts = OrderedDict(sorted(hosts.items(), key=lambda item:-item[1]))
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
141
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
142 print
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
143 print "Total number of contacts: %d" % len(contacts)
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
144 print "Number of different hosts: %d" % len(hosts)
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
145 print
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
146 for host, count in hosts.iteritems():
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
147 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
148 print
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
149 print "Contacts with no 'from' subscription: %d" % no_from
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
150 print "Contacts with no 'to' subscription: %d" % no_to
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
151 print "Contacts with no subscription at all: %d" % no_sub
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
152 print
1798
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
153 print "Total number of groups: %d" % len(unique_groups)
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
154 print "Average contacts per group: %.1f" % (float(total_group_subscription) / len(unique_groups))
c5d58387d031 jp (roster stats): add some extra information
souliane <souliane@mailoo.org>
parents: 1797
diff changeset
155 print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts))
1796
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
156 print "Contacts not assigned to any group: %d" % no_group
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
157 self.host.quit()
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
158
314d2eb7fbaa jp: add command "roster stats"
souliane <souliane@mailoo.org>
parents: 1793
diff changeset
159
1799
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
160 class Get(base.CommandBase):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
161
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
162 def __init__(self, host):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
163 super(Get, self).__init__(host, 'get', help=_('Retrieve the roster contacts'))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
164
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
165 def add_parser_options(self):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
166 self.parser.add_argument("--subscriptions", action="store_true", help=_("Show the contacts' subscriptions"))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
167 self.parser.add_argument("--groups", action="store_true", help=_("Show the contacts' groups"))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
168 self.parser.add_argument("--name", action="store_true", help=_("Show the contacts' names"))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
169
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
170 def connected(self):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
171 self.need_loop = True
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
172 super(Get, self).connected()
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
173 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
174
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
175 def error(self, failure):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
176 print (_("Error while retrieving the contacts [%s]") % failure)
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
177 self.host.quit(1)
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
178
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
179 def gotContacts(self, contacts):
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
180 """Process the list of contacts.
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
181
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
182 @param contacts(list[tuple]): list of contacts with their attributes and groups
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
183 """
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
184 field_count = 1 # only display the contact by default
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
185 if self.args.subscriptions:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
186 field_count += 3 # ask, from, to
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
187 if self.args.name:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
188 field_count += 1
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
189 if self.args.groups:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
190 field_count += 1
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
191 for contact, attrs, groups in contacts:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
192 args = [contact]
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
193 if self.args.subscriptions:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
194 args.append("ask" if C.bool(attrs["ask"]) else "")
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
195 args.append("from" if C.bool(attrs["from"]) else "")
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
196 args.append("to" if C.bool(attrs["to"]) else "")
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
197 if self.args.name:
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
198 args.append(unicode(attrs.get("name", "")))
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
199 if self.args.groups:
1800
5f4d688d8b6e jp (roster): change groups delimiter for "roster get" output, from "|" to "\t"
souliane <souliane@mailoo.org>
parents: 1799
diff changeset
200 args.append(u"\t".join(groups) if groups else "")
1799
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
201 print u";".join(["{}"] * field_count).format(*args).encode("utf-8")
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
202 self.host.quit()
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
203
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
204
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
205 class Roster(base.CommandBase):
1799
f1f4775f8cc0 jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
souliane <souliane@mailoo.org>
parents: 1798
diff changeset
206 subcommands = (Get, Stats, Purge)
1793
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
207
f39ca2832774 jp: add command "roster purge" to remove the contacts with no from/to subscription
souliane <souliane@mailoo.org>
parents:
diff changeset
208 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
209 super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))