""" Functions for helping with serialization and deserialization of
Bokeh objects.

"""
from __future__ import absolute_import

from six import iterkeys
import numpy as np
try:
    import pandas as pd
    is_pandas = True
except ImportError:
    is_pandas = False

import logging
log = logging.getLogger(__name__)

_simple_id = 1000

def make_id():
    """ Return a new unique ID for a Bokeh object.

    Normally this function will return UUIDs to use for identifying Bokeh
    objects. This is especally important for Bokeh objects stored on a
    Bokeh server. However, it is convenient to have more human-readable
    IDs during development, so this behavior can be overridden by
    setting the environment variable ``BOKEH_SIMPLE_IDS=yes``.

    """
    global _simple_id

    import uuid
    from ..settings import settings

    if settings.simple_ids(False):
        _simple_id += 1
        new_id = _simple_id
    else:
        new_id = uuid.uuid4()
    return str(new_id)

def urljoin(*args):
    """ Construct an absolute URL from several URL components.

    Args:
        *args (str) : URL components to join

    Returns:
        str : joined URL

    """
    from six.moves.urllib.parse import urljoin as sys_urljoin
    from functools import reduce
    return reduce(sys_urljoin, args)

def get_json(response):
    """ Unify retrieving JSON responses from different sources.

    Works correctly for HTTP responses from requests <=1.0, >1.0, and
    the Flask test client.

    Args:
        response (Flask or requests response) : a response to process

    Returns:
        JSON

    """
    import json
    try:
        import flask
    except ImportError:
        flask = None