<commit_before>"""
NTP sources - remote clock info from ``ntpq`` and ``chronyc``
=============================================================

The parsers here provide information about the time sources used by
``ntpd`` and ``chronyd``.  These are gathered from the output of the
``ntpq -pn`` and ``chronyc sources`` commands respectively.

There is also a parser for parsing the output of ``ntpq -c 'rv 0 leap'``
command to give leap second status.

"""

from .. import Parser, parser
from insights.specs import chronyc_sources
from insights.specs import ntpq_leap
from insights.specs import ntpq_pn


@parser(chronyc_sources)
class ChronycSources(Parser):
    """
    Chronyc Sources parser

    Parses the list of NTP time sources in use by ``chronyd``.  So far only
    the source IP address and the mode and the state flags are retrieved.

    Sample input::

        210 Number of sources = 6
        MS Name/IP address         Stratum Poll Reach LastRx Last sample
        ===============================================================================
        ^- 10.20.30.40                   2   9   377    95  -1345us[-1345us] +/-   87ms
        ^- 10.56.72.8                    2  10   377   949  -3449us[-3483us] +/-  120ms
        ^* 10.64.108.95                  2  10   377   371    -91us[ -128us] +/-   30ms
        ^- 10.8.205.17                   2   8   377    27  +7161us[+7161us] +/-   52ms

    Examples:

        >>> sources = shared[ChronycSources].data
        >>> len(sources)
        4
        >>> sources[0]['source']
        '10.20.30.40'
        >>> sources[0]['mode']
        '^'
        >>> sources[0]['state']
        '-'
    """

    def parse_content(self, content):
        """
        Get source, mode and state for chrony
        """
        self.data = []
        for row in content[3:]:
            if row.strip():
                values = row.split(" ", 2)
                self.data.append({"source": values[1], "mode": values[0][0], "state": values[0][1]})


@parser(ntpq_leap)
class NtpqLeap(Parser):
    """
    Converts the output of ``ntpq -c 'rv 0 leap'`` into a dictionary in the
    ``data`` property, and sets the ``leap`` property to the value of the
    'leap' key if found.

    Sample input::

        leap=00

    Examples:

        >>> print shared[NtpqLeap].leap  # same data
        '00'
    """

    def parse_content(self, content):
        line = content[0].strip()
        if "=" in line:
            k, v = line.split("=")
            self.data = {k: v}

    @property
    def leap(self):
        if self.data:
            return self.data.get('leap')


@parser(ntpq_pn)
class NtpqPn(Parser):
    """
    Get source and flag for each NTP time source from the output of `ntpq -pn`.

    Currently, this only captures the source IP address and the 'flag'
    character in the first column at this stage.  Therefore it will need
    to be extended should you wish to determine the stratum, polling rate
    or other properties of the source.

    Sample input::

             remote           refid      st t when poll reach   delay   offset  jitter
        ==============================================================================
        +10.20.30.40     192.168.127.12  3 u  638 1024  377    0.242    2.461   1.886
        *2001:388:608c:8 .GPS.            1 u  371 1024  377   29.323    1.939   1.312
        -2001:4fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b  172.16.31.10  2 u  396 1024  377   37.869   -3.340   6.458
        +150.203.1.10    192.168.3.11    2 u  509 1024  377   20.135    0.800   3.260

    Examples:

        >>> sources = shared[NtpqPn].data
        >>> len(sources)
        4
        >>> sources[0]
        {'flag': '*', 'source', '10.20.30.40'}
    """<commit_msg>Fix bug for "NtpqLeap has no attribute 'data'" (#729)<commit_after>"""
NTP sources - remote clock info from ``ntpq`` and ``chronyc``
=============================================================

The parsers here provide information about the time sources used by
``ntpd`` and ``chronyd``.  These are gathered from the output of the
``ntpq -pn`` and ``chronyc sources`` commands respectively.

There is also a parser for parsing the output of ``ntpq -c 'rv 0 leap'``
command to give leap second status.

"""

from .. import Parser, parser
from insights.specs import chronyc_sources
from insights.specs import ntpq_leap
from insights.specs import ntpq_pn


@parser(chronyc_sources)
class ChronycSources(Parser):
    """
    Chronyc Sources parser

    Parses the list of NTP time sources in use by ``chronyd``.  So far only
    the source IP address and the mode and the state flags are retrieved.

    Sample input::

        210 Number of sources = 6
        MS Name/IP address         Stratum Poll Reach LastRx Last sample
        ===============================================================================
        ^- 10.20.30.40                   2   9   377    95  -1345us[-1345us] +/-   87ms
        ^- 10.56.72.8                    2  10   377   949  -3449us[-3483us] +/-  120ms
        ^* 10.64.108.95                  2  10   377   371    -91us[ -128us] +/-   30ms
        ^- 10.8.205.17                   2   8   377    27  +7161us[+7161us] +/-   52ms

    Examples:

        >>> sources = shared[ChronycSources].data
        >>> len(sources)
        4
        >>> sources[0]['source']
        '10.20.30.40'
        >>> sources[0]['mode']
        '^'
        >>> sources[0]['state']
        '-'
    """

    def parse_content(self, content):
        """
        Get source, mode and state for chrony
        """
        self.data = []
        for row in content[3:]:
            if row.strip():
                values = row.split(" ", 2)
                self.data.append({"source": values[1], "mode": values[0][0], "state": values[0][1]})


@parser(ntpq_leap)
class NtpqLeap(Parser):
    """
    Converts the output of ``ntpq -c 'rv 0 leap'`` into a dictionary in the
    ``data`` property, and sets the ``leap`` property to the value of the
    'leap' key if found.

    Sample input::

        leap=00

    Examples:

        >>> print shared[NtpqLeap].leap  # same data
        '00'
    """

    def parse_content(self, content):
        self.data = {}
        line = content[0].strip()
        if "=" in line:
            k, v = line.split("=")
            self.data = {k: v}

    @property
    def leap(self):
        return self.data.get('leap')


@parser(ntpq_pn)
class NtpqPn(Parser):
    """
    Get source and flag for each NTP time source from the output of `ntpq -pn`.

    Currently, this only captures the source IP address and the 'flag'
    character in the first column at this stage.  Therefore it will need
    to be extended should you wish to determine the stratum, polling rate
    or other properties of the source.

    Sample input::

             remote           refid      st t when poll reach   delay   offset  jitter
        ==============================================================================
        +10.20.30.40     192.168.127.12  3 u  638 1024  377    0.242    2.461   1.886
        *2001:388:608c:8 .GPS.            1 u  371 1024  377   29.323    1.939   1.312
        -200fdf8:f53e:61e4::18  172.16.31.10  2 u  396 1024  377   37.869   -3.340   6.458
        +172.16.31.10    192.168.3.11    2 u  509 1024  377   20.135    0.800   3.260

    Examples:

        >>> sources = shared[NtpqPn].data
        >>> len(sources)
        4
        >>> sources[0]
        {'flag': '*', 'source', '10.20.30.40'}
    """
{'fraction_non_alphanumeric': 0.16925940699375083, 'fraction_numerical': 0.08323361255152241, 'mean_word_length': 2.018459069020867, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 22, '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': '339705', 'n_tokens_mistral': 2720, 'n_tokens_neox': 2252, 'n_words': 751}