コード例 #1
0
ファイル: log_manager.py プロジェクト: willfurnass/config-api
    def __configure_handler(self, config_section, defaults, handler, level,
                            format_, datefmt):
        """ Configure specified handler with a given defaults. """
        cm = ConfigManager.get_instance()
        cm.set_config_defaults(defaults)
        handler_option = cm.get_config_option(config_section, 'handler',
                                              handler)

        # If handler_option is empty, handler cannot be instantiated.
        _handler = None
        if handler_option is not None:
            # Handler argument format: my_module.MyHandler(arg1, arg2, ...)
            # Module will be in lowercase letters, but the class
            # should be capitalized.
            handler_name = re.sub(r'\(.*', '', handler_option)
            module_name = handler_name.split('.')[0]
            try:
                exec 'from uge.log import %s' % (module_name)
                exec '_handler = %s' % (handler_option)
            except IOError, ex:
                _errno = ex.errno
                import errno

                # If the exception raised is an I/O permissions error,
                # ignore it and disable this log handler.  This allows
                # non-root users to use the (system-wide) default log
                # configuration
                if _errno != errno.EACCES:
                    raise
                _handler = None
            except Exception, ex:
                raise ConfigurationError(exception=ex)
コード例 #2
0
ファイル: qconf_object.py プロジェクト: kimiwangjm/config-api
 def set_add_metadata(self):
     """ 
     Sets default object metadata (user/timestamp) for API add operations.
     """
     cm = ConfigManager.get_instance()
     created_by = '%s@%s' % (cm['user'], cm['host'])
     self.metadata['created_by'] = created_by
     self.metadata['created_on'] = datetime.datetime.now().isoformat()
コード例 #3
0
ファイル: log_manager.py プロジェクト: willfurnass/config-api
 def __create_logger_factory(self):
     cm = ConfigManager.get_instance()
     root_log_level = cm.get_config_option('LoggerLevels', 'root', 'error')
     root_level_int = logging.getLevelName(root_log_level.upper())
     root_logger = logging.getLogger('')
     root_logger.root.setLevel(root_level_int)
     root_logger.debug('Set root logger to %s' % root_level_int)
     expressions = cm.get_config_option('LoggerLevels', 'expressions')
     return LoggerFactory(expressions)
コード例 #4
0
ファイル: qconf_cli.py プロジェクト: kimiwangjm/config-api
    def parse_args(self, usage=None):
        """ 
        Parse command arguments. 

        :param usage: Command usage.
        :type usage: str
        """
        if usage:
            self.parser.usage = usage

        try:
            (self.options, self.args) = self.parser.parse_args()
        except SystemExit as rc:
            sys.stdout.flush()
            sys.stderr.flush()
            os._exit(int(str(rc)))

        if self.valid_arg_count < len(self.args):
            # Postitional args are not enabled and we have some
            msg = "Invalid Argument(s):"
            for arg in self.args[self.valid_arg_count:]:
                msg += " " + arg
            raise InvalidArgument(msg)

        opt_dict = self.options.__dict__
        if opt_dict.get('cmd_version'):
            print('%s version: %s' % (os.path.basename(
                sys.argv[0]), ConfigManager.get_instance().get_version()))
            os._exit(0)

        # Log level.
        console_log_level = opt_dict.get('console_log_level', None)
        if console_log_level:
            LogManager.get_instance().set_console_log_level(console_log_level)

        # Check input arguments.
        self.check_input_args()
        return (self.options, self.args)
コード例 #5
0
from .utils import needs_uge
from .utils import generate_random_string
from .utils import create_config_file
from .utils import load_values

from uge.api.qconf_api import QconfApi
from uge.config.config_manager import ConfigManager
from uge.log.log_manager import LogManager
from uge.exceptions.object_not_found import ObjectNotFound
from uge.exceptions.object_already_exists import ObjectAlreadyExists

create_config_file()
API = QconfApi()
HOST_GROUP_NAME = '@%s' % generate_random_string(6)
CONFIG_MANAGER = ConfigManager.get_instance()
HOST_NAME = CONFIG_MANAGER['host']
LOG_MANAGER = LogManager.get_instance()
VALUES_DICT = load_values('test_values.json')
print(VALUES_DICT)


