File: _settings_helpers.py

package info (click to toggle)
glueviz 0.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,180 kB
  • ctags: 6,728
  • sloc: python: 37,111; makefile: 134; sh: 60
file content (55 lines) | stat: -rw-r--r-- 1,583 bytes parent folder | download
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
from __future__ import absolute_import, division, print_function

import os
import json

from glue.external.six.moves import configparser
from glue.logger import logger


def save_settings():

    from glue.config import settings, CFG_DIR
    settings_cfg = os.path.join(CFG_DIR, 'settings.cfg')

    config = configparser.ConfigParser()
    config.add_section('main')

    for name, value, _ in sorted(settings):
        config.set('main', name, value=json.dumps(value))

    if not os.path.exists(CFG_DIR):
        os.mkdir(CFG_DIR)

    with open(settings_cfg, 'w') as fout:
        config.write(fout)


def load_settings(force=False):
    """
    Load the settings from disk.

    By default, only settings not already defined in memory are read in, but
    by setting ``force=True``, all settings will be read in.
    """

    from glue.config import settings, CFG_DIR
    settings_cfg = os.path.join(CFG_DIR, 'settings.cfg')

    logger.info("Loading settings from {0}".format(settings_cfg))

    config = configparser.ConfigParser()
    read = config.read(settings_cfg)

    if len(read) == 0 or not config.has_section('main'):
        return

    for name, value in config.items('main'):
        name = name.upper()
        if name in settings:
            if settings.is_default(name) or force:
                setattr(settings, name, json.loads(value))
            elif not settings.is_default(name):
                logger.info("Setting {0} already initialized - skipping".format(name))
        else:
            logger.info("Unknown setting {0} - skipping".format(name))