mirror of https://github.com/docker/docs.git
Show a warning when a variable is unset
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
parent
8b5bd945d0
commit
ee6ff294a2
|
@ -1,11 +1,13 @@
|
||||||
import os
|
import os
|
||||||
from string import Template
|
from string import Template
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from .errors import ConfigurationError
|
from .errors import ConfigurationError
|
||||||
|
|
||||||
|
import logging
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def interpolate_environment_variables(config):
|
def interpolate_environment_variables(config):
|
||||||
return dict(
|
return dict(
|
||||||
|
@ -59,11 +61,26 @@ def recursive_interpolate(obj):
|
||||||
|
|
||||||
def interpolate(string, mapping):
|
def interpolate(string, mapping):
|
||||||
try:
|
try:
|
||||||
return Template(string).substitute(defaultdict(lambda: "", mapping))
|
return Template(string).substitute(BlankDefaultDict(mapping))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise InvalidInterpolation(string)
|
raise InvalidInterpolation(string)
|
||||||
|
|
||||||
|
|
||||||
|
class BlankDefaultDict(dict):
|
||||||
|
def __init__(self, mapping):
|
||||||
|
super(BlankDefaultDict, self).__init__(mapping)
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
try:
|
||||||
|
return super(BlankDefaultDict, self).__getitem__(key)
|
||||||
|
except KeyError:
|
||||||
|
log.warn(
|
||||||
|
"The {} variable is not set. Substituting a blank string."
|
||||||
|
.format(key)
|
||||||
|
)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
class InvalidInterpolation(Exception):
|
class InvalidInterpolation(Exception):
|
||||||
def __init__(self, string):
|
def __init__(self, string):
|
||||||
self.string = string
|
self.string = string
|
||||||
|
|
Loading…
Reference in New Issue