changeset 2550:1d754bc14381

jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
author Goffi <goffi@goffi.org>
date Sat, 31 Mar 2018 18:21:56 +0200
parents f685ad80ee98
children b27165bf160c
files frontends/src/jp/base.py frontends/src/jp/cmd_pubsub.py frontends/src/jp/constants.py
diffstat 3 files changed, 20 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/base.py	Sat Mar 31 17:20:38 2018 +0200
+++ b/frontends/src/jp/base.py	Sat Mar 31 18:21:56 2018 +0200
@@ -546,6 +546,14 @@
         except AttributeError:
             pass
 
+    def confirmOrQuit(self, message, cancel_message=_(u"action cancelled by user")):
+        """Request user to confirm action, and quit if he doesn't"""
+
+        res = raw_input("{} (y/N)? ".format(message))
+        if res not in ("y", "Y"):
+            self.disp(cancel_message)
+            self.quit(C.EXIT_USER_CANCELLED)
+
     def quitFromSignal(self, errcode=0):
         """Same as self.quit, but from a signal handler
 
--- a/frontends/src/jp/cmd_pubsub.py	Sat Mar 31 17:20:38 2018 +0200
+++ b/frontends/src/jp/cmd_pubsub.py	Sat Mar 31 18:21:56 2018 +0200
@@ -140,11 +140,7 @@
             else:
                 message = _(u"Are you sure to delete node [{node_id}] on service [{service}] ?").format(
                     node_id=self.args.node, service=self.args.service)
-
-            res = raw_input("{} (y/N)? ".format(message))
-            if res not in ("y", "Y"):
-                self.disp(_(u"node deletion cancelled"))
-                self.host.quit(2)
+            self.host.confirmOrQuit(message, _(u"node deletion cancelled"))
 
         self.host.bridge.psNodeDelete(
             self.args.service,
@@ -572,10 +568,7 @@
             self.parser.error(_(u"You need to specify an item to delete"))
         if not self.args.force:
             message = _(u"Are you sure to delete item {item_id} ?").format(item_id=self.args.item)
-            res = raw_input("{} (y/N)? ".format(message))
-            if res not in ("y", "Y"):
-                self.disp(_(u"Item deletion cancelled"))
-                self.host.quit(2)
+            self.host.confirmOrQuit(message, _(u"item deletion cancelled"))
         self.host.bridge.psRetractItem(
             self.args.service,
             self.args.node,
--- a/frontends/src/jp/constants.py	Sat Mar 31 17:20:38 2018 +0200
+++ b/frontends/src/jp/constants.py	Sat Mar 31 18:21:56 2018 +0200
@@ -59,12 +59,13 @@
 
     # exit codes
     EXIT_OK = 0
-    EXIT_ERROR = 1 # generic error, when nothing else match
-    EXIT_BAD_ARG = 2 # arguments given by user are bad
-    EXIT_BRIDGE_ERROR = 3 # can't connect to bridge
-    EXIT_BRIDGE_ERRBACK = 4 # something went wrong when calling a bridge method
-    EXIT_NOT_FOUND = 16 # an item required by a command was not found
-    EXIT_DATA_ERROR = 17 # data needed for a command is invalid
-    EXIT_FILE_NOT_EXE = 126 # a file to be executed was found, but it was not an executable utility (cf. man 1 exit)
-    EXIT_CMD_NOT_FOUND = 127 # a utility to be executed was not found (cf. man 1 exit)
-    EXIT_SIGNAL_INT = 128 # a command was interrupted by a signal (cf. man 1 exit)
+    EXIT_ERROR = 1  # generic error, when nothing else match
+    EXIT_BAD_ARG = 2  # arguments given by user are bad
+    EXIT_BRIDGE_ERROR = 3  # can't connect to bridge
+    EXIT_BRIDGE_ERRBACK = 4  # something went wrong when calling a bridge method
+    EXIT_NOT_FOUND = 16  # an item required by a command was not found
+    EXIT_DATA_ERROR = 17  # data needed for a command is invalid
+    EXIT_USER_CANCELLED = 20  # user cancelled action
+    EXIT_FILE_NOT_EXE = 126  # a file to be executed was found, but it was not an executable utility (cf. man 1 exit)
+    EXIT_CMD_NOT_FOUND = 127  # a utility to be executed was not found (cf. man 1 exit)
+    EXIT_SIGNAL_INT = 128  # a command was interrupted by a signal (cf. man 1 exit)