<commit_before>#!/usr/bin/python
#-*- coding: utf-8 -*-

# ======================================================================
# Copyright 2016 <NAME>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ======================================================================

from collections import OrderedDict
from socket import gethostname

from supervisor.datatypes import boolean, integer, existing_dirpath, byte_size, logging_level, list_of_strings
from supervisor.options import ServerOptions

from supvisors.ttypes import ConciliationStrategies, DeploymentStrategies


# Options of main section
class SupvisorsOptions():
    """ Holder of the Supvisors options.

    Attributes are:
        - address_list: list of host names or IP addresses where supvisors will be running,
        - deployment_file: absolute or relative path to the XML deployment file,
        - internal_port: port number used to publish local events to remote Supvisors instances,
        - event_port: port number used to publish all Supvisors events,
        - auto_fence: when True, Supvisors won't try to reconnect to a Supvisors instance that has been inactive,
        - synchro_timeout: time in seconds that Supvisors waits for all expected Supvisors instances to publish,
        - conciliation_strategy: strategy used to solve conflicts when Supvisors has detected that multiple instances of the same program are running,
        - deployment_strategy: strategy used to start applications on addresses,
        - stats_periods: list of periods for which the statistics will be provided in the Supvisors web page,
        - stats_histo: depth of statistics history,
        - logfile: absolute or relative path of the Supvisors log file,
        - logfile_maxbytes: maximum size of the Supvisors log file,
        - logfile_backups: number of Supvisors backup log files,
        - loglevel: logging level,

        - procnumbers: a dictionary giving the number of the program in a homogeneous group.
    """

    def __init__(self):
        """ Initialization of the attributes. """
        self.procnumbers = {}

    def __str__(self):
        """ Contents as string. """
        return ('address_list={} deployment_file={} internal_port={} event_port={} auto_fence={} synchro_timeout={} '
            'conciliation_strategy={} deployment_strategy={} stats_periods={} stats_histo={} '
            'logfile={} logfile_maxbytes={} logfile_backups={} loglevel={}'.format(self.address_list,
            self.deployment_file, self.internal_port, self.event_port, self.auto_fence, self.synchro_timeout, 
            self.conciliation_strategy, self.deployment_strategy, self.stats_periods, self.stats_histo,
            self.logfile, self.logfile_maxbytes, self.logfile_backups, self.loglevel))


