aboutsummaryrefslogtreecommitdiff
path: root/cleantools/settings.py
blob: 67ecd3bb666118e34e44cee0727ba504a42db2d1 (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import configparser
import os

def expand_dir(d):
    return os.path.expanduser(d)

config_file = expand_dir('~/.clmgr.cfg')

def make_install_dir(install_dir):
    install_dir = expand_dir(install_dir)
    if not os.path.exists(install_dir):
        os.makedirs(install_dir)

def init(install_dir):
    install_dir = expand_dir(install_dir)

    config = configparser.ConfigParser()
    config.add_section('General')
    config.set('General', 'install_dir', install_dir)
    with open(config_file, 'w') as f:
        config.write(f)

    make_install_dir(install_dir)

def read():
    config = configparser.ConfigParser()
    config.read(config_file)
    return config

def get_search_paths():
    config = read()
    paths = []

    def try_append(path):
        if path != None and os.path.isdir(path):
            paths.append(path)
    
    try:
        try_append(config['General']['install_dir'])
    except:
        pass

    try:
        home = config['General']['clean_home']
        try_append(home + '/lib')
    except:
        pass

    return paths

def print_config(conf, indent=0, indent_step=2):
    conf = dict(conf)
    for k, v in sorted(conf.items()):
        if type(v) == configparser.SectionProxy:
            if v.name == 'DEFAULT':
                print_config(v, indent)
            else:
                print('{0}[{1}]'.format(' '*indent, v.name))
                print_config(v, indent + indent_step, indent_step)
        else:
            print('{0}{1: <{f}} = {2}'.format(' '*indent, k, v, f=24-indent))