# This file was automatically generated by SWIG (http://www.swig.org).
# Version 3.0.7
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.





from sys import version_info
if version_info >= (3, 0, 0):
    new_instancemethod = lambda func, inst, cls: _port_log.SWIG_PyInstanceMethod_New(func)
else:
    from new import instancemethod as new_instancemethod
if version_info >= (2, 6, 0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_port_log', [dirname(__file__)])
        except ImportError:
            import _port_log
            return _port_log
        if fp is not None:
            try:
                _mod = imp.load_module('_port_log', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _port_log = swig_import_helper()
    del swig_import_helper
else:
    import _port_log
del version_info
try:
    _swig_property = property
except NameError:
    pass  # Python < 2.2 doesn't have 'property'.


def _swig_setattr_nondynamic(self, class_type, name, value, static=1):
    if (name == "thisown"):
        return self.this.own(value)
    if (name == "this"):
        if type(value).__name__ == 'SwigPyObject':
            self.__dict__[name] = value
            return
    method = class_type.__swig_setmethods__.get(name, None)
    if method:
        return method(self, value)
    if (not static):
        object.__setattr__(self, name, value)
    else:
        raise AttributeError("You cannot add attributes to %s" % self)


def _swig_setattr(self, class_type, name, value):
    return _swig_setattr_nondynamic(self, class_type, name, value, 0)


def _swig_getattr_nondynamic(self, class_type, name, static=1):
    if (name == "thisown"):
        return self.this.own()
    method = class_type.__swig_getmethods__.get(name, None)
    if method:
        return method(self)
    if (not static):
        return object.__getattr__(self, name)
    else:
        raise AttributeError(name)

def _swig_getattr(self, class_type, name):
    return _swig_getattr_nondynamic(self, class_type, name, 0)


def _swig_repr(self):
    try:
        strthis = "proxy of " + self.this.__repr__()
    except:
        strthis = ""
    return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)

try:
    _object = object
    _newclass = 1
except AttributeError:
    class _object:
        pass
    _newclass = 0



def _swig_setattr_nondynamic_method(set):
    def set_attr(self, name, value):
        if (name == "thisown"):
            return self.this.own(value)
        if hasattr(self, name) or (name == "this"):
            set(self, name, value)
        else:
            raise AttributeError("You cannot add attributes to %s" % self)
    return set_attr



def gp_log_add_func(*args):
    """
    gp_log_add_func(level, func, data) -> int

    Parameters:
        level: enum GPLogLevel
        func: callable
        data: object (optional)

    """
    return _port_log.gp_log_add_func(*args)

def gp_log_remove_func(id):
    """
    gp_log_remove_func(id) -> int

    Parameters:
        id: int

    """
    return _port_log.gp_log_remove_func(id)

def gp_log_add_func_py(level, func):
    """
    gp_log_add_func_py(level, func) -> int

    Parameters:
        level: enum GPLogLevel
        func: callable

    """
    return _port_log.gp_log_add_func_py(level, func)

def gp_log_remove_func_py(id):
    """
    gp_log_remove_func_py(id) -> int

    Parameters:
        id: int

    """
    return _port_log.gp_log_remove_func_py(id)

_port_log.GP_LOG_ERROR_swigconstant(_port_log)
GP_LOG_ERROR = _port_log.GP_LOG_ERROR

_port_log.GP_LOG_VERBOSE_swigconstant(_port_log)
GP_LOG_VERBOSE = _port_log.GP_LOG_VERBOSE

_port_log.GP_LOG_DEBUG_swigconstant(_port_log)
GP_LOG_DEBUG = _port_log.GP_LOG_DEBUG

_port_log.GP_LOG_DATA_swigconstant(_port_log)
GP_LOG_DATA = _port_log.GP_LOG_DATA

def gp_log(level, domain, format):
    """
    gp_log(level, domain, format)

    Parameters:
        level: enum GPLogLevel
        domain: char const *
        format: char const *

    """
    return _port_log.gp_log(level, domain, format)

import logging

from gphoto2.result import check_result, GP_OK

class _GPhoto2Logger(object):
    def __init__(self):
        self.log = None
        self.log_id = -1
        self.mapping = {}

    def callback(self, level, domain, msg):
        self.log(self.mapping[level], '(%s) %s', domain, msg)

    def install(self, mapping):
        self.mapping.update(mapping)
        if not self.log:
            self.log = logging.getLogger('gphoto2').log
        if self.log_id >= GP_OK:
            check_result(gp_log_remove_func(self.log_id))
        self.log_id = gp_log_add_func(GP_LOG_DATA, self.callback)
        return self.log_id

_gphoto2_logger = None

def use_python_logging(mapping={}):
    """Install a callback to receive gphoto2 errors and forward them
    to Python's logging system.

    The mapping parameter is a dictionary mapping any of the four
    gphoto2 logging severity levels to a Python logging level.

    """
    global _gphoto2_logger
    if not _gphoto2_logger:
        _gphoto2_logger = _GPhoto2Logger()
    full_mapping = {
        GP_LOG_ERROR   : logging.WARNING,
        GP_LOG_VERBOSE : logging.INFO,
        GP_LOG_DEBUG   : logging.DEBUG,
        GP_LOG_DATA    : logging.DEBUG - 5,
        }
    full_mapping.update(mapping)
    return _gphoto2_logger.install(full_mapping)



