diff sat_frontends/jp/cmd_roster.py @ 3255:012e89fb2dd1

jp (roster): new roster/set command
author Goffi <goffi@goffi.org>
date Tue, 14 Apr 2020 21:01:04 +0200
parents 559a625a236b
children f30b238d9c45
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_roster.py	Tue Apr 14 21:00:33 2020 +0200
+++ b/sat_frontends/jp/cmd_roster.py	Tue Apr 14 21:01:04 2020 +0200
@@ -1,7 +1,6 @@
 #!/usr/bin/env python3
 
-
-# jp: a SAT command line tool
+# jp: a SàT command line tool
 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
 # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org)
 
@@ -84,6 +83,48 @@
         self.host.quit()
 
 
+class Set(base.CommandBase):
+
+    def __init__(self, host):
+        super().__init__(host, 'set', help=_('set metadata for a roster entity'))
+
+    def add_parser_options(self):
+        self.parser.add_argument(
+            "-n", "--name", default="", help=_('name to use for this entity'))
+        self.parser.add_argument(
+            "-g", "--group", dest='groups', action='append', metavar='GROUP', default=[],
+            help=_('groups for this entity'))
+        self.parser.add_argument(
+            "-R", "--replace", action="store_true",
+            help=_("replace all metadata instead of adding them"))
+        self.parser.add_argument(
+            "jid", help=_("jid of the roster entity"))
+
+    async def start(self):
+
+        if self.args.replace:
+            name = self.args.name
+            groups = self.args.groups
+        else:
+            try:
+                entity_data = await self.host.bridge.contactGet(
+                    self.args.jid, self.host.profile)
+            except Exception as e:
+                self.disp(f"error while retrieving the contact: {e}", error=True)
+                self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+            name = self.args.name or entity_data[0].get('name') or ''
+            groups = set(entity_data[1])
+            groups = list(groups.union(self.args.groups))
+
+        try:
+            await self.host.bridge.updateContact(
+                self.args.jid, name, groups, self.host.profile)
+        except Exception as e:
+            self.disp(f"error while updating the contact: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        self.host.quit()
+
+
 class Stats(base.CommandBase):
 
     def __init__(self, host):
@@ -252,7 +293,7 @@
 
 
 class Roster(base.CommandBase):
-    subcommands = (Get, Stats, Purge, Resync)
+    subcommands = (Get, Set, Stats, Purge, Resync)
 
     def __init__(self, host):
         super(Roster, self).__init__(