<commit_before>import time

import sys

from retry import retry
from pyCGA.Utils.AvroSchema import AvroSchemaFile

from pyCGA.commons import execute, OpenCGAResponseList
from pyCGA.opencgaconfig import ConfigClient


class _ParentRestClient(object):
    """Queries the REST service given the different query params"""

    def __init__(self, configuration, category, session_id=None, login_handler=None):
        """
        :param login_handler: a parameterless method that can log in this connector
        and return a session id
        """
        self._cfg = configuration
        self._category = category
        self.session_id = session_id
        self.login_handler = login_handler
        self.on_retry = None

    def _client_login_handler(self):
        if self.login_handler:
            self.session_id = self.login_handler()

    @staticmethod
    def _get_query_id_str(query_ids):
        if query_ids is None:
            return None
        elif isinstance(query_ids, list):
            return ','.join(map(str, query_ids))
        else:
            return str(query_ids)

    def _rest_retry(self, method, resource, query_id=None, subcategory=None,
                    second_query_id=None, data=None, **options):
        """Invokes the specified HTTP method, with retries if they are specified in the configuration
        :return: an instance of OpenCGAResponseList"""

        query_ids_str = self._get_query_id_str(query_id)

        def exec_retry():
            return execute(host=self._cfg.host,
                           version=self._cfg.version,
                           sid=self.session_id,
                           category=self._category,
                           subcategory=subcategory,
                           method=method,
                           query_id=query_ids_str,
                           second_query_id=second_query_id,
                           resource=resource,
                           data=data,
                           options=options)

        def notify_retry(exc_type, exc_val, exc_tb):
            if self.on_retry:
                self.on_retry(self, exc_type, exc_val, exc_tb, dict(
                    method=method, resource=resource, query_id=query_id,
                    category=self._category, subcategory=subcategory,
                    second_query_id=second_query_id, data=data,
                    options=options
                ))

        response = retry(
            exec_retry, self._cfg.max_attempts, self._cfg.min_retry_secs, self._cfg.max_retry_secs,
            login_handler=self._client_login_handler if self.login_handler else None,
            on_retry=notify_retry)

        return OpenCGAResponseList(response, query_ids_str)

    def _get(self, resource, query_id=None, subcategory=None, second_query_id=None, **options):
        """Queries the REST service and returns the result"""
        return self._rest_retry('get', resource, query_id, subcategory, second_query_id, **options)

    def _post(self, resource, data, query_id=None, subcategory=None, second_query_id=None, **options):
        """Queries the REST service and returns the result"""
        return self._rest_retry('post', resource, query_id, subcategory, second_query_id, data=data, **options)


class _ParentBasicCRUDClient(_ParentRestClient):
    def create(self, data, **options):
        return self._post('create', data=data, **options)

    def info(self, query_id, **options):
        return self._get('info', query_id=query_id, **options)

    def update(self, query_id, data, **options):
        return self._post('update', query_id=query_id, data=data, **options)

    def delete(self, query_id, **options):
        return self._get('delete', query_id=query_id, **options)


class _ParentAclRestClient(_ParentRestClient):
    def acl(self, query_id, **options):
        """
        acl info

        :param query_id:
        :param options:
        """

        return self._get('acl', query_id=query_id, **options)

    def acl_create(self, query_id, data, **options):
        """
        create acl

        :param query_id:
        :param options:
        """

        return self._post('acl', query_id=query_id, subcategory='create', data=data, **options)

    def acl_delete(self, query_id, memberId, **options):
        """
        delete acl

        :param query_id:
        :param options:
        """

        return self._get('acl', query_id=query_id, subcategory='delete', second_query_id=memberId, **options)

    def acl_info(self, query_id, memberId, **options):
        """
        info acl

        :param query_id:
        :param options:
        """

        return self._get('acl', query_id=query_id, subcategory='info', second_query_id=memberId, **options)

    def acl_update(self, query_id, memberId, data, **options):
        """
        info acl

        :param query_id:
        :param options:
        """

        return self._post('acl', query_id=query_id, subcategory='update', second_query_id=memberId, data=data,
                          **options)


