<commit_before># coding=utf-8
from shapely import geometry, geos, wkt
import ConfigParser
import zmq
from threading import Thread, Event
from navitiacommon import type_pb2, request_pb2, models
import glob
from jormungandr.singleton import singleton
from importlib import import_module
import logging
from jormungandr.protobuf_to_dict import protobuf_to_dict
from jormungandr.exceptions import ApiNotFound, RegionNotFound
from jormungandr import app
from jormungandr.instance import Instance
import traceback


@singleton
class InstanceManager(object):

    """ Permet de coordonner les différents NAViTiA gérés par le front-end
    Un identifiant correspond à une socket ZMQ Navitia où se connecter
    Plusieurs identifiants peuvent se connecter à une même socket

    De plus il est possible de définir la zone géographique (au format WKT)
    couverte par un identifiant
    """

    def __init__(self):
        pass

    def initialisation(self, start_ping=True):
        """ Charge la configuration à partir d'un fichier ini indiquant
            les chemins des fichiers contenant :
            - géométries de la région sur laquelle s'applique le moteur
            - la socket pour chaque identifiant navitia
        """

        self.instances = {}
        self.contributors = {}
        self.context = zmq.Context()
        self.default_socket = None

        # if a .ini file is defined in the settings we take it
        # else we load all .ini file found in the INSTANCES_DIR
        if 'INI_FILES' in app.config:
            ini_files = app.config['INI_FILES']
        else:
            ini_files = glob.glob(app.config['INSTANCES_DIR'] + '/*.ini')

        for file_name in ini_files:
            logging.info("Initialisation, reading file : " + file_name)
            conf = ConfigParser.ConfigParser()
            conf.read(file_name)
            instance = Instance(self.context, conf.get('instance', 'key'))
            instance.socket_path = conf.get('instance', 'socket')

            if conf.has_option('instance', 'script'):
                module = import_module(conf.get('instance', 'script'))
                instance.script = module.Script()
            else:
                module = import_module("jormungandr.scripts.default")
                instance.script = module.Script()

            # we give all functional parameters to the script
            if conf.has_section('functional'):
                functional_params = dict(conf.items('functional'))
                instance.script.functional_params = functional_params


            self.instances[conf.get('instance', 'key')] = instance
        self.thread_event = Event()
        self.thread = Thread(target=self.thread_ping)
        #daemon thread does'nt block the exit of a process
        self.thread.daemon = True
        if start_ping:
            self.thread.start()

    def dispatch(self, arguments, api, instance_obj=None, instance_name=None,
                 request=None):
        if instance_obj:
            instance_name = instance_obj.name
        if instance_name in self.instances:
            instance = self.instances[instance_name]
            if api in instance.script.apis:
                api_func = getattr(instance.script, api)
                return api_func(arguments, instance)
            else:
                raise ApiNotFound(api)
        else:
            raise RegionNotFound(instance_name)

    def thread_ping(self, timer=1):
        req = request_pb2.Request()
        req.requested_api = type_pb2.METADATAS
        while not self.thread_event.is_set():
            for key, instance in self.instances.iteritems():
                try:
                    resp = instance.send_and_receive(req, timeout=1000)
                    if resp.HasField("metadatas"):
                        metadatas = resp.metadatas
                        for contributor in metadatas.contributors:
                            self.contributors[str(contributor)] = key
                        instance.geom = wkt.loads(metadatas.shape)
                except geos.ReadingError:
                    instance.geom = None
            self.thread_event.wait(timer)

    def stop(self):
        if not self.thread_event.is_set():
            self.thread_event.set()

    def key_of_id(self, object_id):
        """ Retrieves the key of the region of a given id<commit_msg>Jormungandr: better catch of reading error<commit_after># coding=utf-8
from shapely import geometry, wkt
from shapely.geos import ReadingError
import ConfigParser
import zmq
from threading import Thread, Event
from navitiacommon import type_pb2, request_pb2, models
import glob
from jormungandr.singleton import singleton
from importlib import import_module
import logging
from jormungandr.protobuf_to_dict import protobuf_to_dict
from jormungandr.exceptions import ApiNotFound, RegionNotFound
from jormungandr import app
from jormungandr.instance import Instance
import traceback


@singleton
class InstanceManager(object):

    """ Permet de coordonner les différents NAViTiA gérés par le front-end
    Un identifiant correspond à une socket ZMQ Navitia où se connecter
    Plusieurs identifiants peuvent se connecter à une même socket

    De plus il est possible de définir la zone géographique (au format WKT)
    couverte par un identifiant
    """

    def __init__(self):
        pass

    def initialisation(self, start_ping=True):
        """ Charge la configuration à partir d'un fichier ini indiquant
            les chemins des fichiers contenant :
            - géométries de la région sur laquelle s'applique le moteur
            - la socket pour chaque identifiant navitia
        """

        self.instances = {}
        self.contributors = {}
        self.context = zmq.Context()
        self.default_socket = None

        # if a .ini file is defined in the settings we take it
        # else we load all .ini file found in the INSTANCES_DIR
        if 'INI_FILES' in app.config:
            ini_files = app.config['INI_FILES']
        else:
            ini_files = glob.glob(app.config['INSTANCES_DIR'] + '/*.ini')

        for file_name in ini_files:
            logging.info("Initialisation, reading file : " + file_name)
            conf = ConfigParser.ConfigParser()
            conf.read(file_name)
            instance = Instance(self.context, conf.get('instance', 'key'))
            instance.socket_path = conf.get('instance', 'socket')

            if conf.has_option('instance', 'script'):
                module = import_module(conf.get('instance', 'script'))
                instance.script = module.Script()
            else:
                module = import_module("jormungandr.scripts.default")
                instance.script = module.Script()

            # we give all functional parameters to the script
            if conf.has_section('functional'):
                functional_params = dict(conf.items('functional'))
                instance.script.functional_params = functional_params


            self.instances[conf.get('instance', 'key')] = instance
        self.thread_event = Event()
        self.thread = Thread(target=self.thread_ping)
        #daemon thread does'nt block the exit of a process
        self.thread.daemon = True
        if start_ping:
            self.thread.start()

    def dispatch(self, arguments, api, instance_obj=None, instance_name=None,
                 request=None):
        if instance_obj:
            instance_name = instance_obj.name
        if instance_name in self.instances:
            instance = self.instances[instance_name]
            if api in instance.script.apis:
                api_func = getattr(instance.script, api)
                return api_func(arguments, instance)
            else:
                raise ApiNotFound(api)
        else:
            raise RegionNotFound(instance_name)

    def thread_ping(self, timer=1):
        req = request_pb2.Request()
        req.requested_api = type_pb2.METADATAS
        while not self.thread_event.is_set():
            for key, instance in self.instances.iteritems():
                resp = instance.send_and_receive(req, timeout=1000)
                if resp.HasField("metadatas"):
                    metadatas = resp.metadatas
                    for contributor in metadatas.contributors:
                        self.contributors[str(contributor)] = key
                    try:
                        instance.geom = wkt.loads(metadatas.shape)
                    except ReadingError:
                        instance.geom = None
            self.thread_event.wait(timer)

    def stop(self):
        if not self.thread_event.is_set():
            self.thread_event.set()

    def key_of_id(self, object_id):
        """ Retrieves the key of the region of a given id
{'fraction_non_alphanumeric': 0.07646791078743742, 'fraction_numerical': 0.002275830678197542, 'mean_word_length': 2.2300624770305033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7385550', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2348, 'n_words': 669}