<commit_before>"""
Created on 07 Apr 2021

@author: <NAME> (<EMAIL>)

https://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python
"""

from collections import OrderedDict
from enum import Enum
from http import HTTPStatus

from scs_core.client.http_response import HTTPResponse

from scs_core.data.str import Str

from scs_core.sample.configuration_sample import ConfigurationSample


# --------------------------------------------------------------------------------------------------------------------

class ConfigurationFinder(object):
    """
    classdocs
    """

    __URL = "https://bwhogrzl3b.execute-api.us-west-2.amazonaws.com/default/MQTTDynamoHandler"

    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, http_client, auth):
        self.__http_client = http_client
        self.__auth = auth


    # ----------------------------------------------------------------------------------------------------------------

    def find(self, tag_filter, response_mode):
        # TODO: This is a temporary basic auth, will be updated with cognito pools prob
        headers = {'Authorization': 'scs12'}
        request = ConfigurationRequest(tag_filter, response_mode)

        body = self.__http_client.get(self.__URL, headers=headers, params=request.params())

        print("body: %s" % body)
        print("-")

        return ConfigurationResponse.construct_from_jdict(body.json())


    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "ConfigurationFinder:{auth:%s}" % self.__auth


# --------------------------------------------------------------------------------------------------------------------

class ConfigurationRequest(object):
    """
    classdocs
    """

    MODE = Enum('Mode', 'FULL TAGS_ONLY HISTORY')

    TAG_FILTER = 'tagFilter'
    RESPONSE_MODE = 'responseMode'


    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def construct_from_qsp(cls, qsp):
        if not qsp:
            return None

        # compulsory...
        tag_filter = qsp.get(cls.TAG_FILTER)

        try:
            response_mode = cls.MODE[qsp.get(cls.RESPONSE_MODE)]
        except KeyError:
            response_mode = None

        return cls(tag_filter, response_mode)


    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, tag_filter, response_mode):
        """
        Constructor
        """
        self.__tag_filter = tag_filter                          # string
        self.__response_mode = response_mode                    # MODE enum


    # ----------------------------------------------------------------------------------------------------------------

    def is_valid(self):
        if self.response_mode is None:
            return False


    def tags_only(self):
        return self.__response_mode == self.MODE.TAGS_ONLY


    def history(self):
        return self.__response_mode == self.MODE.HISTORY


    # ----------------------------------------------------------------------------------------------------------------

    def params(self):
        params = {
            self.TAG_FILTER: self.tag_filter,
            self.RESPONSE_MODE: self.response_mode.name
        }

        return params


    # ----------------------------------------------------------------------------------------------------------------

    @property
    def tag_filter(self):
        return self.__tag_filter


    @property
    def response_mode(self):
        return self.__response_mode


    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "ConfigurationRequest:{tag_filter:%s, response_mode:%s}" % \
               (self.tag_filter, self.response_mode)


# --------------------------------------------------------------------------------------------------------------------

class ConfigurationResponse(HTTPResponse):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def construct_from_jdict(cls, jdict):
        print(jdict)
        print("-")

        if not jdict:
            return None

        status = HTTPStatus(jdict.get('statusCode'))

        try:
            mode = ConfigurationRequest.MODE[jdict.get('mode')]
        except KeyError:
            mode = None

        items = []
        if jdict.get('Items'):
            for item_jdict in jdict.get('Items'):
                item = ConfigurationSample.construct_from_jdict(item_jdict)      # TODO: class depends on mode
                items.append(item)

        next_url = jdict.get('next')

        return cls(status, mode, items, next_url)


    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, status, mode=None, items=(), next_url=None):
        """
        Constructor
        """
        super().__init__(status)

        self.__mode = mode                          # ConfigurationRequest.Mode
        self.__items = items                        # list of ConfigurationSample or string
        self.__next_url = next_url                  # URL string

<commit_msg>Refactored ConfigurationFinder<commit_after>"""
Created on 07 Apr 2021

@author: <NAME> (<EMAIL>)

https://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python
"""

from collections import OrderedDict
from enum import Enum
from http import HTTPStatus

