utils.FilterEmptyLinesAndComments: Return list
[ganeti-local] / lib / utils / text.py
index f4c9adc..84de0bb 100644 (file)
@@ -589,3 +589,18 @@ def Truncate(text, length):
     return text
   else:
     return text[:length - len(_ASCII_ELLIPSIS)] + _ASCII_ELLIPSIS
+
+
+def FilterEmptyLinesAndComments(text):
+  """Filters empty lines and comments from a line-based string.
+
+  Whitespace is also removed from the beginning and end of all lines.
+
+  @type text: string
+  @param text: Input string
+  @rtype: list
+
+  """
+  return [line for line in map(lambda s: s.strip(), text.splitlines())
+          # Ignore empty lines and comments
+          if line and not line.startswith("#")]