Changeset - 3d3c7481aede
[Not reviewed]
default
0 1 0
Mads Kiilerich - 6 years ago 2020-04-22 16:21:23
mads@kiilerich.com
Grafted from: 83ce7e84980a
inifile: add doctest coverage of comment corner cases

Shows room for improvement - doesn't show any bugs.
1 file changed with 27 insertions and 0 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/inifile.py
Show inline comments
 
@@ -46,72 +46,99 @@ variable_options = {
 
    'database_engine': ['sqlite', 'postgres', 'mysql'],
 
    'http_server': ['waitress', 'gearbox', 'gevent', 'gunicorn', 'uwsgi'],
 
}
 

	
 
def expand(template, mako_variable_values, settings):
 
    """Expand mako template and tweak it.
 
    Not entirely stable for random templates as input, but good enough for our
 
    single template.
 

	
 
    >>> template = '''
 
    ... [first-section]
 
    ...
 
    ... variable=${mako_variable}
 
    ... variable2  =\tvalue after tab
 
    ... ## This section had some whitespace and stuff
 
    ...
 
    ...
 
    ... # ${mako_function()}
 
    ... [second-section]
 
    ... %if conditional_options == 'option-a':
 
    ... # option a was chosen
 
    ... %elif conditional_options == 'option-b':
 
    ... some_variable = "never mind - option-b will not be used anyway ..."
 
    ... %endif
 
    ...
 
    ... [comment-section]
 
    ... #variable3 = 3.0
 
    ... #variable4 = 4.0
 
    ... #variable5 = 5.0
 
    ... variable5 = 5.1
 
    ... #variable6 = 6.0
 
    ... #variable6 = 6.1
 
    ... #variable7 = 7.0
 
    ... variable7 = 7.1
 
    ... '''
 
    >>> mako_variable_values = {'mako_variable': 'VALUE', 'mako_function': (lambda: 'FUNCTION RESULT'),
 
    ...                         'conditional_options': 'option-a', 'http_server': 'nc'}
 
    >>> settings = { # only partially used
 
    ...     '[first-section]': {'variable2': 'VAL2', 'first_extra': 'EXTRA'},
 
    ...     '[comment-section]': {'variable3': '3.0', 'variable4': '4.1', 'variable5': '5.2', 'variable6': '6.2', 'variable7': '7.0'},
 
    ...     '[third-section]': {'third_extra': ' 3'},
 
    ...     '[fourth-section]': {'fourth_extra': '4', 'fourth': '"four"'},
 
    ... }
 
    >>> print(expand(template, mako_variable_values, settings))
 
    ERROR: http_server is 'nc' - it should be one of 'waitress', 'gearbox', 'gevent', 'gunicorn', 'uwsgi'
 
    <BLANKLINE>
 
    [first-section]
 
    <BLANKLINE>
 
    variable=VALUE
 
    #variable2  =    value after tab
 
    variable2 = VAL2
 
    <BLANKLINE>
 
    first_extra = EXTRA
 
    <BLANKLINE>
 
    <BLANKLINE>
 
    # FUNCTION RESULT
 
    [second-section]
 
    # option a was chosen
 
    <BLANKLINE>
 
    [comment-section]
 
    #variable3 = 3.0
 
    #variable4 = 4.0
 
    #variable5 = 5.0
 
    #variable5 = 5.1
 
    variable5 = 5.2
 
    #variable6 = 6.0
 
    #variable6 = 6.1
 
    #variable7 = 7.0
 
    #variable7 = 7.1
 
    variable7 = 7.0
 
    <BLANKLINE>
 
    variable3 = 3.0
 
    variable4 = 4.1
 
    variable6 = 6.2
 
    <BLANKLINE>
 
    [fourth-section]
 
    fourth = "four"
 
    fourth_extra = 4
 
    <BLANKLINE>
 
    [third-section]
 
    third_extra =  3
 
    <BLANKLINE>
 
    """
 
    mako_variables = dict(default_variables)
 
    mako_variables.update(mako_variable_values or {})
 
    settings = dict((k, dict(v)) for k, v in settings.items()) # deep copy before mutating
 

	
 
    for key, value in mako_variables.items():
 
        if key in variable_options:
 
            if value not in variable_options[key]:
 
                print('ERROR: %s is %r - it should be one of %s' %
 
                      (key, value, ', '.join(repr(x) for x in variable_options[key])))
 

	
 
    ini_lines = mako.template.Template(template).render(**mako_variables)
 

	
 
    def process_section(m):
 
        """process a ini section, replacing values as necessary"""
 
        sectionname, lines = m.groups()
 
        if sectionname in settings:
0 comments (0 inline, 0 general)