Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sage
Path: blob/develop/build/sage_bootstrap/config.py
4052 views
1
# -*- coding: utf-8 -*-
2
"""
3
Set Up Logging
4
5
Logging can be customized using the ``SAGE_BOOTSTRAP`` environment
6
variable. It is a comma-separated list of ``key:value`` pairs. They
7
are not case sensitive. Valid pairs are:
8
9
* ``log:[level]``, where ``[level]`` is one of
10
11
* ``debug``
12
* ``info``
13
* ``warning``
14
* ``critical``
15
* ``error``
16
17
* ``interactive:true`` or ``interactive:false``, to override isatty detection.
18
"""
19
20
21
# ****************************************************************************
22
# Copyright (C) 2015 Volker Braun <[email protected]>
23
#
24
# This program is free software: you can redistribute it and/or modify
25
# it under the terms of the GNU General Public License as published by
26
# the Free Software Foundation, either version 2 of the License, or
27
# (at your option) any later version.
28
# https://www.gnu.org/licenses/
29
# ****************************************************************************
30
31
32
import sys
33
import os
34
35
36
LOG_LEVELS = (
37
'debug',
38
'info',
39
'warning',
40
'critical',
41
'error'
42
)
43
44
45
class Configuration(object):
46
47
_initialized = False
48
49
log = 'info'
50
51
interactive = os.isatty(sys.stdout.fileno())
52
53
def __init__(self):
54
if not Configuration._initialized:
55
Configuration._init_from_environ()
56
if self.log not in LOG_LEVELS:
57
raise ValueError('invalid log level: {0}'.format(self.log))
58
assert isinstance(self.interactive, bool)
59
60
@classmethod
61
def _init_from_environ(cls):
62
env = os.environ.get('SAGE_BOOTSTRAP', '').lower()
63
for pair in env.split(','):
64
if not pair.strip():
65
continue
66
key, value = pair.split(':', 1)
67
key = key.strip()
68
value = value.strip()
69
if key == 'log':
70
cls.log = value
71
elif key == 'interactive':
72
if value == 'true':
73
cls.interactive = True
74
elif value == 'false':
75
cls.interactive = False
76
else:
77
raise ValueError('interactive value must be "true" or "false", got "{0}"'
78
.format(value))
79
else:
80
raise ValueError('unknown key: "{0}"'.format(key))
81
cls._initialized = True
82
83
def __repr__(self):
84
return '\n'.join([
85
'Configuration:',
86
' * log = {0}'.format(self.log),
87
' * interactive = {0}'.format(self.interactive)
88
])
89
90