<commit_before># -*- coding: utf-8 -*-
#Copyright (C) 2009 <NAME>, <EMAIL>
#
#This file is part of Shinken.
#
#Shinken 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.
#
#Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.


""" This python module contains the class BaseModule that shinken modules will subclass """


import os
import signal
import time
from multiprocessing import Queue, Process

from shinken.log import logger
 

## TODO: use a class for defining the module "properties" instead of plain dict ??  Like:
"""
class ModuleProperties(object):
    def __init__(self, type, phases, external=False)
        self.type = type
        self.phases = phases
        self.external = external
"""
## and  have the new modules instanciate this like follow :
"""
properties = ModuleProperties('the_module_type', the_module_phases, is_mod_ext)
"""
## 
""" The `properties´ dict defines what the module can do and if it's an external module or not. """
properties = {
    # name of the module type ; to distinguish between them:
    'type': None,
    
    # is the module "external" (external means here a daemon module) ?
    'external': True,
    
    # Possible configuration phases where the module is involved :
    'phases' : [ 'configuration', 'late_configuration', 'running', 'retention' ], 
    }


class ModulePhases:
## TODO: why not use simply integers instead of string to represent the different phases ??    
    CONFIGURATION       = 1
    LATE_CONFIGURATION  = 2
    RUNNING             = 4 
    RETENTION           = 8
    
    

class BaseModule(object):
    """ This is the base class for the shinken modules.
Modules can be used by the different shinken daemons/services for different tasks.
Example of task that a shinken module can do:
 - load additional configuration objects.
 - recurrently save hosts/services status/perfdata informations in different format.
 - ...
 """

    def __init__(self, mod_conf):
        """ Instanciate a new module. There can be many instance of the same type.
`mod_conf´ is module configuration object for this new module instance. """
        self.myconf = mod_conf
        self.name = mod_conf.get_name()
        # We can have sub modules
        self.modules = getattr(mod_conf, 'modules', [])
        self.props = mod_conf.properties.copy()
        self.properties = self.props # TODO: choose between 'props' or 'properties'..
        self.interrupted = False
        self.is_external = self.props.get('external', False)
        self.phases = self.props.get('phases', [])  # though a module defined with no phase is quite useless ..
        self.phases.append(None)
        self.to_q = None # the queue the module will receive data to manage
        self.from_q = None # the queue the module will put its result data
        self.process = None
        self.init_try = 0


    def init(self):
        """ Handle this module "post" init ; just before it'll be started. 
Like just open necessaries file(s), database(s), or whatever the module will need. """
        pass

    # The manager is None on android, but a true Manager() elsewhere
    def create_queues(self, manager):
        """ Create the shared queues that will be used by shinken daemon process and this module process.
But clear queues if they were already set before recreating new one.  """
        self.clear_queues(manager)
        # If no Manager() object, go with classic Queue()
        if not manager:
            self.from_q = Queue()
            self.to_q = Queue()
        else:
            self.from_q = manager.Queue()
            self.to_q = manager.Queue()


    def clear_queues(self, manager):
        """ Release the resources associated with the queues of this instance """
        for q in (self.to_q, self.from_q):
            if q is None: continue
            # If we gotno manager, we direct call the clean
            if not manager:
                q.close()
                q.join_thread()
#            else:
#                q._callmethod('close')
#                q._callmethod('join_thread')<commit_msg>Fix : for test, we allow the create_queues to be call without a Manager object.<commit_after># -*- coding: utf-8 -*-
#Copyright (C) 2009 <NAME>, <EMAIL>
#
#This file is part of Shinken.
#
#Shinken 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.
#
#Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.


""" This python module contains the class BaseModule that shinken modules will subclass """


import os
import signal
import time
from multiprocessing import Queue, Process

from shinken.log import logger
 

## TODO: use a class for defining the module "properties" instead of plain dict ??  Like:
"""
class ModuleProperties(object):
    def __init__(self, type, phases, external=False)
        self.type = type
        self.phases = phases
        self.external = external
"""
## and  have the new modules instanciate this like follow :
"""
properties = ModuleProperties('the_module_type', the_module_phases, is_mod_ext)
"""
## 
""" The `properties´ dict defines what the module can do and if it's an external module or not. """
properties = {
    # name of the module type ; to distinguish between them:
    'type': None,
    
    # is the module "external" (external means here a daemon module) ?
    'external': True,
    
    # Possible configuration phases where the module is involved :
    'phases' : [ 'configuration', 'late_configuration', 'running', 'retention' ], 
    }


class ModulePhases:
## TODO: why not use simply integers instead of string to represent the different phases ??    
    CONFIGURATION       = 1
    LATE_CONFIGURATION  = 2
    RUNNING             = 4 
    RETENTION           = 8
    
    

class BaseModule(object):
    """ This is the base class for the shinken modules.
Modules can be used by the different shinken daemons/services for different tasks.
Example of task that a shinken module can do:
 - load additional configuration objects.
 - recurrently save hosts/services status/perfdata informations in different format.
 - ...
 """

    def __init__(self, mod_conf):
        """ Instanciate a new module. There can be many instance of the same type.
`mod_conf´ is module configuration object for this new module instance. """
        self.myconf = mod_conf
        self.name = mod_conf.get_name()
        # We can have sub modules
        self.modules = getattr(mod_conf, 'modules', [])
        self.props = mod_conf.properties.copy()
        self.properties = self.props # TODO: choose between 'props' or 'properties'..
        self.interrupted = False
        self.is_external = self.props.get('external', False)
        self.phases = self.props.get('phases', [])  # though a module defined with no phase is quite useless ..
        self.phases.append(None)
        self.to_q = None # the queue the module will receive data to manage
        self.from_q = None # the queue the module will put its result data
        self.process = None
        self.init_try = 0


    def init(self):
        """ Handle this module "post" init ; just before it'll be started. 
Like just open necessaries file(s), database(s), or whatever the module will need. """
        pass

    # The manager is None on android, but a true Manager() elsewhere
    def create_queues(self, manager=None):
        """ Create the shared queues that will be used by shinken daemon process and this module process.
But clear queues if they were already set before recreating new one.  """
        self.clear_queues(manager)
        # If no Manager() object, go with classic Queue()
        if not manager:
            self.from_q = Queue()
            self.to_q = Queue()
        else:
            self.from_q = manager.Queue()
            self.to_q = manager.Queue()


    def clear_queues(self, manager):
        """ Release the resources associated with the queues of this instance """
        for q in (self.to_q, self.from_q):
            if q is None: continue
            # If we gotno manager, we direct call the clean
            if not manager:
                q.close()
                q.join_thread()
#            else:
#                q._callmethod('close')
#                q._callmethod('join_thread')
{'fraction_non_alphanumeric': 0.08522172828797571, 'fraction_numerical': 0.0023853409953377425, 'mean_word_length': 2.761827079934747, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7352703', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2456, 'n_words': 1056}