-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathenv.py
More file actions
103 lines (88 loc) · 3.03 KB
/
Copy pathenv.py
File metadata and controls
103 lines (88 loc) · 3.03 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
#
# Environment access and substitution.
# - Cameron Simpson <cs@cskk.id.au>
#
r'''
Some environment related functions.
* LOGDIR, VARRUN, FLAGDIR: lambdas defining standard places used in other modules
* envsub: replace substrings of the form '$var' with the value of 'var' from `environ`.
* getenv: fetch environment value, optionally performing substitution
'''
import os
from cs.gimmicks import warning
from cs.lex import get_qstr
__version__ = '20230407-post'
DISTINFO = {
'keywords': ["python2", "python3"],
'classifiers': [
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
],
'install_requires': ['cs.gimmicks', 'cs.lex'],
}
# TODO: look at the standard desktop stuff for these?
# various standard locations used in the cs.* modules
LOGDIR = lambda environ=None: _get_standard_var(
'LOGDIR', '$HOME/var/log', environ=environ
)
VARRUN = lambda environ=None: _get_standard_var(
'VARRUN', '$HOME/var/run', environ=environ
)
FLAGDIR = lambda environ=None: _get_standard_var(
'FLAGDIR', '$HOME/var/flags', environ=environ
)
def _get_standard_var(varname, default, environ=None):
if environ is None:
environ = os.environ
value = environ.get(varname)
if value is None:
value = envsub(default, environ)
return value
def getenv(var, default=None, environ=None, dosub=False, parse=None):
''' Fetch environment value.
Parameters:
* `var`: name of variable to fetch.
* `default`: default value if not present. If not specified or None,
raise KeyError.
* `environ`: environment mapping, default `os.environ`.
* `dosub`: if true, use envsub() to perform environment variable
substitution on `default` if it used. Default value is `False`.
* `parse`: optional callable to parse the environment variable;
*NOTE*: if this raises `ValueError` and there is a default, issue
a warning and return `default`
'''
if environ is None:
environ = os.environ
value = environ.get(var)
if value is None:
if default is None:
raise KeyError("getenv: $%s: unknown variable" % (var,))
value = default
if dosub:
value = envsub(value, environ=environ)
if parse is not None:
try:
value = parse(value)
except ValueError as e:
if default is None:
raise
warning(
"getenv: $%s: parse fails, using default %r: %s(%r): %s", var,
default, parse, value, e
)
value = default
if dosub:
value = envsub(value, environ=environ)
return value
def envsub(s, environ=None, default=None):
''' Replace substrings of the form '$var' with the value of 'var' from environ.
Parameters:
* `environ`: environment mapping, default `os.environ`.
* `default`: value to substitute for unknown vars;
if `default` is `None` a `ValueError` is raised.
'''
if environ is None:
environ = os.environ
return get_qstr(s, 0, q=None, environ=environ, default=default)[0]