import six
import json
import pprint


def pkdpretty(obj):
    """Return pretty print the object.

    If `obj` is JSON, parse and print it. If it is a regular python
    data structure, pretty print that. Any exceptions are caught,
    and the return value will be `obj`.

    Args:
        obj (object): JSON string or python object

    Returns:
        str: pretty printed string
    """
    try:
        if isinstance(obj, six.string_types):
            try:
                obj = json.loads(obj)
            except Exception:
                pass
        # try to dump as JSON else dump as Python
        try:
            return json.dumps(
                obj,
                sort_keys=True,
                indent=4,
                separators=(',', ': '),
            ) + '\n'
        except Exception as e:
            pass
        if pprint.isreadable(obj):
            return pprint.pformat(obj, indent=4) + '\n'
    except Exception:
        pass
    return obj