pungi/pungi_utils/config_utils.py
Lubomír Sedlář acd3c19618 config-validate: Allow defining variables
When trying to validate a template that should later be filled in with
`pungi-config-dump`, there will be errors about undefined variables.
These are meant to be set when the template is populated.

This patch adds support for `-e`, `--define` argument to the validation
script that can be used to suppress these errors.

Alternatively a JSON file is read from the directory with config file
that can contain values for the variables.

The `--define` option is changed in both validation and dumping to allow
empty string as an accepted value.

JIRA: COMPOSE-3599
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
2019-06-21 12:47:50 +02:00

21 lines
576 B
Python

# -*- coding: utf-8 -*-
import argparse
import re
def validate_definition(value):
"""Check that the variable name is a valid Python variable name, and that
there is an equals sign. The value can by anything non-empty.
"""
if not re.match(r"^[a-z_]\w*=.*$", value):
raise argparse.ArgumentTypeError(
"definition should be in var=value format: %r" % value
)
return value
def extract_defines(args):
"""Given an iterable of "key=value" strings, parse them into a dict."""
return dict(var.split("=", 1) for var in args)