<commit_before>from builtins import str
from builtins import next
from builtins import object

import logging
import re
import socket
from datetime import datetime, timedelta
from subprocess import Popen, PIPE


class PyBird(object):
    ignored_field_numbers = [0, 1, 13, 1008, 2002, 9001]

    def __init__(self, socket_file, hostname=None, user=None, password=<PASSWORD>, dummy=False):
        """Basic pybird setup.
        Required argument: socket_file: full path to the BIRD control socket."""
        self.socket_file = socket_file
        self.hostname = hostname
        self.user = user
        self.password = password
        self.clean_input_re = re.compile('\W+')
        self.field_number_re = re.compile('^(\d+)[ -]')
        self.routes_field_re = re.compile('(\d+) imported, (\d+) exported')
        self.dummy = dummy
        self.log = logging.getLogger(__name__)

    def get_bird_status(self):
        """Get the status of the BIRD instance. Returns a dict with keys:
        - router_id (string)
        - last_reboot (datetime)
        - last_reconfiguration (datetime)"""
        if self.dummy:
            return [
                {'prefix': '2001:db8:/32', 'as_path': '65520',
                    'communities': '65520:79 65521:421'},
                {'prefix': '2002::/16', 'as_path': '65520',
                    'communities': '65520:1234'},
            ]
        query = "show status"
        data = self._send_query(query)
        if not self.socket_file:
            return data

        line_iterator = iter(data.splitlines())
        data = {}

        for line in line_iterator:
            line = line.strip()
            (field_number, line) = self._extract_field_number(line)

            if field_number in self.ignored_field_numbers:
                continue

            if field_number == 1011:
                # Parse the status section, which looks like:
                # 1011-Router ID is 192.168.3.11
                # Current server time is 10-01-2012 10:24:37
                # Last reboot on 03-01-2012 12:46:40
                # Last reconfiguration on 03-01-2012 12:46:40
                data['router_id'] = self._parse_router_status_line(line)
                line = next(line_iterator)  # skip current server time
                data['last_reboot'] = self._parse_router_status_line(
                    next(line_iterator), parse_date=True)
                data['last_reconfiguration'] = self._parse_router_status_line(
                    next(line_iterator), parse_date=True)

        return data

    def _parse_router_status_line(self, line, parse_date=False):
        """Parse a line like:
            Current server time is 10-01-2012 10:24:37
        optionally (if parse_date=True), parse it into a datetime"""
        data = line.strip().split(' ', 3)[-1]
        if parse_date:
            return datetime.strptime(data, '%d-%m-%Y %H:%M:%S')
        else:
            return data

    def get_peer_prefixes_announced(self, peer_name):
        """Get prefixes announced by a specific peer, without applying
        filters - i.e. this includes routes which were not accepted"""
        if self.dummy:
            return [
                {'prefix': '2001:db8:/32', 'as_path': '65520',
                    'community': '65520:79 65521:421'},
                {'prefix': '2002::/16', 'as_path': '65520',
                    'community': '65520:1234'},
            ]
        clean_peer_name = self._clean_input(peer_name)
        query = "show route table T_%s all protocol %s" % (
            clean_peer_name, clean_peer_name)
        data = self._send_query(query)
        return self._parse_route_data(data)

    def get_peer_prefixes_exported(self, peer_name):
        """Get prefixes exported TO a specific peer"""
        if self.dummy:
            return [
                {'prefix': '2001:db8:/32', 'as_path': '65520',
                    'community': '65520:79 65521:421'},
                {'prefix': '2002::/16', 'as_path': '65520',
                    'community': '65520:1234'},
            ]<commit_msg>fix date format for new bird<commit_after>from builtins import str
from builtins import next
from builtins import object

import logging
import re
import socket
from datetime import datetime, timedelta
from subprocess import Popen, PIPE


class PyBird(object):
    ignored_field_numbers = [0, 1, 13, 1008, 2002, 9001]

    def __init__(self, socket_file, hostname=None, user=None, password=<PASSWORD>, dummy=False):
        """Basic pybird setup.
        Required argument: socket_file: full path to the BIRD control socket."""
        self.socket_file = socket_file
        self.hostname = hostname
        self.user = user
        self.password = password
        self.clean_input_re = re.compile('\W+')
        self.field_number_re = re.compile('^(\d+)[ -]')
        self.routes_field_re = re.compile('(\d+) imported, (\d+) exported')
        self.dummy = dummy
        self.log = logging.getLogger(__name__)

    def get_bird_status(self):
        """Get the status of the BIRD instance. Returns a dict with keys:
        - router_id (string)
        - last_reboot (datetime)
        - last_reconfiguration (datetime)"""
        if self.dummy:
            return [
                {'prefix': '2001:db8:/32', 'as_path': '65520',
                    'communities': '65520:79 65521:421'},
                {'prefix': '2002::/16', 'as_path': '65520',
                    'communities': '65520:1234'},
            ]
        query = "show status"
        data = self._send_query(query)
        if not self.socket_file:
            return data

        line_iterator = iter(data.splitlines())
        data = {}

        for line in line_iterator:
            line = line.strip()
            (field_number, line) = self._extract_field_number(line)

            if field_number in self.ignored_field_numbers:
                continue

            if field_number == 1011:
                # Parse the status section, which looks like:
                # 1011-Router ID is 192.168.3.11
                # Current server time is 10-01-2012 10:24:37
                # Last reboot on 03-01-2012 12:46:40
                # Last reconfiguration on 03-01-2012 12:46:40
                data['router_id'] = self._parse_router_status_line(line)
                line = next(line_iterator)  # skip current server time
                data['last_reboot'] = self._parse_router_status_line(
                    next(line_iterator), parse_date=True)
                data['last_reconfiguration'] = self._parse_router_status_line(
                    next(line_iterator), parse_date=True)

        return data

    def _parse_router_status_line(self, line, parse_date=False):
        """Parse a line like:
            Current server time is 10-01-2012 10:24:37
        optionally (if parse_date=True), parse it into a datetime"""
        data = line.strip().split(' ', 3)[-1]
        if parse_date:
            try:
                return datetime.strptime(data, '%Y-%m-%d %H:%M:%S')
            # old versions of bird used DD-MM-YYYY
            except ValueError:
                return datetime.strptime(data, '%d-%m-%Y %H:%M:%S')
        else:
            return data

    def get_peer_prefixes_announced(self, peer_name):
        """Get prefixes announced by a specific peer, without applying
        filters - i.e. this includes routes which were not accepted"""
        if self.dummy:
            return [
                {'prefix': '2001:db8:/32', 'as_path': '65520',
                    'community': '65520:79 65521:421'},
                {'prefix': '2002::/16', 'as_path': '65520',
                    'community': '65520:1234'},
            ]
        clean_peer_name = self._clean_input(peer_name)
        query = "show route table T_%s all protocol %s" % (
            clean_peer_name, clean_peer_name)
        data = self._send_query(query)
        return self._parse_route_data(data)

    def get_peer_prefixes_exported(self, peer_name):
        """Get prefixes exported TO a specific peer"""
        if self.dummy:
            return [
                {'prefix': '2001:db8:/32', 'as_path': '65520',
                    'community': '65520:79 65521:421'},
                {'prefix': '2002::/16', 'as_path': '65520',
                    'community': '65520:1234'},
            ]
{'fraction_non_alphanumeric': 0.11376168784464158, 'fraction_numerical': 0.05562215296092064, 'mean_word_length': 2.0151788941091433, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3664956', 'n_tokens_mistral': 2762, 'n_tokens_neox': 2350, 'n_words': 622}