From 2cbe9af3998f298062298bea7df4b1b6b86c0730 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 4 Oct 2012 03:50:47 +0200
Subject: [PATCH] Factorize removing comments and empty lines from string

This will also be used for verifying the file storage directory.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Bernardo Dal Seno <bdalseno@google.com>
---
 lib/http/auth.py  |  9 ++-------
 lib/utils/text.py | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/lib/http/auth.py b/lib/http/auth.py
index 5a7bfac19..1e33f2cdb 100644
--- a/lib/http/auth.py
+++ b/lib/http/auth.py
@@ -29,6 +29,7 @@ import binascii
 
 from ganeti import compat
 from ganeti import http
+from ganeti import utils
 
 from cStringIO import StringIO
 
@@ -305,13 +306,7 @@ def ParsePasswordFile(contents):
   """
   users = {}
 
-  for line in contents.splitlines():
-    line = line.strip()
-
-    # Ignore empty lines and comments
-    if not line or line.startswith("#"):
-      continue
-
+  for line in utils.FilterEmptyLinesAndComments(contents):
     parts = line.split(None, 2)
     if len(parts) < 2:
       # Invalid line
diff --git a/lib/utils/text.py b/lib/utils/text.py
index f4c9adc14..3da8f8316 100644
--- a/lib/utils/text.py
+++ b/lib/utils/text.py
@@ -589,3 +589,21 @@ 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: generator
+
+  """
+  for line in text.splitlines():
+    line = line.strip()
+
+    # Ignore empty lines and comments
+    if line and not line.startswith("#"):
+      yield line
-- 
GitLab