class _ParentAnnotationSetRestClient(_ParentRestClient):
    def annotationsets_search(self, query_id, study, **options):
        """
        annotationsets search

        :param query_id:
        :param options:
        """<commit_msg>adapted acl methods to new version<commit_after>import time

import sys

from retry import retry
from pyCGA.Utils.AvroSchema import AvroSchemaFile

from pyCGA.commons import execute, OpenCGAResponseList
from pyCGA.opencgaconfig import ConfigClient


class _ParentRestClient(object):
    """Queries the REST service given the different query params"""

    def __init__(self, configuration, category, session_id=None, login_handler=None):
        """
        :param login_handler: a parameterless method that can log in this connector
        and return a session id
        """
        self._cfg = configuration
        self._category = category
        self.session_id = session_id
        self.login_handler = login_handler
        self.on_retry = None

    def _client_login_handler(self):
        if self.login_handler:
            self.session_id = self.login_handler()

    @staticmethod
    def _get_query_id_str(query_ids):
        if query_ids is None:
            return None
        elif isinstance(query_ids, list):
            return ','.join(map(str, query_ids))
        else:
            return str(query_ids)

    def _rest_retry(self, method, resource, query_id=None, subcategory=None,
                    second_query_id=None, data=None, **options):
        """Invokes the specified HTTP method, with retries if they are specified in the configuration
        :return: an instance of OpenCGAResponseList"""

        query_ids_str = self._get_query_id_str(query_id)

        def exec_retry():
            return execute(host=self._cfg.host,
                           version=self._cfg.version,
                           sid=self.session_id,
                           category=self._category,
                           subcategory=subcategory,
                           method=method,
                           query_id=query_ids_str,
                           second_query_id=second_query_id,
                           resource=resource,
                           data=data,
                           options=options)

        def notify_retry(exc_type, exc_val, exc_tb):
            if self.on_retry:
                self.on_retry(self, exc_type, exc_val, exc_tb, dict(
                    method=method, resource=resource, query_id=query_id,
                    category=self._category, subcategory=subcategory,
                    second_query_id=second_query_id, data=data,
                    options=options
                ))

        response = retry(
            exec_retry, self._cfg.max_attempts, self._cfg.min_retry_secs, self._cfg.max_retry_secs,
            login_handler=self._client_login_handler if self.login_handler else None,
            on_retry=notify_retry)

        return OpenCGAResponseList(response, query_ids_str)

    def _get(self, resource, query_id=None, subcategory=None, second_query_id=None, **options):
        """Queries the REST service and returns the result"""
        return self._rest_retry('get', resource, query_id, subcategory, second_query_id, **options)

    def _post(self, resource, data, query_id=None, subcategory=None, second_query_id=None, **options):
        """Queries the REST service and returns the result"""
        return self._rest_retry('post', resource, query_id, subcategory, second_query_id, data=data, **options)


class _ParentBasicCRUDClient(_ParentRestClient):
    def create(self, data, **options):
        return self._post('create', data=data, **options)

    def info(self, query_id, **options):
        return self._get('info', query_id=query_id, **options)

    def update(self, query_id, data, **options):
        return self._post('update', query_id=query_id, data=data, **options)

    def delete(self, query_id, **options):
        return self._get('delete', query_id=query_id, **options)


class _ParentAclRestClient(_ParentRestClient):
    def acl(self, query_id, **options):
        """
        acl info

        :param query_id:
        :param options:
        """

        return self._get('acl', query_id=query_id, **options)

    def acl_update(self, memberId, data, **options):
        """
        update acl

        :param query_id:
        :param options:
        """

        return self._post('acl', subcategory='update', second_query_id=memberId, data=data, **options)


class _ParentAnnotationSetRestClient(_ParentRestClient):
    def annotationsets_search(self, query_id, study, **options):
        """
        annotationsets search

        :param query_id:
        :param options:
        """
{'fraction_non_alphanumeric': 0.09474862609403623, 'fraction_numerical': 0.0, 'mean_word_length': 2.413337964571032, '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': '3747650', 'n_tokens_mistral': 2794, 'n_tokens_neox': 2731, 'n_words': 646}