# Path: configman/datetime_util.py
# def datetime_from_ISO_string(s):
#     """ Take an ISO date string of the form YYYY-MM-DDTHH:MM:SS.S
#     and convert it into an instance of datetime.datetime
#     """
#     try:
#         return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')
#     except ValueError:
#         try:
#             return datetime.datetime.strptime(s, '%Y-%m-%d')
#         except ValueError:
#             return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
# 
# def date_from_ISO_string(s):
#     """ Take an ISO date string of the form YYYY-MM-DD
#     and convert it into an instance of datetime.date
#     """
#     return datetime.datetime.strptime(s, '%Y-%m-%d').date()
# 
# def datetime_to_ISO_string(aDate):
#     """ Take a datetime and convert to string of the form YYYY-MM-DDTHH:MM:SS.S
#     """
#     return aDate.isoformat()
# 
# def date_to_ISO_string(aDate):
#     """ Take a datetime and convert to string of the form YYYY-MM-DD
#     """
#     return aDate.strftime('%Y-%m-%d')
# 
# Path: configman/datetime_util.py
# def str_to_timedelta(input_str):
#     """ a string conversion function for timedelta for strings in the format
#     DD:HH:MM:SS or D HH:MM:SS
#     """
#     try:
#         input_str = input_str.replace(' ', ':')
#     except (TypeError, AttributeError):
#         from configman.converters import to_str
#         raise TypeError('%s should have been a string' % to_str(input_str))
#     days, hours, minutes, seconds = 0, 0, 0, 0
#     details = input_str.split(':')
#     if len(details) >= 4:
#         days = int(details[-4])
#     if len(details) >= 3:
#         hours = int(details[-3])
#     if len(details) >= 2:
#         minutes = int(details[-2])
#     if len(details) >= 1:
#         seconds = int(details[-1])
#     return datetime.timedelta(days=days,
#                               hours=hours,
#                               minutes=minutes,
#                               seconds=seconds)
# 
# def timedelta_to_str(aTimedelta):
#     """ a conversion function for time deltas to string in the form
#     DD:HH:MM:SS
#     """
#     days = aTimedelta.days
#     temp_seconds = aTimedelta.seconds
#     hours = int(temp_seconds / 3600)
#     minutes = int((temp_seconds - hours * 3600) / 60)
#     seconds = temp_seconds - hours * 3600 - minutes * 60
#     return '%d %02d:%02d:%02d' % (days, hours, minutes, seconds)


import sys
import re
import datetime
import types
import json
import six
import six.moves.builtins as builtins
from configman.datetime_util import (
    datetime_from_ISO_string,
    date_from_ISO_string,
    datetime_to_ISO_string,
    date_to_ISO_string,
)
from configman.config_exceptions import CannotConvertError
from configman.datetime_util import (
    str_to_timedelta,  # don't worry about pyflakes here, unused in this file
    timedelta_to_str,  # but used elsewhere
)
    from configman.namespace import Namespace
    from configman.required_config import RequiredConfig


#------------------------------------------------------------------------------
def py2_to_str(a_unicode):
    return six.text_type(a_unicode)

def py3_to_str(a_bytes):
    return a_bytes.decode('utf-8')

#------------------------------------------------------------------------------
known_mapping_type_to_str = {}
for key, val in sorted(builtins.__dict__.items()):
    if val not in (True, False, list, dict):
        try:
            known_mapping_type_to_str[val] = key
        except TypeError:
            pass


#------------------------------------------------------------------------------
to_string_converters = {
    int: str,
    float: str,
    str: str,
    list: list_to_str,
    tuple: list_to_str,
    bool: lambda x: 'True' if x else 'False',
    dict: json.dumps,
    datetime.datetime: datetime_to_ISO_string,
    datetime.date: date_to_ISO_string,

# Path: configman/datetime_util.py
# def datetime_from_ISO_string(s):
#     """ Take an ISO date string of the form YYYY-MM-DDTHH:MM:SS.S
#     and convert it into an instance of datetime.datetime
#     """
#     try:
#         return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')
#     except ValueError:
#         try:
#             return datetime.datetime.strptime(s, '%Y-%m-%d')
#         except ValueError:
#             return datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
# 
# def date_from_ISO_string(s):
#     """ Take an ISO date string of the form YYYY-MM-DD
#     and convert it into an instance of datetime.date
#     """
#     return datetime.datetime.strptime(s, '%Y-%m-%d').date()
# 
# def datetime_to_ISO_string(aDate):
#     """ Take a datetime and convert to string of the form YYYY-MM-DDTHH:MM:SS.S
#     """
#     return aDate.isoformat()
# 
# def date_to_ISO_string(aDate):
#     """ Take a datetime and convert to string of the form YYYY-MM-DD
#     """
#     return aDate.strftime('%Y-%m-%d')
# 
# Path: configman/datetime_util.py
# def str_to_timedelta(input_str):
#     """ a string conversion function for timedelta for strings in the format
#     DD:HH:MM:SS or D HH:MM:SS
#     """
#     try:
#         input_str = input_str.replace(' ', ':')
#     except (TypeError, AttributeError):
#         from configman.converters import to_str
#         raise TypeError('%s should have been a string' % to_str(input_str))
#     days, hours, minutes, seconds = 0, 0, 0, 0
#     details = input_str.split(':')
#     if len(details) >= 4:
#         days = int(details[-4])
#     if len(details) >= 3:
#         hours = int(details[-3])
#     if len(details) >= 2:
#         minutes = int(details[-2])
#     if len(details) >= 1:
#         seconds = int(details[-1])
#     return datetime.timedelta(days=days,
#                               hours=hours,
#                               minutes=minutes,
#                               seconds=seconds)
# 
# def timedelta_to_str(aTimedelta):
#     """ a conversion function for time deltas to string in the form
#     DD:HH:MM:SS
#     """
#     days = aTimedelta.days
#     temp_seconds = aTimedelta.seconds
#     hours = int(temp_seconds / 3600)
#     minutes = int((temp_seconds - hours * 3600) / 60)
#     seconds = temp_seconds - hours * 3600 - minutes * 60
#     return '%d %02d:%02d:%02d' % (days, hours, minutes, seconds)


# Path: configman/converters.py
import sys
import re
import datetime
import types
import json
import six
import six.moves.builtins as builtins
from configman.datetime_util import (
    datetime_from_ISO_string,
    date_from_ISO_string,
    datetime_to_ISO_string,
    date_to_ISO_string,
)
from configman.config_exceptions import CannotConvertError
from configman.datetime_util import (
    str_to_timedelta,  # don't worry about pyflakes here, unused in this file
    timedelta_to_str,  # but used elsewhere
)
    from configman.namespace import Namespace
    from configman.required_config import RequiredConfig



#------------------------------------------------------------------------------
def py2_to_str(a_unicode):
    return six.text_type(a_unicode)

def py3_to_str(a_bytes):
    return a_bytes.decode('utf-8')

#------------------------------------------------------------------------------
known_mapping_type_to_str = {}
for key, val in sorted(builtins.__dict__.items()):
    if val not in (True, False, list, dict):
        try:
            known_mapping_type_to_str[val] = key
        except TypeError:
            pass


#------------------------------------------------------------------------------
to_string_converters = {
    int: str,
    float: str,
    str: str,
    list: list_to_str,
    tuple: list_to_str,
    bool: lambda x: 'True' if x else 'False',
    dict: json.dumps,
    datetime.datetime: datetime_to_ISO_string,
    datetime.date: date_to_ISO_string,

    datetime.timedelta: timedelta_to_str,