From c14ad43f8c6ba9cabe1bb11cad859c8da6dc54ca Mon Sep 17 00:00:00 2001 From: VG Date: Fri, 11 Dec 2015 14:56:40 +0100 Subject: include a custom conf parser --- confparser.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 confparser.py (limited to 'confparser.py') diff --git a/confparser.py b/confparser.py new file mode 100644 index 0000000..e43443b --- /dev/null +++ b/confparser.py @@ -0,0 +1,27 @@ +#!python3 + +__all__ = ['read_conf'] + +import csv + +class conf_dialect(csv.Dialect): + delimiter = '=' + quotechar = '"' + escapechar = '\\' + doublequote = True + skipinitialspace = True + lineterminator = '\n' + quoting = csv.QUOTE_MINIMAL + +def genestrip(geneorg): + for line in geneorg: + line = line.strip() + if not len(line) or line.startswith('#'): + continue + yield line + +def read_conf(filename): + with open(filename, 'r', encoding='utf8') as linegen: + reader = csv.reader(genestrip(linegen), dialect=conf_dialect) + dic = {row[0].strip(): row[1] for row in reader if len(row) >= 2} + return dic -- cgit v1.2.3