@needs_uge
def test_generate_hgrp():
    hgrp = API.generate_hgrp(HOST_GROUP_NAME)
    assert (hgrp.data['group_name'] == HOST_GROUP_NAME)


def test_list_hgrps():
    hgrpl = API.list_hgrps()
    assert (hgrpl is not None)
コード例 #6
0
ファイル: log_manager.py プロジェクト: gridengine/config-api
    def __configure_handler(self, config_section, defaults, handler, level,
                            format_, datefmt):
        """ Configure specified handler with a given defaults. """
        cm = ConfigManager.get_instance()
        cm.set_config_defaults(defaults)
        handler_option = cm.get_config_option(config_section, 'handler',
                                              handler)

        # If handler_option is empty, handler cannot be instantiated.
        _handler = None
        if handler_option is not None:
            # Handler argument format: my_module.MyHandler(arg1, arg2, ...)
            # Module will be in lowercase letters, but the class
            # should be capitalized.
            handler_name = re.sub(r'\(.*', '', handler_option)
            module_name = handler_name.split('.')[0]
            try:
                exec('from uge.log import %s' % (module_name))
                exec('_handler = %s' % (handler_option))
            except IOError as ex:
                _errno = ex.errno
                import errno

                # If the exception raised is an I/O permissions error,
                # ignore it and disable this log handler.  This allows
                # non-root users to use the (system-wide) default log
                # configuration
                if _errno != errno.EACCES:
                    raise
                _handler = None
            except Exception as ex:
                raise ConfigurationError(exception=ex)

        # Only request setting from the config file if it was
        # not set via environment variable, or programmatically.
        if _handler is not None:
            try:
                _level = level
                if _level is None:
                    _level = cm.get_config_option(config_section, 'level',
                                                  defaults['level'])
                intLevel = self.__get_log_level(_level.upper())
                _handler.setLevel(intLevel)
                _handler.level = _level.upper()

                _format = format_
                if _format is None:
                    _format = cm.get_config_option(config_section, 'format',
                                                   defaults['format'])

                _datefmt = datefmt
                if _datefmt is None:
                    _datefmt = cm.get_config_option(config_section, 'datefmt',
                                                    defaults['datefmt'])

                _handler.setFormatter(logging.Formatter(_format, _datefmt))
            except Exception as ex:
                raise ConfigurationError(exception=ex)

            # Look to see if there is a filter to apply to the handler
            filter_ = None

            try:
                filter_ = cm.get_config_option(config_section, 'filter')
            except Exception as ex:
                pass

            if filter_:
                _handler.addFilter(logging.Filter(filter_))

        return _handler
コード例 #7
0
ファイル: log_manager.py プロジェクト: gridengine/config-api
    def __configure_handlers(self):
        """ Configure all log handlers from the config file. """
        cm = ConfigManager.get_instance()

        # Config manager will return the requested setting, or
        # its own default values if the default value is not passed in.
        handler = 'stream_log_handler.StreamLogHandler(sys.stderr,)'
        level = cm.get_console_log_level(None)
        format_ = cm.get_log_record_format(None)
        datefmt = cm.get_log_date_format(None)

        defaults = {
            'level': cm.get_console_log_level(),
            'format': cm.get_log_record_format(),
            'datefmt': cm.get_log_date_format(),
            'handler': handler
        }

        console_handler = None
        if cm.get_console_log_level() != 'notset':
            console_handler = self.__configure_handler('ConsoleLogging',
                                                       defaults, handler,
                                                       level, format_, datefmt)

        if console_handler is not None:
            self.user_handler_list.append(console_handler)
            cm.set_console_log_level(console_handler.level)

        # Local file logging (only turned on if it is in the config file).
        handler = None
        level = cm.get_file_log_level(None)

        defaults['level'] = cm.get_file_log_level()
        defaults['handler'] = handler

        # Parse all of the file loggers present in the config file
        config_sections = cm.get_config_sections()
        for config_section in config_sections:
            if config_section.startswith('FileLogging'):
                file_handler = self.__configure_handler(
                    config_section, defaults, handler, level, format_, datefmt)

                if file_handler is not None:
                    self.system_handler_list.append(file_handler)
                    cm.set_file_log_level(file_handler.level)

        # Remote logging (only turned on if it is in the config file).
        remote_handler = self.__configure_handler('RemoteLogging', defaults,
                                                  handler, level, format_,
                                                  datefmt)

        if remote_handler is not None:
            self.system_handler_list.append(remote_handler)

        # Add handlers to the root logger.  Use logging class here
        # to make sure we can have a logger when we parse the
        # logger expressions
        root_logger = logging.getLogger('')

        for handler in self.user_handler_list + self.system_handler_list:
            root_logger.addHandler(handler)

        # Now get a logger factory based on our current config
        self.logger_factory = self.__create_logger_factory()

        # Reset levels.
        self.set_console_log_level(cm.get_console_log_level())
        self.set_file_log_level(cm.get_file_log_level())