class SupvisorsServerOptions(ServerOptions):
    """ Class used to parse the options of the 'supvisors' section in the supervisor configuration file.

    Attributes are:
        - supvisors_options: the instance holding all Supvisors options,
        - _Section: constant for the name of the Supvisors section in the Supervisor configuration file.
    """

    _Section = 'supvisors'

    def __init__(self):
        """ Initialization of the attributes. 
        Default parameters fit, so realize is called directly. """
        ServerOptions.__init__(self)
        self.supvisors_options = SupvisorsOptions()
        self.realize()

    def _processes_from_section(self, parser, section, group_name, klass=None):
        """ This method is overriden to store the program number of a homogeneous program.
        This is used in Supervisor to set the real program name from the format defined in the ini file.
        However, Supervisor does not keep this information in its internal structure. """
        # call super behaviour
        programs = ServerOptions._processes_from_section(self, parser, section, group_name, klass)
        # store the number of each program
        for idx, program in enumerate(programs):
            self.supvisors_options.procnumbers[program.name] = idx
        # return original result
        return programs

    def server_configs_from_parser(self, parser):
        """ The following has nothing to deal with Supervisor's server configurations.
        It gets Supvisors configuration.
        Supervisor's ServerOptions has not been designed to be specialized.
        This method is overriden just to have an access point to the Supervisor parser. """
        configs = ServerOptions.server_configs_from_parser(self, parser)
        # set section
        if not parser.has_section(self._Section):
            raise ValueError('section [{}] not found in ini file {}'.format(self._Section))
        temp, parser.mysection = parser.mysection, self._Section
        # get values
        opt = self.supvisors_options
        opt.address_list = list(OrderedDict.fromkeys(filter(None, list_of_strings(parser.getdefault('address_list', gethostname())))))
        opt.deployment_file = existing_dirpath(parser.getdefault('deployment_file', ''))
        opt.internal_port = self.to_port_num(parser.getdefault('internal_port', '65001'))
        opt.event_port = self.to_port_num(parser.getdefault('event_port', '65002'))
        opt.auto_fence = boolean(parser.getdefault('auto_fence', 'false'))
        opt.synchro_timeout = self.to_timeout(parser.getdefault('synchro_timeout', '15'))
        opt.conciliation_strategy = self.to_conciliation_strategy(parser.getdefault('conciliation_strategy', 'USER'))
        opt.deployment_strategy = self.to_deployment_strategy(parser.getdefault('deployment_strategy', 'CONFIG'))
        # configure statistics
        opt.stats_periods = self.to_periods(list_of_strings(parser.getdefault('stats_periods', '10')))
        opt.stats_histo = self.to_histo(parser.getdefault('stats_histo', 200))
        # configure logger
        opt.logfile = existing_dirpath(parser.getdefault('logfile', '{}.log'.format(self._Section)))
        opt.logfile_maxbytes = byte_size(parser.getdefault('logfile_maxbytes', '50MB'))
        opt.logfile_backups = integer(parser.getdefault('logfile_backups', 10))
        opt.loglevel = logging_level(parser.getdefault('loglevel', 'info'))
        # reset mysection and return original result
        parser.mysection = temp
        print opt
        return configs

    # conversion utils (completion of supervisor.datatypes)
    @staticmethod
    def to_port_num(value):
        """ Convert a string into a port number. """
        value = integer(value)<commit_msg>fix logger problem. supvisors logs were written in supervisord logs<commit_after>#!/usr/bin/python
#-*- coding: utf-8 -*-

# ======================================================================
# Copyright 2016 <NAME>
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ======================================================================

from collections import OrderedDict
from socket import gethostname

from supervisor.datatypes import boolean, integer, existing_dirpath, byte_size, logging_level, list_of_strings
from supervisor.options import ServerOptions

from supvisors.ttypes import ConciliationStrategies, DeploymentStrategies


# Options of main section
class SupvisorsOptions():
    """ Holder of the Supvisors options.

    Attributes are:
        - address_list: list of host names or IP addresses where supvisors will be running,
        - deployment_file: absolute or relative path to the XML deployment file,
        - internal_port: port number used to publish local events to remote Supvisors instances,
        - event_port: port number used to publish all Supvisors events,
        - auto_fence: when True, Supvisors won't try to reconnect to a Supvisors instance that has been inactive,
        - synchro_timeout: time in seconds that Supvisors waits for all expected Supvisors instances to publish,
        - conciliation_strategy: strategy used to solve conflicts when Supvisors has detected that multiple instances of the same program are running,
        - deployment_strategy: strategy used to start applications on addresses,
        - stats_periods: list of periods for which the statistics will be provided in the Supvisors web page,
        - stats_histo: depth of statistics history,
        - logfile: absolute or relative path of the Supvisors log file,
        - logfile_maxbytes: maximum size of the Supvisors log file,
        - logfile_backups: number of Supvisors backup log files,
        - loglevel: logging level,

        - procnumbers: a dictionary giving the number of the program in a homogeneous group.
    """

    def __init__(self):
        """ Initialization of the attributes. """
        self.procnumbers = {}

    def __str__(self):
        """ Contents as string. """
        return ('address_list={} deployment_file={} internal_port={} event_port={} auto_fence={} synchro_timeout={} '
            'conciliation_strategy={} deployment_strategy={} stats_periods={} stats_histo={} '
            'logfile={} logfile_maxbytes={} logfile_backups={} loglevel={}'.format(self.address_list,
            self.deployment_file, self.internal_port, self.event_port, self.auto_fence, self.synchro_timeout, 
            self.conciliation_strategy, self.deployment_strategy, self.stats_periods, self.stats_histo,
            self.logfile, self.logfile_maxbytes, self.logfile_backups, self.loglevel))


