Example #1
0
def load_data(path_or_string, opts):
    """
    Load data in from a filepath or a string
    """
    kwargs = {}
    if not path_or_string:
        return kwargs
    try:
        d = serialize.json_to_obj(path_or_string)
        kwargs.update(d)
        return kwargs

    except ValueError as e:
        # only deal with these formats.
        if not ( path_or_string.endswith('.yaml') or 
            path_or_string.endswith('.yml') or 
            path_or_string.endswith('.json') ):
           return kwargs

        fp = os.path.expand_user(path_or_string)
        try:
            kwargs.update(serialize.yaml_to_obj(fp.read()))
            return kwargs
        except Exception as e:
            pass
    echo_error(RuntimeError("Could not parse input data:\n'{}'".format(e.message)),
        no_color=opts.no_color)
    sys.exit(1)
Example #2
0
def load_data(path_or_string):
    """
    Load data in from a filepath or a string
    """
    if not path_or_string:
        return {}

    # treat it as a file first
    if acceptable_file(path_or_string):
        fp = os.path.expanduser(path_or_string)
        try:
            return serialize.yaml_to_obj(open(fp).read())
        except Exception as e:
            raise IOError(
                "Could not read file '{}' \n{}"
                .format(fp, e.message))

    # otherwise assume it's a JSON blob
    else:
        try:
            return serialize.json_to_obj(path_or_string)

        except ValueError as e:
            raise ValueError(
                "Could not parse JSON string '{}' \n{}"
                .format(path_or_string, e.message))
Example #3
0
def _load_config_file(fp):
    """
    Attempt to load a file or raise a ConfigError
    """
    try:
        return yaml_to_obj(open(fp).read())
    except Exception as e:
        raise ConfigError("There was an error loding config '{}'.\n"
                          "Here is the error: \n{}".format(fp, e.message))
Example #4
0
def _load_config_file(fp):
    """
    Attempt to load a file or raise a ConfigError
    """
    try:
        return yaml_to_obj(open(fp).read())
    except Exception as e:
        raise ConfigError(
            "There was an error loding config '{}'.\n"
            "Here is the error: \n{}"
            .format(fp, e.message))
Example #5
0
def load_data(path_or_string):
    """
    Load data in from a filepath or a string
    """
    if not path_or_string:
        return {}

    # treat it as a file first
    if acceptable_file(path_or_string):
        fp = os.path.expanduser(path_or_string)
        try:
            return serialize.yaml_to_obj(open(fp).read())
        except Exception as e:
            raise IOError("Could not read file '{}' \n{}".format(
                fp, e.message))

    # otherwise assume it's a JSON blob
    else:
        try:
            return serialize.json_to_obj(path_or_string)

        except ValueError as e:
            raise ValueError("Could not parse JSON string '{}' \n{}".format(
                path_or_string, e.message))
Example #6
0
import os
import re
import importlib

from jsonschema import Draft4Validator

from newslynx.sc import SousChef as SC
from newslynx.exc import SousChefSchemaError
from newslynx.models import SousChef
from newslynx.lib.serialize import yaml_to_obj
from newslynx.lib.serialize import obj_to_json, json_to_obj
from newslynx.constants import SOUS_CHEF_RESERVED_FIELDS
from newslynx.util import here, update_nested_dict

# load souschef schema + validator.
SOUS_CHEF_JSON_SCHEMA = yaml_to_obj(
    open(here(__file__, 'sous_chef.yaml')).read())

# these are default options that all sous chefs have.
SOUS_CHEF_DEFAULT_OPTIONS = yaml_to_obj(
    open(here(__file__, 'sous_chef_default_options.yaml')).read())

# a json-schema validator for a sous chef.
SOUS_CHEF_VALIDATOR = Draft4Validator(SOUS_CHEF_JSON_SCHEMA)

# a regex for validation option + metric names
re_opt_name = re.compile(r'^[a-z][a-z_]+[a-z]$')


def validate(sc):
    """
    Validate a sous chef schema:
import os
import re
import importlib

from jsonschema import Draft4Validator

from newslynx.sc import SousChef as SC
from newslynx.exc import SousChefSchemaError
from newslynx.models import SousChef
from newslynx.lib.serialize import yaml_to_obj
from newslynx.lib.serialize import obj_to_json, json_to_obj
from newslynx.constants import SOUS_CHEF_RESERVED_FIELDS
from newslynx.util import here, update_nested_dict

# load souschef schema + validator.
SOUS_CHEF_JSON_SCHEMA = yaml_to_obj(
    open(here(__file__, 'sous_chef.yaml')).read())

# these are default options that all sous chefs have.
SOUS_CHEF_DEFAULT_OPTIONS = yaml_to_obj(
    open(here(__file__, 'sous_chef_default_options.yaml')).read())

# a json-schema validator for a sous chef.
SOUS_CHEF_VALIDATOR = Draft4Validator(SOUS_CHEF_JSON_SCHEMA)

# a regex for validation option + metric names
re_opt_name = re.compile(r'^[a-z][a-z_]+[a-z]$')


def validate(sc):
    """
    Validate a sous chef schema: