def build_policy(config=None, update=None, replace=None):
    """Builds the policy as a string from the settings."""

    if config is None:
        config = from_settings()

    # Update rules from settings.
    if update is not None:
        for key, value in update.items():
            if not isinstance(value, (list, tuple)):
                value = (value,)
            if config[key] is not None:
                config[key] += value
            else:
                config[key] = value

    # Replace rules from settings.
    if replace is not None:
        for key, value in replace.items():
            if value is not None and not isinstance(value, (list, tuple)):
                value = [value]
            config[key] = value

    report_uri = config.pop('report-uri', None)
    policy = ['%s %s' % (k, ' '.join(v)) for k, v in
              sorted(config.items()) if v is not None]
    if report_uri:
        policy.append('report-uri %s' % report_uri)
    return '; '.join(policy)
dataset/ETHPy150Open mozilla/django-csp/csp/utils.py/build_policy/original