"""
opbeat.utils
~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2011-2015 Opbeat

Large portions are
:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from opbeat.utils import six

default_ports = {
    "https": 433,
    "http": 80,
    "postgresql": 5432
}

def varmap(func, var, context=None, name=None):
    """
    Executes ``func(key_name, value)`` on all values
    recurisively discovering dict and list scoped
    values.
    """
    if context is None:
        context = {}
    objid = id(var)
    if objid in context:
        return func(name, '<...>')
    context[objid] = 1
    if isinstance(var, dict):
        ret = dict((k, varmap(func, v, context, k)) for k, v in six.iteritems(var))
    elif isinstance(var, (list, tuple)):
        ret = [varmap(func, f, context, name) for f in var]
    else:
        ret = func(name, var)
    del context[objid]
    return ret

def disabled_due_to_debug(opbeat_config, debug):
    """
    Compares module and app configs to determine whether to log to Opbeat
    :param opbeat_config: Dictionary containing module config
    :param debug: Boolean denoting app DEBUG state
    :return: Boolean True if logging is disabled
    """
    return debug and not opbeat_config.get('DEBUG', False)