8784: Fix test for latest firefox.
[arvados.git] / sdk / python / arvados / config.py
1 # config.py - configuration settings and global variables for Arvados clients
2 #
3 # Arvados configuration settings are taken from $HOME/.config/arvados.
4 # Environment variables override settings in the config file.
5
6 import os
7 import re
8
9 _settings = None
10 if os.environ.get('HOME') is not None:
11     default_config_file = os.environ['HOME'] + '/.config/arvados/settings.conf'
12 else:
13     default_config_file = ''
14
15 KEEP_BLOCK_SIZE = 2**26
16 EMPTY_BLOCK_LOCATOR = 'd41d8cd98f00b204e9800998ecf8427e+0'
17
18 def initialize(config_file=default_config_file):
19     global _settings
20     _settings = {}
21
22     # load the specified config file if available
23     try:
24         _settings = load(config_file)
25     except IOError:
26         pass
27
28     # override any settings with environment vars
29     for var in os.environ:
30         if var.startswith('ARVADOS_'):
31             _settings[var] = os.environ[var]
32
33 def load(config_file):
34     cfg = {}
35     with open(config_file, "r") as f:
36         for config_line in f:
37             if re.match('^\s*$', config_line):
38                 continue
39             if re.match('^\s*#', config_line):
40                 continue
41             var, val = config_line.rstrip().split('=', 2)
42             cfg[var] = val
43     return cfg
44
45 def flag_is_true(key, d=None):
46     if d is None:
47         d = settings()
48     return d.get(key, '').lower() in set(['1', 't', 'true', 'y', 'yes'])
49
50 def get(key, default_val=None):
51     return settings().get(key, default_val)
52
53 def settings():
54     if _settings is None:
55         initialize()
56     return _settings