from scs_core.client.http_response import HTTPResponse

from scs_core.data.str import Str

from scs_core.sample.configuration_sample import ConfigurationSample


# --------------------------------------------------------------------------------------------------------------------

class ConfigurationFinder(object):
    """
    classdocs
    """

    __URL = "https://bwhogrzl3b.execute-api.us-west-2.amazonaws.com/default/MQTTDynamoHandler"

    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, http_client, auth):
        self.__http_client = http_client
        self.__auth = auth


    # ----------------------------------------------------------------------------------------------------------------

    def find(self, tag_filter, response_mode):
        # TODO: This is a temporary basic auth, will be updated with cognito pools prob
        headers = {'Authorization': 'scs12'}
        request = ConfigurationRequest(tag_filter, response_mode)

        body = self.__http_client.get(self.__URL, headers=headers, params=request.params())

        print("body: %s" % body)
        print("-")

        return ConfigurationResponse.construct_from_jdict(body.json())


    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "ConfigurationFinder:{auth:%s}" % self.__auth


# --------------------------------------------------------------------------------------------------------------------

class ConfigurationRequest(object):
    """
    classdocs
    """

    MODE = Enum('Mode', 'FULL TAGS_ONLY HISTORY')

    TAG_FILTER = 'tagFilter'
    RESPONSE_MODE = 'responseMode'


    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def construct_from_qsp(cls, qsp):
        if not qsp:
            return None

        # compulsory...
        tag_filter = qsp.get(cls.TAG_FILTER)

        try:
            response_mode = cls.MODE[qsp.get(cls.RESPONSE_MODE)]
        except KeyError:
            response_mode = None

        return cls(tag_filter, response_mode)


    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, tag_filter, response_mode):
        """
        Constructor
        """
        self.__tag_filter = tag_filter                          # string
        self.__response_mode = response_mode                    # MODE enum


    # ----------------------------------------------------------------------------------------------------------------

    def is_valid(self):
        if self.response_mode is None:
            return False


    def tags_only(self):
        return self.__response_mode == self.MODE.TAGS_ONLY


    def history(self):
        return self.__response_mode == self.MODE.HISTORY


    # ----------------------------------------------------------------------------------------------------------------

    def params(self):
        params = {
            self.TAG_FILTER: self.tag_filter,
            self.RESPONSE_MODE: self.response_mode.name
        }

        return params


    # ----------------------------------------------------------------------------------------------------------------

    @property
    def tag_filter(self):
        return self.__tag_filter


    @property
    def response_mode(self):
        return self.__response_mode


    # ----------------------------------------------------------------------------------------------------------------

    def __str__(self, *args, **kwargs):
        return "ConfigurationRequest:{tag_filter:%s, response_mode:%s}" % \
               (self.tag_filter, self.response_mode)


# --------------------------------------------------------------------------------------------------------------------

class ConfigurationResponse(HTTPResponse):
    """
    classdocs
    """

    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    def construct_from_jdict(cls, jdict):
        print(jdict)
        print("-")

        if not jdict:
            return None

        status = HTTPStatus(jdict.get('statusCode'))

        try:
            mode = ConfigurationRequest.MODE[jdict.get('mode')]
        except KeyError:
            mode = None

        items = None
        if jdict.get('Items'):
            items = []
            for item_jdict in jdict.get('Items'):
                item = ConfigurationSample.construct_from_jdict(item_jdict)      # TODO: class depends on mode
                items.append(item)

        next_url = jdict.get('next')

        return cls(status, mode=mode, items=items, next_url=next_url)


    # ----------------------------------------------------------------------------------------------------------------

    def __init__(self, status, mode=None, items=None, next_url=None):
        """
        Constructor
        """
        super().__init__(status)

        self.__mode = mode                          # ConfigurationRequest.Mode
        self.__items = items                        # list of ConfigurationSample or string
        self.__next_url = next_url                  # URL string


{'fraction_non_alphanumeric': 0.3512316143895091, 'fraction_numerical': 0.002658160552897395, 'mean_word_length': 0.9517551443887255, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, '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': '522525', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2326, 'n_words': 497}