Example #1
0
def test_run_apifile_modified(tmpdir):
    c = {'a': 'b', 'c': 'd', 'state': {'nodes': 0}}
    apifile = tmpdir.join('api_file.json')
    apifile.write_text(j_dump(c), 'utf-8')

    nodelist = tmpdir.join('nodelist.json')
    nodelist.write_text(j_dump({
        'version': 1,
        'nodes': [
            {'status': {'clients': 23, 'online': True}},
            {'status': {'clients': 42, 'online': False}}
        ],
        'updated_at': 'never'
    }), 'utf-8')

    side = tmpdir.join('a.json')
    car = tmpdir.join('c.yaml')

    assert tmpdir.listdir() == [apifile, nodelist]

    assert run([
        str(apifile),
        '-n', str(nodelist),
        '-s', str(side), str(car)
    ]) is False

    assert sorted(tmpdir.listdir()) == sorted([side, apifile, car, nodelist])

    assert j_load(side.read_text('utf-8')) == 'b'
    assert y_load(car.read_text('utf-8')) == 'd'

    c['state']['nodes'] = 1
    assert j_load(apifile.read_text('utf-8')) == c

    assert tmpdir.remove() is None
def main():
    inp_name = 'input.yml' if len(sys.argv) < 2 else sys.argv[1]
    out_name = 'output' if len(sys.argv) < 3 else sys.argv[2]

    with open(inp_name, 'r') as f:
        debug_info = y_load(f.read())

    save_debug_dump(debug_info)

    return
Example #3
0
def load_struct(content, fallback=None, as_yaml=False,):
    '''
    Contextmanager to unpickle either *json* or *yaml* from a string

    :param content: string to unpickle
    :param fallback: data to return in case of unpickle failure
    :param as_yaml: read as *yaml* instead of *json*
    :yield: unpickled ``content``
    '''
    try:
        yield (
            y_load(content) if as_yaml else j_load(content)
        ) if isinstance(content, str) else fallback
    except (ValueError, ScannerError, ParserError):
        yield fallback
Example #4
0
def load_struct(
    content,
    fallback=None,
    as_yaml=False,
):
    '''
    Contextmanager to unpickle either *json* or *yaml* from a string

    :param content: string to unpickle
    :param fallback: data to return in case of unpickle failure
    :param as_yaml: read as *yaml* instead of *json*
    :yield: unpickled ``content``
    '''
    try:
        yield (y_load(content) if as_yaml else j_load(content)) if isinstance(
            content, str) else fallback
    except (ValueError, ScannerError, ParserError):
        yield fallback
Example #5
0
def test_load_struct_yaml():
    for ty in ['', 'a', 'a:', 'a: b', 'a: [b, c]']:
        with load_struct(ty, as_yaml=True) as t:
            assert t == y_load(ty)
def load_yml_config(path):
    with open(path, 'r', encoding='utf-8') as fp:
        return dict_to_simple_namespace(y_load(fp, Loader=YamlLoader))