<commit_before>#!/usr/bin/python2
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
#
#    Copyright (C) 2012 <NAME> <<EMAIL>>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.

"""
Interface to the Linux network emulator and rate limiting features.

"""

# python 3 compatibility
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

import operator
import functools
import re

from pycopia import aid
from pycopia import proctools
from pycopia.physics.physical_quantities import PhysicalQuantity as PQ


class Error(Exception):
    pass


class RouterConfigError(Error):
    """Raised if a configuration command was not able to run.

    Carries a tuple of integer exit status and stderr text.
    """
    pass

class RouterConstructorError(Error):
    """Raised if you try to do something that's not possible."""
    pass


# possible delay distribution values.
DIST_UNIFORM = "uniform"
DIST_NORMAL = "normal"
DIST_PARETO = "pareto"
DIST_PARETONORMAL = "paretonormal"

# Direction values
Up, Down, Level, Unknown = aid.Enums("up", "down", "level", "unknown")


class ImpairmentsConfigurer(object):
    """Simplified interface to configuring network interfaces and path impairments.

    Parameters
    ----------
    interfaces : list of str
        A list of the data interface names used in the path configuration.
    """

    def __init__(self, interfaces):
        self.interfaces = interfaces

    def _perform(self, cmd):
        pm = proctools.get_procmanager()
        proc = pm.spawnpipe(cmd)
        out = proc.read()
        es = proc.wait()
        if es:
            return out
        else:
            raise RouterConfigError((int(es), out))


    def clear(self, interface):
        r"""clear the interface configuration.

        Ensures there are no delay or loss configurations on the interface.

        Parameters
        ----------
        interface : str
            The name of the interface to operate on. e.g. "eth1".

        Raises
        ------
        RouterConfigError
            If there was a problem running the command.

        See Also
        --------
        show, delay, reorder, corrupt, loss

        Examples
        --------
        >>> router.clear("eth1")
        """
        cmd = "tc qdisc del dev {0} root".format(interface)
        es, out = self._perform(cmd)
        if es:
            return out
        if int(es) == 2: # Indicates it was already clear, which is ok.
            return out
        else:
            raise RouterConfigError((int(es), out))

    def show(self, interface):
        r"""Get the current interface configuration.

        Return information about the current configuration of the interface.

        Parameters
        ----------
        interface : str<commit_msg>Fixed a bug.<commit_after>#!/usr/bin/python2
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
#
#    Copyright (C) 2012 <NAME> <<EMAIL>>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.

"""
Interface to the Linux network emulator and rate limiting features.

"""

# python 3 compatibility
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

import operator
import functools
import re

from pycopia import aid
from pycopia import proctools
from pycopia.physics.physical_quantities import PhysicalQuantity as PQ


class Error(Exception):
    pass


class RouterConfigError(Error):
    """Raised if a configuration command was not able to run.

    Carries a tuple of integer exit status and stderr text.
    """
    pass

class RouterConstructorError(Error):
    """Raised if you try to do something that's not possible."""
    pass


# possible delay distribution values.
DIST_UNIFORM = "uniform"
DIST_NORMAL = "normal"
DIST_PARETO = "pareto"
DIST_PARETONORMAL = "paretonormal"

# Direction values
Up, Down, Level, Unknown = aid.Enums("up", "down", "level", "unknown")


class ImpairmentsConfigurer(object):
    """Simplified interface to configuring network interfaces and path impairments.

    Parameters
    ----------
    interfaces : list of str
        A list of the data interface names used in the path configuration.
    """

    def __init__(self, interfaces):
        self.interfaces = interfaces

    def _perform(self, cmd):
        pm = proctools.get_procmanager()
        proc = pm.spawnpipe(cmd)
        out = proc.read()
        es = proc.wait()
        if es:
            return out
        else:
            raise RouterConfigError(int(es), out)


    def clear(self, interface):
        r"""clear the interface configuration.

        Ensures there are no delay or loss configurations on the interface.

        Parameters
        ----------
        interface : str
            The name of the interface to operate on. e.g. "eth1".

        Raises
        ------
        RouterConfigError
            If there was a problem running the command.

        See Also
        --------
        show, delay, reorder, corrupt, loss

        Examples
        --------
        >>> router.clear("eth1")
        """
        cmd = "tc qdisc del dev {0} root".format(interface)
        try:
            self._perform(cmd)
        except RouterConfigError as rerr:
            if rerr.args[0] == 2: # Indicates it was already clear, which is ok.
                return
            else:
                raise

    def show(self, interface):
        r"""Get the current interface configuration.

        Return information about the current configuration of the interface.

        Parameters
        ----------
        interface : str
{'fraction_non_alphanumeric': 0.08742354169774728, 'fraction_numerical': 0.004923168730419215, 'mean_word_length': 2.4556701030927837, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6920875', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1749, 'n_words': 703}