<commit_before># -*- coding: utf-8 -*-
"""
.. module:: MyCapytain.resolvers.cts.remote
   :synopsis: Resolver built for CTS APIs

.. moduleauthor:: <NAME> <<EMAIL>>


"""

from typing import Union, Optional, Any, Dict
import re

from MyCapytain.resolvers.prototypes import Resolver
from MyCapytain.common.reference import BaseReference, BaseReferenceSet, \
    DtsReference, DtsReferenceSet, DtsCitation
from MyCapytain.retrievers.dts import HttpDtsRetriever
from MyCapytain.common.utils.dts import parse_metadata
from MyCapytain.resources.collections.dts import DtsCollection

from pyld.jsonld import expand


__all__ = [
    "HttpDtsResolver"
]

_empty = [{"@value": None}]
_re_page = re.compile("page=(\d+)")


def _parse_ref(ref_dict, default_type :str =None):
    if "https://w3id.org/dts/api#ref" in ref_dict:
        refs = ref_dict["https://w3id.org/dts/api#ref"][0]["@value"],
    elif "https://w3id.org/dts/api#start" in ref_dict and \
         "https://w3id.org/dts/api#end" in ref_dict:
        refs = (
            ref_dict["https://w3id.org/dts/api#start"][0]["@value"],
            ref_dict["https://w3id.org/dts/api#end"][0]["@value"]
        )
    else:
        return None  # Maybe Raise ?
    type_ = default_type
    if "https://w3id.org/dts/api#citeType" in ref_dict:
        type_ = ref_dict["https://w3id.org/dts/api#citeType"][0]["@value"]

    obj = DtsReference(*refs, type_=type_)
    parse_metadata(obj.metadata, ref_dict)

    return obj


class HttpDtsResolver(Resolver):
    """ HttpDtsResolver provide a resolver for DTS API http endpoint.

    :param endpoint: DTS API Retriever
    """
    def __init__(self, endpoint: Union[str, HttpDtsRetriever]):
        if not isinstance(endpoint, HttpDtsRetriever):
            endpoint = HttpDtsRetriever(endpoint)
        self._endpoint = endpoint

    @property
    def endpoint(self) -> HttpDtsRetriever:
        """ DTS Endpoint of the resolver

        :return: DTS Endpoint
        :rtype: HttpDtsRetriever
        """
        return self._endpoint

    def getMetadata(self, objectId: str=None, **filters) -> DtsCollection:
        req = self.endpoint.get_collection(objectId)
        req.raise_for_status()

        collection = DtsCollection.parse(req.json())
        # Pagination is not completed upon first query.
        # Pagination will be treated direction in the DtsCollection

        return collection

    def getReffs(
            self,
            textId: str,
            level: int=1,
            subreference: Union[str, BaseReference]=None,
            include_descendants: bool=False,
            additional_parameters: Optional[Dict[str, Any]]=None
    ) -> DtsReferenceSet:
        if not additional_parameters:
            additional_parameters = {}

        references = []
        default_type = None
        level_ = level

        page = 1
        while page:
            kwargs = dict(
                level = level, ref = subreference,
                exclude=additional_parameters.get("exclude", None),<commit_msg>(DTSResolver) Formatting<commit_after># -*- coding: utf-8 -*-
"""
.. module:: MyCapytain.resolvers.cts.remote
   :synopsis: Resolver built for CTS APIs

.. moduleauthor:: <NAME> <<EMAIL>>


"""

from typing import Union, Optional, Any, Dict
import re

from MyCapytain.resolvers.prototypes import Resolver
from MyCapytain.common.reference import BaseReference, BaseReferenceSet, \
    DtsReference, DtsReferenceSet, DtsCitation
from MyCapytain.retrievers.dts import HttpDtsRetriever
from MyCapytain.common.utils.dts import parse_metadata
from MyCapytain.resources.collections.dts import DtsCollection

from pyld.jsonld import expand


__all__ = [
    "HttpDtsResolver"
]

_empty = [{"@value": None}]
_re_page = re.compile("page=(\d+)")


def _parse_ref(ref_dict, default_type :str =None):
    if "https://w3id.org/dts/api#ref" in ref_dict:
        refs = ref_dict["https://w3id.org/dts/api#ref"][0]["@value"],
    elif "https://w3id.org/dts/api#start" in ref_dict and \
         "https://w3id.org/dts/api#end" in ref_dict:
        refs = (
            ref_dict["https://w3id.org/dts/api#start"][0]["@value"],
            ref_dict["https://w3id.org/dts/api#end"][0]["@value"]
        )
    else:
        return None  # Maybe Raise ?
    type_ = default_type
    if "https://w3id.org/dts/api#citeType" in ref_dict:
        type_ = ref_dict["https://w3id.org/dts/api#citeType"][0]["@value"]

    obj = DtsReference(*refs, type_=type_)
    parse_metadata(obj.metadata, ref_dict)

    return obj


class HttpDtsResolver(Resolver):
    """ HttpDtsResolver provide a resolver for DTS API http endpoint.

    :param endpoint: DTS API Retriever
    """
    def __init__(self, endpoint: Union[str, HttpDtsRetriever]):
        if not isinstance(endpoint, HttpDtsRetriever):
            endpoint = HttpDtsRetriever(endpoint)
        self._endpoint = endpoint

    @property
    def endpoint(self) -> HttpDtsRetriever:
        """ DTS Endpoint of the resolver

        :return: DTS Endpoint
        :rtype: HttpDtsRetriever
        """
        return self._endpoint

    def getMetadata(self, objectId: str=None, **filters) -> DtsCollection:
        req = self.endpoint.get_collection(objectId)
        req.raise_for_status()

        collection = DtsCollection.parse(req.json())
        # Pagination is not completed upon first query.
        # Pagination will be treated direction in the DtsCollection

        return collection

    def getReffs(
            self,
            textId: str,
            level: int=1,
            subreference: Union[str, BaseReference]=None,
            include_descendants: bool=False,
            additional_parameters: Optional[Dict[str, Any]]=None
    ) -> DtsReferenceSet:
        if not additional_parameters:
            additional_parameters = {}

        references = []
        default_type = None
        level_ = level

        page = 1
        while page:
            kwargs = dict(
                level=level, ref=subreference,
                exclude=additional_parameters.get("exclude", None),
{'fraction_non_alphanumeric': 0.11560693641618497, 'fraction_numerical': 0.00495458298926507, 'mean_word_length': 2.7568238213399505, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 16, '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': '3810906', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1885, 'n_words': 418}