~snappy-dev/snapcraft/core

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
#!/usr/bin/env python3

import os
import os.path
import sys
import yaml

_CONFIG = 'configuration'

_DEFAULT_INTERVAL = 10


def main():
    config_file = os.path.join(os.environ['SNAP_APP_DATA_PATH'], _CONFIG)

    config_yaml = yaml.load(sys.stdin)
    if config_yaml:
        set_config(config_file, config_yaml)

    yaml.dump(get_config(config_file),
              stream=sys.stdout,
              default_flow_style=False)


def set_config(config_file, config_yaml={}):
    with open(config_file, 'w') as f:
        yaml.dump(_config(config_yaml), stream=f, default_flow_style=False)

    return config_yaml


def get_config(config_file):
    try:
        with open(config_file) as f:
            return yaml.load(f)
    except FileNotFoundError:
        return _config()


def _config(config_yaml={}):
    try:
        interval_value = config_yaml['config'][
            os.environ['SNAP_NAME']]['interval']
        if not isinstance(interval_value, int):
            config_yaml['config'][
                os.environ['SNAP_NAME']]['interval'] = _DEFAULT_INTERVAL
    except KeyError:
        interval = {
            'config': {
                os.environ['SNAP_NAME']: {
                    'interval': _DEFAULT_INTERVAL
                }
            }
        }
        config_yaml.update(interval)

    return config_yaml


if __name__ == '__main__':
    main()