class SupvisorsServerOptions(ServerOptions):
    """ Class used to parse the options of the 'supvisors' section in the supervisor configuration file.

    Attributes are:
        - supvisors_options: the instance holding all Supvisors options,
        - _Section: constant for the name of the Supvisors section in the Supervisor configuration file.
    """

    _Section = 'supvisors'

    def __init__(self):
        """ Initialization of the attributes. 
        Default parameters fit, so realize is called directly. """
        ServerOptions.__init__(self)
        self.supvisors_options = SupvisorsOptions()
        self.realize()

    def _processes_from_section(self, parser, section, group_name, klass=None):
        """ This method is overriden to store the program number of a homogeneous program.
        This is used in Supervisor to set the real program name from the format defined in the ini file.
        However, Supervisor does not keep this information in its internal structure. """
        # call super behaviour
        programs = ServerOptions._processes_from_section(self, parser, section, group_name, klass)
        # store the number of each program
        for idx, program in enumerate(programs):
            self.supvisors_options.procnumbers[program.name] = idx
        # return original result
        return programs

    def server_configs_from_parser(self, parser):
        """ The following has nothing to deal with Supervisor's server configurations.
        It gets Supvisors configuration.
        Supervisor's ServerOptions has not been designed to be specialized.
        This method is overriden just to have an access point to the Supervisor parser. """
        configs = ServerOptions.server_configs_from_parser(self, parser)
        # set section
        if not parser.has_section(self._Section):
            raise ValueError('section [{}] not found in ini file {}'.format(self._Section))
        temp, parser.mysection = parser.mysection, self._Section
        # get values
        opt = self.supvisors_options
        opt.address_list = list(OrderedDict.fromkeys(filter(None, list_of_strings(parser.getdefault('address_list', gethostname())))))
        opt.deployment_file = existing_dirpath(parser.getdefault('deployment_file', ''))
        opt.internal_port = self.to_port_num(parser.getdefault('internal_port', '65001'))
        opt.event_port = self.to_port_num(parser.getdefault('event_port', '65002'))
        opt.auto_fence = boolean(parser.getdefault('auto_fence', 'false'))
        opt.synchro_timeout = self.to_timeout(parser.getdefault('synchro_timeout', '15'))
        opt.conciliation_strategy = self.to_conciliation_strategy(parser.getdefault('conciliation_strategy', 'USER'))
        opt.deployment_strategy = self.to_deployment_strategy(parser.getdefault('deployment_strategy', 'CONFIG'))
        # configure statistics
        opt.stats_periods = self.to_periods(list_of_strings(parser.getdefault('stats_periods', '10')))
        opt.stats_histo = self.to_histo(parser.getdefault('stats_histo', 200))
        # configure logger
        opt.logfile = existing_dirpath(parser.getdefault('logfile', '{}.log'.format(self._Section)))
        opt.logfile_maxbytes = byte_size(parser.getdefault('logfile_maxbytes', '50MB'))
        opt.logfile_backups = integer(parser.getdefault('logfile_backups', 10))
        opt.loglevel = logging_level(parser.getdefault('loglevel', 'info'))
        # reset mysection and return original result
        parser.mysection = temp
        return configs

    # conversion utils (completion of supervisor.datatypes)
    @staticmethod
    def to_port_num(value):
        """ Convert a string into a port number. """
        value = integer(value)
{'fraction_non_alphanumeric': 0.10116097975364576, 'fraction_numerical': 0.004247486903582048, 'mean_word_length': 3.6242225859247137, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '312974', 'n_tokens_mistral': 3787, 'n_tokens_neox': 3608, 'n_words': 1208}