示例#1
0
def load_yaml_file(file,
                   path=None,
                   get_all=False,
                   skip_error=False,
                   extension=YAML_EXT,
                   return_path=False,
                   logger=True,
                   keep_order=False):
    """
    Import any data from a YAML file.

    NOTE: the logger problem

    Since YAML are important files for configuration in our case,
    we may be in the situation of not having the loggers yet,
    since the configuration in itself is needed to configure the logger.

    In that case we have logger=False and a silenced read.
    """

    if logger:
        from utilities.logs import get_logger
        log = get_logger(__name__)

    filepath = get_yaml_path(path, file, extension)

    if not return_path and logger:
        log.very_verbose("Reading file %s" % filepath)

    # load from this file
    error = None
    if not os.path.exists(filepath):
        error = 'File does not exist'
    else:
        if return_path:
            return filepath

        with open(filepath) as fh:
            try:
                loader = get_loader(fh, keep_order)
            except Exception as e:
                error = e
            else:
                docs = list(loader)
                if get_all:
                    return docs

                if len(docs) > 0:
                    return docs[0]

                message = "YAML file is empty %s" % filepath
                if logger:
                    log.exit(message)
                else:
                    raise AttributeError(message)

    # # IF dealing with a strange exception string (escaped)
    # import codecs
    # mystring, _ = codecs.getdecoder("unicode_escape")(str(error))
    # message = "Failed to read YAML file [%s]: %s" % (filepath, mystring)

    message = "Failed to read YAML file [%s]: %s" % (filepath, error)
    if logger:
        log.warning(message)
    elif not skip_error:
        raise AttributeError(message)
    # else:
    #     pass
    return {}
示例#2
0
# -*- coding: utf-8 -*-

import os
import time
import click
import better_exceptions as be
from flask.cli import FlaskGroup
from utilities.logs import get_logger
from restapi import __package__ as current_package

APP = 'FLASK_APP'
PORT = 'FLASK_PORT'

log = get_logger(__name__)


@click.group()
# @click.option('--debug/--no-debug', default=False)
# def cli(debug):
def cli():
    # click.echo('Debug mode is %s' % ('on' if debug else 'off'))
    click.echo('*** RESTful HTTP API ***')


def main(args, another_app=None):

    if another_app is not None:
        os.environ[APP] = '%s.py' % another_app
    else:
        current_app = os.environ.get(APP)
        if current_app is None or current_app.strip() == '':
示例#3
0
def setup_logger(name, level_name):

    log_level = getattr(logging, level_name.upper())
    set_global_log_level(package=name, app_level=log_level)
    # log.critical("TRAVIS: %s, %s", name, log_level)
    return get_logger(name)