Skip to content

Commit

Permalink
Show a warning when a variable is unset
Browse files Browse the repository at this point in the history
Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
  • Loading branch information
aanand committed Aug 6, 2015
1 parent 8b5bd94 commit ee6ff29
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions compose/config/interpolation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
from string import Template
from collections import defaultdict

import six

from .errors import ConfigurationError

import logging
log = logging.getLogger(__name__)


def interpolate_environment_variables(config):
return dict(
Expand Down Expand Up @@ -59,11 +61,26 @@ def recursive_interpolate(obj):

def interpolate(string, mapping):
try:
return Template(string).substitute(defaultdict(lambda: "", mapping))
return Template(string).substitute(BlankDefaultDict(mapping))
except ValueError:
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):
def __init__(self, string):
self.string = string

0 comments on commit ee6ff29

Please sign in to comment.