pgreco / rpms / ipa

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

Blame SOURCES/0211-advise-add-an-infrastructure-for-formatting-Bash-com.patch

483b06
From 7e2702164e28576dfa64c0c9bbc83dc7dcb30ba7 Mon Sep 17 00:00:00 2001
483b06
From: Martin Babinsky <mbabinsk@redhat.com>
483b06
Date: Thu, 22 Jun 2017 15:00:00 +0200
483b06
Subject: [PATCH] advise: add an infrastructure for formatting Bash compound
483b06
 statements
483b06
483b06
A series of context managers simplify formatting of common compound
483b06
statements such as `if`, `else if`, `else` blocks.
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 | 73 ++++++++++++++++++++++++++++++++++++++++++++++++
483b06
 1 file changed, 73 insertions(+)
483b06
483b06
diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py
483b06
index c320b002c83198cbb0fd73a5c158df07dd309242..940d87ed4c1804326a46e2866381364e6f4f3f3e 100644
483b06
--- a/ipaserver/advise/base.py
483b06
+++ b/ipaserver/advise/base.py
483b06
@@ -128,6 +128,79 @@ class _IndentationTracker(object):
483b06
         self._recompute_indentation_level()
483b06
 
483b06
 
483b06
+class CompoundStatement(object):
483b06
+    """
483b06
+    Wrapper around indented blocks of Bash statements.
483b06
+
483b06
+    Override `begin_statement` and `end_statement` methods to issue
483b06
+    opening/closing commands using the passed in _AdviceOutput instance
483b06
+    """
483b06
+
483b06
+    def __init__(self, advice_output):
483b06
+        self.advice_output = advice_output
483b06
+
483b06
+    def __enter__(self):
483b06
+        self.begin_statement()
483b06
+        self.advice_output.indent()
483b06
+
483b06
+    def begin_statement(self):
483b06
+        pass
483b06
+
483b06
+    def __exit__(self, exc_type, exc_value, traceback):
483b06
+        self.advice_output.dedent()
483b06
+        self.end_statement()
483b06
+
483b06
+    def end_statement(self):
483b06
+        pass
483b06
+
483b06
+
483b06
+class IfBranch(CompoundStatement):
483b06
+    """
483b06
+    Base wrapper around `if` branch. The closing statement is empty so it
483b06
+    leaves trailing block that can be closed off or continued by else branches
483b06
+    """
483b06
+    def __init__(self, advice_output, conditional):
483b06
+        super(IfBranch, self).__init__(advice_output)
483b06
+        self.conditional = conditional
483b06
+
483b06
+    def begin_statement(self):
483b06
+        self.advice_output.command('if {}'.format(self.conditional))
483b06
+        self.advice_output.command('then')
483b06
+
483b06
+
483b06
+class ElseIfBranch(CompoundStatement):
483b06
+    """
483b06
+    Wrapper for `else if <CONDITIONAL>`
483b06
+    """
483b06
+    def __init__(self, advice_output, alternative_conditional):
483b06
+        super(ElseIfBranch, self).__init__(advice_output)
483b06
+        self.alternative_conditional = alternative_conditional
483b06
+
483b06
+    def begin_statement(self):
483b06
+        command = 'else if {}'.format(self.alternative_conditional)
483b06
+
483b06
+        self.advice_output.command(command)
483b06
+
483b06
+
483b06
+class ElseBranch(CompoundStatement):
483b06
+    """
483b06
+    Wrapper for final `else` block
483b06
+    """
483b06
+    def begin_statement(self):
483b06
+        self.advice_output.command('else')
483b06
+
483b06
+    def end_statement(self):
483b06
+        self.advice_output.command('fi')
483b06
+
483b06
+
483b06
+class UnbranchedIfStatement(IfBranch):
483b06
+    """
483b06
+    Plain `if` without branches
483b06
+    """
483b06
+    def end_statement(self):
483b06
+        self.advice_output.command('fi')
483b06
+
483b06
+
483b06
 class _AdviceOutput(object):
483b06
 
483b06
     def __init__(self):
483b06
-- 
483b06
2.9.4
483b06