This repository has been archived by the owner on Feb 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile.py
67 lines (58 loc) · 1.75 KB
/
profile.py
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
"""
Cloudless Profile
These are helper functions to manage cloudless profiles.
"""
import os
import yaml
class FileConfigSource:
"""
Loads and saves configuration data to a YAML file.
"""
def __init__(self):
self.config_dir = os.path.expanduser("~/.cloudless/")
self.config_path = os.path.join(self.config_dir, "config.yml")
def load(self):
"""
Load configuration.
"""
if not os.path.exists(self.config_path):
return None
with open(self.config_path, 'r') as config_file:
return yaml.safe_load(config_file)
def save(self, config):
"""
Save configuration.
"""
if not os.path.exists(self.config_dir):
os.makedirs(self.config_dir)
with open(self.config_path, 'w') as config_file:
config_file.write(yaml.dump(config))
def save_profile(profile, options):
"""
Saves the profile to the correct path. Currently only accepts a profile name and a provider.
"""
file_config_source = FileConfigSource()
config = file_config_source.load()
if not config:
config = {}
config[profile] = options
file_config_source.save(config)
def load_profile(profile):
"""
Loads the profile into a dictionary.
"""
file_config_source = FileConfigSource()
config = file_config_source.load()
return config.get(profile, None)
def select_profile(profile=None):
"""
Figure out what profile we should use. Order of precedence:
1. Explicitly set profile
2. CLOUDLESS_PROFILE environment variable
3. "default"
"""
if profile:
return profile
if "CLOUDLESS_PROFILE" in os.environ:
return os.environ["CLOUDLESS_PROFILE"]
return "default"