Ejemplo n.º 1
0
def create_object(class_path, **kwargs):
    log = logging.getLogger(__name__)
    try:
        if '.' not in class_path:
            log.warn(
                'impossible to build object {}, wrongly formated class'.format(
                    class_path))
            raise ConfigException(class_path)

        module_path, name = class_path.rsplit('.', 1)
        module = import_module(module_path)
        attr = getattr(module, name)
    except ImportError:
        log.warn('impossible to build object {}, cannot find class'.format(
            class_path))
        raise ConfigException(class_path)

    try:
        obj = attr(**kwargs)  # call to the contructor, with all the args
    except TypeError as e:
        log.warn('impossible to build object {}, wrong arguments: {}'.format(
            class_path, e.message))
        raise ConfigException(class_path)

    return obj
Ejemplo n.º 2
0
def create_object(configuration):
    """
    Create an object from a dict
    The dict must contains a 'class' key with the class path of the class we want to create
    It can contains also an 'args' key with a dictionary of arguments to pass to the constructor
    """
    class_path = configuration['class']
    kwargs = configuration.get('args', {})

    log = logging.getLogger(__name__)
    try:
        if '.' not in class_path:
            log.warn('impossible to build object {}, wrongly formated class'.format(class_path))
            raise ConfigException(class_path)

        module_path, name = class_path.rsplit('.', 1)
        module = import_module(module_path)
        attr = getattr(module, name)
    except AttributeError as e:
        log.warn('impossible to build object {} : {}'.format(class_path, e))
        raise ConfigException(class_path)
    except ImportError:
        log.exception('impossible to build object {}, cannot find class'.format(class_path))
        raise ConfigException(class_path)

    try:
        obj = attr(**kwargs)  # call to the contructor, with all the args
    except TypeError as e:
        log.warn('impossible to build object {}, wrong arguments: {}'.format(class_path, e.message))
        raise ConfigException(class_path)

    return obj
Ejemplo n.º 3
0
    def get_street_network_services(instance, street_network_configurations):
        log = logging.getLogger(__name__)
        street_network_services = {}
        for config in street_network_configurations:
            # Set default arguments
            if 'args' not in config:
                config['args'] = {}
            if 'service_url' not in config['args']:
                config['args'].update({'service_url': None})
            if 'instance' not in config['args']:
                config['args'].update({'instance': instance})

            modes = config.get('modes')
            if not modes:
                raise KeyError('impossible to build a StreetNetwork, missing mandatory field in configuration: modes')

            try:
                service = utils.create_object(config)
            except KeyError as e:
                raise KeyError('impossible to build a StreetNetwork, missing mandatory field in configuration: {}'
                               .format(e.message))
            except ConfigException as e:
                raise ConfigException("impossible to build StreetNetwork, wrongly formated class: {}"
                                      .format(e))

            for mode in modes:
                street_network_services[mode] = service
                log.info('** StreetNetwork {} used for direct_path with mode: {} **'
                         .format(type(service).__name__, mode))
        return street_network_services
Ejemplo n.º 4
0
    def init_external_services(self):
        # Init external services from config file
        for config in self._external_service_configuration:
            # Set default arguments
            if 'args' not in config:
                config['args'] = {}
            if 'service_url' not in config['args']:
                config['args'].update({'service_url': None})
            try:
                service = utils.create_object(config)
            except KeyError as e:
                self.logger.error(
                    'Impossible to create external service with missing key: {}'
                    .format(str(e)))
                raise KeyError(
                    'Impossible to create external service with missing key: {}'
                    .format(str(e)))
            except Exception as e:
                self.logger.error(
                    'Impossible to create external service with wrong class: {}'
                    .format(str(e)))
                raise ConfigException(
                    'Impossible to create external service with wrong class: {}'
                    .format(str(e)))

            self._external_services_legacy.setdefault(
                config['navitia_service'], []).append(service)
Ejemplo n.º 5
0
def create_object(configuration):
    """
    Create an object from a dict
    The dict must contains a 'class' or 'klass' key with the class path of the class we want to create
    It can contains also an 'args' key with a dictionary of arguments to pass to the constructor
    """
    log = logging.getLogger(__name__)
    class_path = configuration.get('class') or configuration.get('klass')
    if class_path is None:
        log.warning('impossible to build object, class_path is empty')
        raise KeyError(
            'impossible to build a StreetNetwork, missing mandatory field in configuration: class or klass'
        )

    kwargs = configuration.get('args', {})

    try:
        if '.' not in class_path:
            log.warning(
                'impossible to build object {}, wrongly formated class'.format(
                    class_path))
            raise ConfigException(class_path)

        module_path, name = class_path.rsplit('.', 1)
        module = import_module(module_path)
        attr = getattr(module, name)
    except AttributeError as e:
        log.warning('impossible to build object {} : {}'.format(class_path, e))
        raise ConfigException(class_path)
    except ImportError:
        log.exception(
            'impossible to build object {}, cannot find class'.format(
                class_path))
        raise ConfigException(class_path)

    try:
        obj = attr(**kwargs)  # call to the contructor, with all the args
    except TypeError as e:
        log.warning(
            'impossible to build object {}, wrong arguments: {}'.format(
                class_path, e))
        raise ConfigException(class_path)

    return obj