import copy
import re


def _rescale_fontsize(cfg_in, scale):
    """ Traverses cfg rescaling values of keys that contain the word 'fontsize'.

    This keeps fonts the correct size when a user has requested to scale the configuration.
    """
    cfg = copy.deepcopy(cfg_in)
    for key, value in cfg.items():
        if isinstance(value, dict):
            cfg[key] = _rescale_fontsize(value, scale)
        elif re.search('fontsize', key):
            cfg[key] = value * scale
    return cfg