Merge branch 'garotosopa-devel' into devel

This commit is contained in:
Muayyad alsadi 2021-05-06 00:49:54 +03:00
commit 905914b0dc
4 changed files with 55 additions and 26 deletions

View File

@ -146,25 +146,19 @@ def fix_mount_dict(mount_dict, proj_name, srv_name):
# ${VARIABLE?err} raise error if not set
# $$ means $
var_re = re.compile(r'\$(\{(?:[^\s\$:\-\}]+)\}|(?:[^\s\$\{\}]+))')
var_def_re = re.compile(r'\$\{([^\s\$:\-\}]+)(:)?-([^\}]*)\}')
var_err_re = re.compile(r'\$\{([^\s\$:\-\}]+)(:)?\?([^\}]*)\}')
def dicts_get(dicts, key, fallback='', fallback_empty=False):
"""
get the given key from any dict in dicts, trying them one by one
if not found in any, then use fallback, if fallback is Exception raise is
"""
value = None
for d in dicts:
value = d.get(key, None)
if value is not None: break
if not value:
if fallback_empty or value is None:
value = fallback
if isinstance(value, Exception):
raise value
return value
var_re = re.compile(r"""
\$(?:
(?P<escaped>\$) |
(?P<named>[_a-zA-Z][_a-zA-Z0-9]*) |
(?:{
(?P<braced>[_a-zA-Z][_a-zA-Z0-9]*)
(?:
(?::?-(?P<default>[^}]+)) |
(?::?\?(?P<err>[^}]+))
)?
})
)
""", re.VERBOSE)
def rec_subs(value, dicts):
"""
@ -173,13 +167,18 @@ def rec_subs(value, dicts):
if is_dict(value):
value = dict([(k, rec_subs(v, dicts)) for k, v in value.items()])
elif is_str(value):
value = var_re.sub(lambda m: dicts_get(dicts, m.group(1).strip('{}')), value)
sub_def = lambda m: dicts_get(dicts, m.group(1), m.group(3), m.group(2) == ':')
value = var_def_re.sub(sub_def, value)
sub_err = lambda m: dicts_get(dicts, m.group(1), RuntimeError(m.group(3)),
m.group(2) == ':')
value = var_err_re.sub(sub_err, value)
value = value.replace('$$', '$')
def convert(m):
if m.group("escaped") is not None:
return "$"
name = m.group("named") or m.group("braced")
for d in dicts:
value = d.get(name)
if value is not None:
return "%s" % value
if m.group("err") is not None:
raise RuntimeError(m.group("err"))
return m.group("default") or ""
value = var_re.sub(convert, value)
elif hasattr(value, "__iter__"):
value = [rec_subs(i, dicts) for i in value]
return value

View File

@ -0,0 +1,8 @@
version: "3.7"
services:
variables:
image: busybox
command: ["/bin/busybox", "sh", "-c", "export | grep EXAMPLE"]
environment:
EXAMPLE_COLON_QUESTION_ERROR: ${NOT_A_VARIABLE:?Missing variable}

View File

@ -0,0 +1,8 @@
version: "3.7"
services:
variables:
image: busybox
command: ["/bin/busybox", "sh", "-c", "export | grep EXAMPLE"]
environment:
EXAMPLE_QUESTION_ERROR: ${NOT_A_VARIABLE?Missing variable}

View File

@ -0,0 +1,14 @@
version: "3.7"
services:
variables:
image: busybox
command: ["/bin/busybox", "sh", "-c", "export | grep EXAMPLE"]
environment:
EXAMPLE_VARIABLE: "Host user: $USER"
EXAMPLE_BRACES: "Host user: ${USER}"
EXAMPLE_COLON_DASH_DEFAULT: ${NOT_A_VARIABLE:-My default}
EXAMPLE_DASH_DEFAULT: ${NOT_A_VARIABLE-My other default}
EXAMPLE_DOT_ENV: $DOT_ENV_VARIABLE
EXAMPLE_LITERAL: This is a $$literal
EXAMPLE_EMPTY: $NOT_A_VARIABLE