summaryrefslogtreecommitdiffstats
path: root/pinolo/config.py
blob: fc2f316a76249189f0b28b56ac5fe9f971b69ec3 (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
28
29
30
31
32
# -*- coding: utf-8 -*-
"""
    pinolo.config
    ~~~~~~~~~~~~~

    Configuration file handling. Boring stuff.

    :copyright: (c) 2013 Daniel Kertesz
    :license: BSD, see LICENSE for more details.
"""
import sys
import os
import coil


def read_config_file(filename):
    """Read a configuration file in coil format and returns a Struct
    object (dict-like)"""

    def fatal(msg):
        sys.stderr.write("%s\n" % msg)
        sys.exit(1)
    
    config = coil.parse_file(filename, encoding="utf-8")

    if config.get("@root.datadir") is None:
        fatal("Config error: empty 'datadir' parameter")
    else:
        if config["datadir"].startswith("~"):
            config.set("datadir", os.path.expanduser(config["datadir"]))
        
    return config