Example #1
0
def get_log():
    """Get Log Function

    Function allow getting particular logger

    Returns:
        value: custom of default logger.

    """
    try:
        # Trying to get needed logger
        logger = logging.getLogger(
            app_config_manager.get_property('logger.name')
        )
        logging.root.handlers = []
        handler = logging.StreamHandler()
        logging.root.addHandler(handler)
        logging.root.setLevel(logging.CRITICAL)

        # if try was successful return logger
        if len(logger.handlers):
            logging.root.handlers = []
            return logger
        # Otherwise return default logging.root logger
        else:
            return logging.root
    except Exception as e:
        return logging.root
Example #2
0
def _get_config_path():
    """Get Config Path Function.

    Do not use this function outside of this module.

    Function get correct user config file path according
    to app config.

    Returns:
        String value of file path.
        None if some exception occupies.

    """
    use_custom_config = app_config_manager.get_property(
        'user_config.use_custom')
    search_query = 'user_config.path.{}'.format(
        'custom' if use_custom_config else 'default')

    return app_config_manager.get_property(search_query)
Example #3
0
def _get_config_path():
    """Get Config Path Function.

    Do not use this function outside of this module.

    Returns:
        String value of file path.
        None if some exception occupies.

    """
    return app_config_manager.get_property('bin_config.path')
Example #4
0
def is_supported_platform():
    """Is Supported Platform Function.

    Function check if platform is supported

    Returns:
        Boolean shows is platform supported.

    """
    platform_name = sys.platform.lower()
    supported_platforms = app_config_manager.get_property(
        'supported_platforms')
    return platform_name in supported_platforms
Example #5
0
def setup(options=None):
    """Setup Function
    
    Init function for log_helper, should be called before logging
    anything
    
    Arguments:
        options: list of optional config params.
    Returns:
        value: 1/0 depend on success of operation.
        
    """
    try:
        # If it's 'silent' mod or options was not passed than
        # disable logger, because it's either test mod or lib kind usage
        # of script
        if options is None or 'silent' in options:
            logging.root.disabled = True
            return 0

        logging_config = app_config_manager.get_property('logger')

        formatter = LvlDependFormatter()
        handler = logging.StreamHandler()

        logger = logging.getLogger(logging_config['name'])

        # Try to find loglvl option in params and if exists,
        # try to equal it to logger log level.
        if 'loglvl' in options and options['loglvl'] in logging._levelNames:
            logger.setLevel(logging.getLevelName(options['loglvl']))
        # Otherwise get loglevel from config file.
        else:
            logger.setLevel(logging.getLevelName(logging_config['level']))

        # Set up logger is formatter and empty handlers.
        handler.setFormatter(formatter)
        logger.handlers = []
        logger.addHandler(handler)

        # Make logger disabled if silent mod
        if options is None or 'silent' in options:
            logger.disabled = True
            return 0

        return 0
    except Exception as e:
        logging.exception(e)
        return 1
Example #6
0
    def format(self, record):
        """Format Method.
        
        Returns right format depend on level logging name
        
        """
        format_orig = self._fmt

        # Get custom formatter config
        formats = app_config_manager.get_property('logger.formats')

        # Choose needed formatter depend on logging level.
        # If level does not exists, choose default formatter.
        if logging.getLevelName(record.levelno) in formats:
            self._fmt = formats[logging.getLevelName(record.levelno)]
        else:
            self._fmt = formats['default']

        result = logging.Formatter.format(self, record)
        self._fmt = format_orig
        return result
Example #7
0
def setup(options):
    """Setup Function
    
    Function setting up path to custom user config, checking if
    'userconfpath' was passed as an argument.
    
    Arguments:
        options: list of optional arguments.
    
    """
    try:
        if 'userconfpath' in options:
            app_config_manager.set_property(
                'user_config.path.custom',
                os.path.abspath(options['userconfpath']))
        else:
            app_config_manager.set_property(
                'user_config.path.custom',
                app_config_manager.get_property('user_config.path.default'))
        return 0
    except Exception as e:
        return 1
Example #8
0
def confirm_question(question, default="yes", options=None):
    """Ask a yes/no question via raw_input() and return their answer.
    
    Arguments:
        question: is a string that is presented to the user.
        default: is the presumed answer if the user just hits <Enter>.
            It must be "yes" (the default), "no" or None (meaning
            an answer is required of the user).
        options: list of options
    
    Returns:
        value: boolean True for 'yes' answer, False --'no'.

    """
    if not options or 'confirm' not in options:
        return True
    logger = logging.getLogger(app_config_manager.get_property('logger.name'))
    valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
    if default is None:
        prompt = " [y/n] "
    elif default == "yes":
        prompt = " [Y/n] "
    elif default == "no":
        prompt = " [y/N] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)

    while True:
        logger.info(question + prompt)
        choice = raw_input().lower()
        if default is not None and choice == '':
            return valid[default]
        elif choice in valid:
            return valid[choice]
        else:
            logger.info("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
Example #9
0
def get_install_script():
    """Get Install Script Function.

    Generate bash command to install script

    Returns:
        Install script code body(type == str)

    """
    return 'echo alias {}=\\\"{}\\\" >> ~/.bashrc'.format(
        app_config_manager.get_property('name.short'),
        'python {}'.format(
            os.path.abspath(
                os.path.join(
                    os.path.dirname(__file__),
                    *[
                        '..',
                        '..',
                        'controller.py'
                    ]
                )
            )
        )
    )