[PATCH master] Factorize removing comments and empty lines from string

9 views
Skip to first unread message

Michael Hanselmann

unread,
Oct 3, 2012, 9:52:13 PM10/3/12
to ganeti...@googlegroups.com
This will also be used for verifying the file storage directory.
---
lib/http/auth.py | 9 ++-------
lib/utils/text.py | 16 ++++++++++++++++
2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/lib/http/auth.py b/lib/http/auth.py
index 5a7bfac..1e33f2c 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 f4c9adc..5785e80 100644
--- a/lib/utils/text.py
+++ b/lib/utils/text.py
@@ -589,3 +589,19 @@ 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.
+
+ @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
--
1.7.7.3

Bernardo Dal Seno

unread,
Oct 4, 2012, 7:28:21 AM10/4/12
to Michael Hanselmann, ganeti...@googlegroups.com
On Thu, Oct 4, 2012 at 3:52 AM, Michael Hanselmann <han...@google.com> wrote:
> This will also be used for verifying the file storage directory.
> ---
> lib/http/auth.py | 9 ++-------
> lib/utils/text.py | 16 ++++++++++++++++
> 2 files changed, 18 insertions(+), 7 deletions(-)

> + for line in text.splitlines():
> + line = line.strip()

This stripping is not advertised in the doc string. Please document
it, then LGTM.

Bernardo
Reply all
Reply to author
Forward
0 new messages