pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0212-delegate-formatting-of-compound-Bash-statements-to-d.patch

483b06
From d22e7953295f878950ca5be976d89bf9af8d36b1 Mon Sep 17 00:00:00 2001
483b06
From: Martin Babinsky <mbabinsk@redhat.com>
483b06
Date: Thu, 22 Jun 2017 15:02:25 +0200
483b06
Subject: [PATCH] delegate formatting of compound Bash statements to dedicated
483b06
 classes
483b06
483b06
this simplifies handling compound statements using _AdviceOutput class.
483b06
The necessary statements are exposed as context managers and API for
483b06
most common constructs is provided.
483b06
483b06
https://pagure.io/freeipa/issue/7036
483b06
483b06
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
483b06
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
483b06
---
483b06
 ipaserver/advise/base.py | 48 ++++++++++++++++++++++++++++++++++--------------
483b06
 1 file changed, 34 insertions(+), 14 deletions(-)
483b06
483b06
diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py
483b06
index 940d87ed4c1804326a46e2866381364e6f4f3f3e..581478fb75bc4f50b6bffe2e4cf9b51de46fa095 100644
483b06
--- a/ipaserver/advise/base.py
483b06
+++ b/ipaserver/advise/base.py
483b06
@@ -286,33 +286,53 @@ class _AdviceOutput(object):
483b06
         )
483b06
 
483b06
     def exit_on_predicate(self, predicate, error_message_lines):
483b06
-        commands_to_run = [
483b06
-            self._format_error(error_message_line)
483b06
-            for error_message_line in error_message_lines]
483b06
+        with self.unbranched_if(predicate):
483b06
+            for error_message_line in error_message_lines:
483b06
+                self.command(self._format_error(error_message_line))
483b06
 
483b06
-        commands_to_run.append('exit 1')
483b06
-        self.commands_on_predicate(
483b06
-            predicate,
483b06
-            commands_to_run)
483b06
+            self.command('exit 1')
483b06
+
483b06
+    @contextmanager
483b06
+    def unbranched_if(self, predicate):
483b06
+        with self._compound_statement(UnbranchedIfStatement, predicate):
483b06
+            yield
483b06
+
483b06
+    @contextmanager
483b06
+    def _compound_statement(self, statement_cls, *args):
483b06
+        with statement_cls(self, *args):
483b06
+            yield
483b06
 
483b06
     def commands_on_predicate(self, predicate, commands_to_run_when_true,
483b06
                               commands_to_run_when_false=None):
483b06
-        if_command = 'if {}'.format(predicate)
483b06
-        self.command(if_command)
483b06
-        self.command('then')
483b06
+        if commands_to_run_when_false is not None:
483b06
+            if_statement = self.if_branch
483b06
+        else:
483b06
+            if_statement = self.unbranched_if
483b06
 
483b06
-        with self.indented_block():
483b06
+        with if_statement(predicate):
483b06
             for command_to_run_when_true in commands_to_run_when_true:
483b06
                 self.command(
483b06
                     command_to_run_when_true)
483b06
 
483b06
         if commands_to_run_when_false is not None:
483b06
-            self.command("else")
483b06
-            with self.indented_block():
483b06
+            with self.else_branch():
483b06
                 for command_to_run_when_false in commands_to_run_when_false:
483b06
                     self.command(command_to_run_when_false)
483b06
 
483b06
-        self.command('fi')
483b06
+    @contextmanager
483b06
+    def if_branch(self, predicate):
483b06
+        with self._compound_statement(IfBranch, predicate):
483b06
+            yield
483b06
+
483b06
+    @contextmanager
483b06
+    def else_branch(self):
483b06
+        with self._compound_statement(ElseBranch):
483b06
+            yield
483b06
+
483b06
+    @contextmanager
483b06
+    def else_if_branch(self, predicate):
483b06
+        with self._compound_statement(ElseIfBranch, predicate):
483b06
+            yield
483b06
 
483b06
 
483b06
 class Advice(Plugin):
483b06
-- 
483b06
2.9.4
483b06