-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
90 lines (80 loc) · 3.45 KB
/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Configuration YAML files for Gemini.
Provide Gemini configuration files in alternative locations:
- Installer based: gemini-virtualenv/../data or gemini-virtualenv/../gemini/data
- Global: /usr/local/share/gemini/gemini-config.yaml
- User only: $HOME/.gemini/gemini-config.yaml
Prefer installer based or global if you have system level permissions for
installation since it will work for all system users.
"""
import os
import yaml
CONFIG_FILE = "gemini-config.yaml"
CONFIG_ENVIRONMENT_VARIABLE = "GEMINI_CONFIG"
def get_config_dirs(use_globals=True):
virtualenv_loc = __file__.find("gemini-virtualenv")
anaconda_loc = __file__.find("anaconda")
if anaconda_loc >= 0:
base = __file__[:anaconda_loc]
dirs = [os.path.join(base), os.path.join(base, "gemini")]
elif virtualenv_loc >= 0:
base = __file__[:virtualenv_loc]
dirs = [os.path.join(base), os.path.join(base, "gemini")]
else:
dirs = []
if use_globals:
environment_config = os.getenv(CONFIG_ENVIRONMENT_VARIABLE)
if environment_config is not None:
dirs.append(environment_config)
dirs.append("/usr/local/share/gemini")
dirs.append(os.path.join(os.environ["HOME"], ".gemini"))
return dirs
def _get_config_file(dirs=None, use_globals=True):
dirs = [] if dirs is None else dirs
dnames = dirs + get_config_dirs(use_globals=use_globals)
for dname in dnames:
fname = os.path.join(dname, CONFIG_FILE)
if os.path.exists(fname):
return fname
raise ValueError("GEMINI configuration file {0} not found in {1}.\n"
"Please ensure the GEMINI data is installed using the install-data.py script\n"
"http://gemini.readthedocs.org/en/latest/content/installation.html"
.format(CONFIG_FILE, dnames))
def read_gemini_config(dirs=None, allow_missing=False, use_globals=True, args=None):
config = {}
try:
fname = _get_config_file(dirs, use_globals=use_globals)
except ValueError:
if allow_missing:
fname = None
else:
raise
if fname:
with open(fname) as in_handle:
config = yaml.load(in_handle)
if args and hasattr(args, "annotation_dir") and args.annotation_dir:
# If --annotation-dir is given via commandline interface, we will overwrite the
# location from the config file
config["annotation_dir"] = args.annotation_dir
return config
def _find_best_config_file(dirs=None):
dirs = [] if dirs is None else dirs
dnames = dirs + get_config_dirs()
for dname in dnames:
if os.access(dname, os.W_OK) or \
os.access(os.path.dirname(dname), os.W_OK):
return os.path.join(dname, CONFIG_FILE)
raise ValueError("Gemini configuration: "
"Could not find writeable directory: {0}".format(dnames))
def write_gemini_config(new_config, dirs=None):
try:
fname = _get_config_file(dirs, use_globals=False)
except ValueError:
fname = _find_best_config_file(dirs)
if not os.access(os.path.dirname(fname), os.W_OK):
print("Warning: unable to write GEMINI configuration file to %s" % fname)
else:
if not os.path.exists(os.path.dirname(fname)):
os.makedirs(os.path.dirname(fname))
with open(fname, "w") as out_handle:
yaml.dump(new_config, out_handle, allow_unicode=False,
default_flow_style=False)