Project

General

Profile

« Previous | Next » 

Revision 9fdf0ddd

Added by Mikaël Ates almost 11 years ago

Allow a silent mode for missing variables in template generation.

View differences:

calebasse/doc_templates.py
23 23
        raise
24 24

  
25 25

  
26
def replace_variables(template, variables):
26
def replace_variables(template, variables, ignore_unused=True):
27 27
    '''Lookup all substring looking like @XXX9 in the string template, and
28 28
       replace them by the value of the key XXX9 in the dictionary variables.
29 29

  
......
37 37
    given_vars = set(variables.keys())
38 38
    if given_vars != needed_vars:
39 39
        missing = needed_vars - given_vars
40
        unused = given_vars - needed_vars
41
        raise DocTemplateError(
42
            'Mismatch between given and needed variables: missing={0} unused={1}'
43
            .format(map(repr, missing), map(repr, unused)))
40
        if missing or not ignore_unused:
41
            unused = given_vars - needed_vars
42
            raise DocTemplateError(
43
                'Mismatch between given and needed variables: missing={0} unused={1}'
44
                .format(map(repr, missing), map(repr, unused)))
44 45
    def variable_replacement(match_obj):
45 46
        return variables[match_obj.group(0)[1:]]
46 47
    return re.sub(VARIABLE_RE, variable_replacement, template)

Also available in: Unified diff