b38368
From bdf024c20213110306b2fcf3651f274c229aae29 Mon Sep 17 00:00:00 2001
b38368
From: Martin Babinsky <mbabinsk@redhat.com>
b38368
Date: Thu, 22 Jun 2017 13:18:54 +0200
b38368
Subject: [PATCH] add a class that tracks the indentation in the generated
b38368
 advises
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 | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
b38368
 1 file changed, 49 insertions(+)
b38368
b38368
diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py
b38368
index ba412b872472580cd32baf2a326a14edb951cab1..639fd1807f72f11f46136999c4ce4c6eec6b3698 100644
b38368
--- a/ipaserver/advise/base.py
b38368
+++ b/ipaserver/advise/base.py
b38368
@@ -76,6 +76,55 @@ As a result, you can redirect the advice's output directly to a script file.
b38368
 """
b38368
 
b38368
 
b38368
+class _IndentationTracker(object):
b38368
+    """
b38368
+    A simple wrapper that tracks the indentation level of the generated bash
b38368
+    commands
b38368
+    """
b38368
+    def __init__(self, spaces_per_indent=0):
b38368
+        if spaces_per_indent <= 0:
b38368
+            raise ValueError(
b38368
+                "Indentation increments cannot be zero or negative")
b38368
+        self.spaces_per_indent = spaces_per_indent
b38368
+        self._indentation_stack = []
b38368
+        self._total_indentation_level = 0
b38368
+
b38368
+    @property
b38368
+    def indentation_string(self):
b38368
+        """
b38368
+        return a string containing number of spaces corresponding to
b38368
+        indentation level
b38368
+        """
b38368
+        return " " * self._total_indentation_level
b38368
+
b38368
+    def indent(self):
b38368
+        """
b38368
+        track a single indentation of the generated code
b38368
+        """
b38368
+        self._indentation_stack.append(self.spaces_per_indent)
b38368
+        self._recompute_indentation_level()
b38368
+
b38368
+    def _recompute_indentation_level(self):
b38368
+        """
b38368
+        Track total indentation level of the generated code
b38368
+        """
b38368
+        self._total_indentation_level = sum(self._indentation_stack)
b38368
+
b38368
+    def dedent(self):
b38368
+        """
b38368
+        track a single dedentation of the generated code
b38368
+        dedents that would result in zero or negative indentation level will be
b38368
+        ignored
b38368
+        """
b38368
+        try:
b38368
+            self._indentation_stack.pop()
b38368
+        except IndexError:
b38368
+            # can not dedent any further
b38368
+            pass
b38368
+
b38368
+        self._recompute_indentation_level()
b38368
+
b38368
+
b38368
 class _AdviceOutput(object):
b38368
 
b38368
     def __init__(self):
b38368
-- 
b38368
2.9.4
b38368