-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathfab_state_config.py
More file actions
76 lines (55 loc) · 1.95 KB
/
fab_state_config.py
File metadata and controls
76 lines (55 loc) · 1.95 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import json
import os
from os.path import exists, expanduser
from fabric_cli.core import fab_constant
def config_location():
_location = expanduser("~/.config/fab/")
if not exists(_location):
os.makedirs(_location)
return _location
config_file = os.path.join(config_location(), "config.json")
def read_config(file_path) -> dict:
try:
with open(file_path, "r") as file:
return json.load(file)
except FileNotFoundError:
return {}
except json.JSONDecodeError:
return {}
def write_config(data):
with open(config_file, "w") as file:
json.dump(data, file, indent=4)
def set_config(key, value):
config = read_config(config_file)
config[key] = value
write_config(config)
def get_config(key):
config = read_config(config_file)
return config.get(key)
def list_configs():
config = read_config(config_file)
return {**config}
def init_defaults():
"""
Ensures that all known config keys have default values if they are not already set.
"""
current_config = read_config(config_file)
changed = False
# Migration: remove the deprecated 'mode' key (mode is now detected at runtime)
if fab_constant.FAB_MODE in current_config:
del current_config[fab_constant.FAB_MODE]
for key in fab_constant.FAB_CONFIG_KEYS_TO_VALID_VALUES:
old_key = f"fab_{key}"
if old_key in current_config:
# Transfer value if not already set under the new key
if key not in current_config:
current_config[key] = current_config[old_key]
del current_config[old_key]
changed = True
if key not in current_config and key in fab_constant.CONFIG_DEFAULT_VALUES:
current_config[key] = fab_constant.CONFIG_DEFAULT_VALUES[key]
changed = True
if changed:
write_config(current_config)