コード例 #8
0
ファイル: utils.py プロジェクト: kimiwangjm/config-api
def create_config_manager():
    from uge.config.config_manager import ConfigManager
    cm = ConfigManager.get_instance()
    cm.set_config_file(CONFIG_FILE)
    cm.set_log_file(LOG_FILE)
    cm.set_file_log_level('trace')
コード例 #9
0
ファイル: qconf_cli.py プロジェクト: willfurnass/config-api
class QconfCli(object):
    """ Base qconf command line interface class. """
    __metaclass__ = abc.ABCMeta

    def __init__(self, valid_arg_count=0):
        """ 
        Class constructor.

        :param valid_arg_count: Number of allowed positional arguments (default: 0).
        :type valid_arg_count: int
        """
        self.logger = LogManager.get_instance().get_logger(
            self.__class__.__name__)
        self.parser = OptionParser(add_help_option=False)
        self.options = {}
        self.args = []
        self.valid_arg_count = valid_arg_count
        self.option_group_dict = {}

        common_group = 'Common Options'
        self.add_option_group(common_group, None)

        self.add_option_to_group(common_group,
                                 '-h',
                                 '--help',
                                 action='help',
                                 help='Show this help message and exit.')
        self.add_option_to_group(common_group,
                                 '-?',
                                 '',
                                 action='help',
                                 help='Show this help message and exit.')

        self.add_option_to_group(common_group,
                                 '-v',
                                 '',
                                 action='store_true',
                                 dest='cmd_version',
                                 default=False,
                                 help='Print version and exit.')

        self.add_option_to_group(
            common_group,
            '-d',
            '--debug',
            dest='console_log_level',
            help=
            'Set debug level; valid values are: critical, error, warning, info, debug'
        )

    def add_option(self, *args, **kwargs):
        """ 
        Add CLI option. 
        """
        self.parser.add_option(*args, **kwargs)

    def add_option_to_group(self, group_name, *args, **kwargs):
        """
        Add option to the given group.
        Group should be created using add_option_group().

        :param group_name: Group name.
        :type group_name: str
        """
        group = self.option_group_dict.get(group_name)
        group.add_option(*args, **kwargs)

    def add_option_group(self, group_name, desc):
        """ 
        Add option group. 

        :param group_name: Group name.
        :type group_name: str
        """
        group = OptionGroup(self.parser, group_name, desc)
        self.parser.add_option_group(group)
        self.option_group_dict[group_name] = group

    def parse_args(self, usage=None):
        """ 
        Parse command arguments. 

        :param usage: Command usage.
        :type usage: str
        """
        if usage:
            self.parser.usage = usage

        try:
            (self.options, self.args) = self.parser.parse_args()
        except SystemExit, rc:
            sys.stdout.flush()
            sys.stderr.flush()
            os._exit(int(str(rc)))

        if self.valid_arg_count < len(self.args):
            # Postitional args are not enabled and we have some
            msg = "Invalid Argument(s):"
            for arg in self.args[self.valid_arg_count:]:
                msg += " " + arg
            raise InvalidArgument(msg)

        opt_dict = self.options.__dict__
        if opt_dict.get('cmd_version'):
            print '%s version: %s' % (os.path.basename(
                sys.argv[0]), ConfigManager.get_instance().get_version())
            os._exit(0)

        # Log level.
        console_log_level = opt_dict.get('console_log_level', None)
        if console_log_level:
            LogManager.get_instance().set_console_log_level(console_log_level)

        # Check input arguments.
        self.check_input_args()
        return (self.options, self.args)