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