# Path: golem/core/parsing/date_utils.py
# def process_datetime(values, duration=None):
# def timedelta_from_grain(grain):
# def date_now(tzinfo):
# def date_today(tzinfo):
# def date_this_week(tzinfo):
# def date_this_month(tzinfo):
# def format_date_interval(from_date, to_date, grain):
# 
# Path: golem/core/parsing/entity_extractor.py
# class EntityExtractor(ABC):
#     """
#     Abstract class for entity extractors.
#     Responsible for processing text and extracting entities such as names, dates, places etc.
#     """
# 
#     def __init__(self):
#         pass
# 
#     @abstractmethod
#     def extract_entities(self, text: str, max_retries=5):
#         """
#         Extracts entities from text (a message from user).
#         :param text:        a string.
#         :param max_retries: how many times to retry on error.
#         :return: A dict of extracted entities.
#                  For example:
#                  {
#                     "place": [
#                         {"value": "Prague"},
#                         {"value": "New York", "metadata": {}}
#                     ],
#                     "name": [{"value": "Golem"}],
#                     ...
#                  }
#         """
#         return dict()


import json
import logging
import requests
from golem.core.parsing import date_utils
from golem.core.parsing.entity_extractor import EntityExtractor
                logging.debug('Duckling:', jsn)
                if jsn is not None:
                    return self.to_entities(jsn)
            else:
                resp.raise_for_status()
        except Exception:
            if max_retries > 0:
                return self.extract_entities(text, max_retries - 1)
            else:
                logging.exception('Exception @ Duckling')
        return {}

    def to_entities(self, jsn):
        """Converts duckling output to the correct format."""
        entities = {}
        for entity in jsn:
            key, value = entity['dim'], entity['value']
            if key == 'time': key = 'datetime'
            if key not in entities:
                entities[key] = []
            entities[key].append(value)
        return self._process_wit_entities(entities)

    def _process_wit_entities(self, entities: dict):

        entities = self._process_metadata(entities)

        if 'datetime' in entities:
            datetime = entities['datetime']
            duration = entities.get('duration', None)

# Path: golem/core/parsing/date_utils.py
# def process_datetime(values, duration=None):
# def timedelta_from_grain(grain):
# def date_now(tzinfo):
# def date_today(tzinfo):
# def date_this_week(tzinfo):
# def date_this_month(tzinfo):
# def format_date_interval(from_date, to_date, grain):
# 
# Path: golem/core/parsing/entity_extractor.py
# class EntityExtractor(ABC):
#     """
#     Abstract class for entity extractors.
#     Responsible for processing text and extracting entities such as names, dates, places etc.
#     """
# 
#     def __init__(self):
#         pass
# 
#     @abstractmethod
#     def extract_entities(self, text: str, max_retries=5):
#         """
#         Extracts entities from text (a message from user).
#         :param text:        a string.
#         :param max_retries: how many times to retry on error.
#         :return: A dict of extracted entities.
#                  For example:
#                  {
#                     "place": [
#                         {"value": "Prague"},
#                         {"value": "New York", "metadata": {}}
#                     ],
#                     "name": [{"value": "Golem"}],
#                     ...
#                  }
#         """
#         return dict()


# Path: golem/core/parsing/duckling_extractor.py
import json
import logging
import requests
from golem.core.parsing import date_utils
from golem.core.parsing.entity_extractor import EntityExtractor

                logging.debug('Duckling:', jsn)
                if jsn is not None:
                    return self.to_entities(jsn)
            else:
                resp.raise_for_status()
        except Exception:
            if max_retries > 0:
                return self.extract_entities(text, max_retries - 1)
            else:
                logging.exception('Exception @ Duckling')
        return {}

    def to_entities(self, jsn):
        """Converts duckling output to the correct format."""
        entities = {}
        for entity in jsn:
            key, value = entity['dim'], entity['value']
            if key == 'time': key = 'datetime'
            if key not in entities:
                entities[key] = []
            entities[key].append(value)
        return self._process_wit_entities(entities)

    def _process_wit_entities(self, entities: dict):

        entities = self._process_metadata(entities)

        if 'datetime' in entities:
            datetime = entities['datetime']
            duration = entities.get('duration', None)

            append = date_utils.process_datetime(datetime, duration)