Files @ 90fac7c6abb9
Branch filter:

Location: conntrackt/conntrackt/utils.py

branko
Renamed the tags library. Removed the line stripper middleware, and replaced it with some custom code that will remove all blank lines. The iptables rule rendering has been rewritten to accomodate this change (a utility function has been added for generating them). Fixed some code styling.
# Standard library imports.
import re

# Django-specific imports.
from django.template import Context, loader


def generate_entity_iptables(entity):
    """
    Generates full iptables rules for the supplied entity. The generated rules
    can be fed directly to the iptables-restore utility.

    Arguments:

        entity - An Entity instance for which the iptables rules should be
        generated.

    Returns:

        String containing the iptables rules for entity.
    """

    # Render the iptables template.
    template = loader.get_template('conntrackt/entity_iptables.html')
    context = Context({'entity': entity})
    content = template.render(context)

    # Remove the blank lines.
    content = re.sub('^\s*\n', '', content)
    content = re.sub('\n\s*\n', '\n', content)

    return content