-
-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathconfig.py
88 lines (70 loc) · 2.47 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
# -*- coding: utf-8 -*-
"""
Set Up Logging
Logging can be customized using the ``SAGE_BOOTSTRAP`` environment
variable. It is a comma-separated list of ``key:value`` pairs. They
are not case sensitive. Valid pairs are:
* ``log:[level]``, where ``[level]`` is one of
* ``debug``
* ``info``
* ``warning``
* ``critical``
* ``error``
* ``interactive:true`` or ``interactive:false``, to override isatty detection.
"""
# ****************************************************************************
# Copyright (C) 2015 Volker Braun <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
import sys
import os
LOG_LEVELS = (
'debug',
'info',
'warning',
'critical',
'error'
)
class Configuration(object):
_initialized = False
log = 'info'
interactive = os.isatty(sys.stdout.fileno())
def __init__(self):
if not Configuration._initialized:
Configuration._init_from_environ()
if self.log not in LOG_LEVELS:
raise ValueError('invalid log level: {0}'.format(self.log))
assert isinstance(self.interactive, bool)
@classmethod
def _init_from_environ(cls):
env = os.environ.get('SAGE_BOOTSTRAP', '').lower()
for pair in env.split(','):
if not pair.strip():
continue
key, value = pair.split(':', 1)
key = key.strip()
value = value.strip()
if key == 'log':
cls.log = value
elif key == 'interactive':
if value == 'true':
cls.interactive = True
elif value == 'false':
cls.interactive = False
else:
raise ValueError('interactive value must be "true" or "false", got "{0}"'
.format(value))
else:
raise ValueError('unknown key: "{0}"'.format(key))
cls._initialized = True
def __repr__(self):
return '\n'.join([
'Configuration:',
' * log = {0}'.format(self.log),
' * interactive = {0}'.format(self.interactive)
])