From 160f430dd391c2e0de580f7d548b94e9e58366ac Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Mon, 24 Oct 2011 10:54:44 +0200 Subject: [PATCH] [entropy.tools] generic_file_content_parser: add encoding= support --- lib/entropy/tools.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/entropy/tools.py b/lib/entropy/tools.py index 2db1cf8ca..353f4115c 100644 --- a/lib/entropy/tools.py +++ b/lib/entropy/tools.py @@ -31,6 +31,8 @@ import traceback import gzip import bz2 import mmap +import codecs + from entropy.output import print_generic from entropy.const import etpConst, const_kill_threads, const_islive, \ const_isunicode, const_convert_to_unicode, const_convert_to_rawstring, \ @@ -1730,7 +1732,7 @@ def md5string(string): return m.hexdigest() def generic_file_content_parser(filepath, comment_tag = "#", - filter_comments = True): + filter_comments = True, encoding = None): """ Generic unix-style file content parser. Return a list of parsed lines with filtered comments. @@ -1748,8 +1750,12 @@ def generic_file_content_parser(filepath, comment_tag = "#", """ data = [] if os.access(filepath, os.R_OK) and os.path.isfile(filepath): - with open(filepath, "r") as gen_f: - content = gen_f.readlines() + if encoding is None: + with open(filepath, "r") as gen_f: + content = gen_f.readlines() + else: + with codecs.open(filepath, "r", encoding=encoding) as gen_f: + content = gen_f.readlines() # filter comments and white lines content = [x.strip().rsplit(comment_tag, 1)[0].strip() for x \ in content if x.strip()]