aboutsummaryrefslogtreecommitdiffstats
path: root/confparser.py
blob: e43443b8c2e7a222e07c161b490e0a5992e4c5fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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