<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals

import logging
import re
from datetime import datetime

import pypuppetdb
from pypuppetdb.QueryBuilder import (EqualsOperator)
from pypuppetdb.api.base import BaseAPI
from pypuppetdb.errors import (APIError)
from pypuppetdb.types import (Catalog, Node, Report)

log = logging.getLogger(__name__)


class PqlAPI(BaseAPI):
    """
    This class provides methods that interact with the "pdb/query/v4"
    PuppetDB API endpoint.
    """

    def _pql(self, pql, request_method='GET'):
        """This method prepares a PQL query to PuppetDB. Actual making
        the HTTP request is done by _make_request().

        :param pql: PQL query
        :type pql: :obj:`string`

        :param request_method: (optional) GET or POST, the default is GET

        :raises: :class:`~pypuppetdb.errors.EmptyResponseError`

        :returns: The decoded response from PuppetDB
        :rtype: :obj:`dict` or :obj:`list`
        """

        log.debug(f"_pql called with pql={pql}, request_method={request_method}")

        pql = pql.strip()
        if not pql:
            log.error("Non-empty PQL query is required!")
            raise APIError

        payload = {}

        # PQL queries are made to the same endpoint regardless of the queried entities
        url = self._url('pql')
        payload['query'] = pql

        return self._make_request(url, request_method, payload)

    # TODO: deduplicate this - see QueryAPI.nodes()
    def pql(self, pql, with_status=False, unreported=2, with_event_numbers=True):
        """Makes a PQL (Puppet Query Language) and tries to cast results
         to a rich type. If it won't work, returns plain dicts.
<commit_msg>Fix formatting in generated docs<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals

import logging
import re
from datetime import datetime

import pypuppetdb
from pypuppetdb.QueryBuilder import (EqualsOperator)
from pypuppetdb.api.base import BaseAPI
from pypuppetdb.errors import (APIError)
from pypuppetdb.types import (Catalog, Node, Report)

log = logging.getLogger(__name__)


class PqlAPI(BaseAPI):
    """
    This class provides methods that interact with the "pdb/query/v4"
    PuppetDB API endpoint.
    """

    def _pql(self, pql, request_method='GET'):
        """This method prepares a PQL query to PuppetDB. Actual making
        the HTTP request is done by _make_request().

        :param pql: PQL query
        :type pql: :obj:`string`

        :param request_method: (optional) GET or POST, the default is GET

        :raises: :class:`~pypuppetdb.errors.EmptyResponseError`

        :returns: The decoded response from PuppetDB
        :rtype: :obj:`dict` or :obj:`list`
        """

        log.debug(f"_pql called with pql={pql}, request_method={request_method}")

        pql = pql.strip()
        if not pql:
            log.error("Non-empty PQL query is required!")
            raise APIError

        payload = {}

        # PQL queries are made to the same endpoint regardless of the queried entities
        url = self._url('pql')
        payload['query'] = pql

        return self._make_request(url, request_method, payload)

    # TODO: deduplicate this - see QueryAPI.nodes()
    def pql(self, pql, with_status=False, unreported=2, with_event_numbers=True):
        """Makes a PQL (Puppet Query Language) and tries to cast results
        to a rich type. If it won't work, returns plain dicts.

{'fraction_non_alphanumeric': 0.08811824900511654, 'fraction_numerical': 0.0011370096645821489, 'mean_word_length': 3.0729166666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6920988', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1036, 'n_words': 352}