<commit_before>#
# geoloc.py
#
# Copyright (C) 2010 <NAME> <<EMAIL>>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import httplib
import xmpp

from archipelcore.archipelPlugin import TNArchipelPlugin
from archipelcore.utils import build_error_iq


ARCHIPEL_NS_HYPERVISOR_GEOLOC           = "archipel:hypervisor:geolocalization"
ARCHIPEL_ERROR_CODE_LOCALIZATION_GET    = -9001


class TNHypervisorGeolocalization (TNArchipelPlugin):
    """
    this plugin allow to geolocalize the hypervisor
    """

    def __init__(self, configuration, entity, entry_point_group):
        """
        initialize the module
        @type configuration: Configuration object
        @param configuration: the configuration
        @type entity: L{TNArchipelEntity}
        @param entity: the entity that owns the plugin
        @type entry_point_group: string
        @param entry_point_group: the group name of plugin entry_point
        """
        TNArchipelPlugin.__init__(self, configuration=configuration, entity=entity, entry_point_group=entry_point_group)
        mode    = self.configuration.get("GEOLOCALIZATION", "localization_mode")
        lat     = ""
        lon     = ""
        if mode == "auto":
            service         = self.configuration.get("GEOLOCALIZATION", "localization_service_url")
            request         = self.configuration.get("GEOLOCALIZATION", "localization_service_request")
            method          = self.configuration.get("GEOLOCALIZATION", "localization_service_method")
            conn = httplib.HTTPConnection(service)
            conn.request(method, request)
            data_node = xmpp.simplexml.NodeBuilder(data=str(conn.getresponse().read())).getDom()
            lat = data_node.getTagData("Latitude")
            lon = data_node.getTagData("Longitude")
        else:
            lat = self.configuration.getfloat("GEOLOCALIZATION", "localization_latitude")
            lon = self.configuration.getfloat("GEOLOCALIZATION", "localization_longitude")
        string = "<gelocalization><Latitude>"+str(lat)+"</Latitude>\n<Longitude>"+str(lon)+"</Longitude></gelocalization>"
        self.localization_information = xmpp.simplexml.NodeBuilder(data=string).getDom()
        registrar_item = {  "commands" : ["where are you", "localize"],
                            "parameters": {},
                            "method": self.message_get,
                            "permissions": ["geolocalization_get"],
                            "description": "give my the latitude and longitude." }
        self.entity.add_message_registrar_item(registrar_item)
        # permissions
        self.entity.permission_center.create_permission("geolocalization_get", "Authorizes user to get the entity location coordinates", False)


    ### Plugin interface

    def register_for_stanza(self):
        """
        this method will be called by the plugin user when it will be
        necessary to register module for listening to stanza
        """
        self.entity.xmppclient.RegisterHandler('iq', self.process_iq, ns=ARCHIPEL_NS_HYPERVISOR_GEOLOC)

    @staticmethod
    def plugin_info():
        """
        return inforations about the plugin
        @rtype: dict
        @return: dictionary contaning plugin informations
        """
        plugin_friendly_name           = "Hypervisor Geolocalization"
        plugin_identifier              = "geolocalization"
        plugin_configuration_section   = "GEOLOCALIZATION"
        plugin_configuration_tokens    = [ "localization_mode",
                                            "localization_latitude",
                                            "localization_longitude",
                                            "localization_service_url",
                                            "localization_service_request",
                                            "localization_service_method",
                                            "localization_service_response_root_node"]<commit_msg>auto deactivate the geolocalization plugin if something goes wrong<commit_after>#
# geoloc.py
#
# Copyright (C) 2010 <NAME> <<EMAIL>>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import httplib
import xmpp

from archipelcore.archipelPlugin import TNArchipelPlugin
from archipelcore.utils import build_error_iq


ARCHIPEL_NS_HYPERVISOR_GEOLOC           = "archipel:hypervisor:geolocalization"
ARCHIPEL_ERROR_CODE_LOCALIZATION_GET    = -9001


class TNHypervisorGeolocalization (TNArchipelPlugin):
    """
    this plugin allow to geolocalize the hypervisor
    """

    def __init__(self, configuration, entity, entry_point_group):
        """
        initialize the module
        @type configuration: Configuration object
        @param configuration: the configuration
        @type entity: L{TNArchipelEntity}
        @param entity: the entity that owns the plugin
        @type entry_point_group: string
        @param entry_point_group: the group name of plugin entry_point
        """
        TNArchipelPlugin.__init__(self, configuration=configuration, entity=entity, entry_point_group=entry_point_group)
        self.plugin_deactivated = False
        try:
            mode    = self.configuration.get("GEOLOCALIZATION", "localization_mode")
            lat     = ""
            lon     = ""
            if mode == "auto":
                service         = self.configuration.get("GEOLOCALIZATION", "localization_service_url")
                request         = self.configuration.get("GEOLOCALIZATION", "localization_service_request")
                method          = self.configuration.get("GEOLOCALIZATION", "localization_service_method")
                conn = httplib.HTTPConnection(service)
                conn.request(method, request)
                data_node = xmpp.simplexml.NodeBuilder(data=str(conn.getresponse().read())).getDom()
                lat = data_node.getTagData("Latitude")
                lon = data_node.getTagData("Longitude")
            else:
                lat = self.configuration.getfloat("GEOLOCALIZATION", "localization_latitude")
                lon = self.configuration.getfloat("GEOLOCALIZATION", "localization_longitude")
            string = "<gelocalization><Latitude>"+str(lat)+"</Latitude>\n<Longitude>"+str(lon)+"</Longitude></gelocalization>"
            self.localization_information = xmpp.simplexml.NodeBuilder(data=string).getDom()
            registrar_item = {  "commands" : ["where are you", "localize"],
                                "parameters": {},
                                "method": self.message_get,
                                "permissions": ["geolocalization_get"],
                                "description": "give my the latitude and longitude." }
            self.entity.add_message_registrar_item(registrar_item)
            # permissions
            self.entity.permission_center.create_permission("geolocalization_get", "Authorizes user to get the entity location coordinates", False)
        except Exception as ex:
            self.plugin_deactivated = True;
            self.entity.log.error("cannot initialize geolocalization. plugin deactivated. Exception: %s" % str(ex))

    ### Plugin interface

    def register_for_stanza(self):
        """
        this method will be called by the plugin user when it will be
        necessary to register module for listening to stanza
        """
        if self.plugin_deactivated:
            return
        self.entity.xmppclient.RegisterHandler('iq', self.process_iq, ns=ARCHIPEL_NS_HYPERVISOR_GEOLOC)

    @staticmethod
    def plugin_info():
        """
        return inforations about the plugin
        @rtype: dict
        @return: dictionary contaning plugin informations
        """
        plugin_friendly_name           = "Hypervisor Geolocalization"
        plugin_identifier              = "geolocalization"
        plugin_configuration_section   = "GEOLOCALIZATION"
        plugin_configuration_tokens    = [ "localization_mode",
                                            "localization_latitude",
                                            "localization_longitude",
                                            "localization_service_url",
                                            "localization_service_request",
                                            "localization_service_method",
                                            "localization_service_response_root_node"]
{'fraction_non_alphanumeric': 0.08084267330842673, 'fraction_numerical': 0.0018679950186799503, 'mean_word_length': 2.404097492052278, 'pattern_counts': {'":': 10, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4159809', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2402, 'n_words': 633}