comparison frontends/src/jp/cmd_roster.py @ 1799:f1f4775f8cc0

jp (roster): add command "roster get" with parameters "--subscriptions", "--name" and "--groups"
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 20:51:42 +0100
parents c5d58387d031
children 5f4d688d8b6e
comparison
equal deleted inserted replaced
1798:c5d58387d031 1799:f1f4775f8cc0
155 print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts)) 155 print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts))
156 print "Contacts not assigned to any group: %d" % no_group 156 print "Contacts not assigned to any group: %d" % no_group
157 self.host.quit() 157 self.host.quit()
158 158
159 159
160 class Get(base.CommandBase):
161
162 def __init__(self, host):
163 super(Get, self).__init__(host, 'get', help=_('Retrieve the roster contacts'))
164
165 def add_parser_options(self):
166 self.parser.add_argument("--subscriptions", action="store_true", help=_("Show the contacts' subscriptions"))
167 self.parser.add_argument("--groups", action="store_true", help=_("Show the contacts' groups"))
168 self.parser.add_argument("--name", action="store_true", help=_("Show the contacts' names"))
169
170 def connected(self):
171 self.need_loop = True
172 super(Get, self).connected()
173 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
174
175 def error(self, failure):
176 print (_("Error while retrieving the contacts [%s]") % failure)
177 self.host.quit(1)
178
179 def gotContacts(self, contacts):
180 """Process the list of contacts.
181
182 @param contacts(list[tuple]): list of contacts with their attributes and groups
183 """
184 field_count = 1 # only display the contact by default
185 if self.args.subscriptions:
186 field_count += 3 # ask, from, to
187 if self.args.name:
188 field_count += 1
189 if self.args.groups:
190 field_count += 1
191 for contact, attrs, groups in contacts:
192 args = [contact]
193 if self.args.subscriptions:
194 args.append("ask" if C.bool(attrs["ask"]) else "")
195 args.append("from" if C.bool(attrs["from"]) else "")
196 args.append("to" if C.bool(attrs["to"]) else "")
197 if self.args.name:
198 args.append(unicode(attrs.get("name", "")))
199 if self.args.groups:
200 # FIXME: pipe character could also be in a group name
201 args.append(u"|".join(groups) if groups else "")
202 print u";".join(["{}"] * field_count).format(*args).encode("utf-8")
203 self.host.quit()
204
205
160 class Roster(base.CommandBase): 206 class Roster(base.CommandBase):
161 subcommands = (Purge, Stats) 207 subcommands = (Get, Stats, Purge)
162 208
163 def __init__(self, host): 209 def __init__(self, host):
164 super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster")) 210 super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))