Beispiel #1
0
def test_cls(clear_database_before_test):
    """Test that only instances of `cls` or its subclasses are matched by ``GroupPath``."""
    for label in ['a', 'a/b', 'a/c/d', 'a/c/e/g']:
        orm.Group.objects.get_or_create(label)
    for label in ['a/c/e', 'a/f']:
        orm.UpfFamily.objects.get_or_create(label)
    group_path = GroupPath()
    assert sorted([c.path for c in group_path.walk()
                   ]) == ['a', 'a/b', 'a/c', 'a/c/d', 'a/c/e', 'a/c/e/g']
    group_path = GroupPath(cls=orm.UpfFamily)
    assert sorted([c.path for c in group_path.walk()
                   ]) == ['a', 'a/c', 'a/c/e', 'a/f']
    assert GroupPath('a/b/c') != GroupPath('a/b/c', cls=orm.UpfFamily)
Beispiel #2
0
def test_walk_with_invalid_path(clear_database_before_test):
    """Test the ``GroupPath.walk`` method with invalid paths."""
    for label in [
            'a', 'a/b', 'a/c/d', 'a/c/e/g', 'a/f', 'bad//group', 'bad/other'
    ]:
        orm.Group.objects.get_or_create(label)
    group_path = GroupPath()
    expected = [
        'a', 'a/b', 'a/c', 'a/c/d', 'a/c/e', 'a/c/e/g', 'a/f', 'bad',
        'bad/other'
    ]
    assert [c.path for c in sorted(group_path.walk())] == expected
Beispiel #3
0
def group_path_ls(path, type_string, recursive, as_table, no_virtual,
                  with_description, no_warn):
    # pylint: disable=too-many-arguments,too-many-branches
    """Show a list of existing group paths."""
    from aiida.plugins import GroupFactory
    from aiida.tools.groups.paths import GroupPath, InvalidPath

    try:
        path = GroupPath(path or '',
                         cls=GroupFactory(type_string),
                         warn_invalid_child=not no_warn)
    except InvalidPath as err:
        echo.echo_critical(str(err))

    if recursive:
        children = path.walk()
    else:
        children = path.children

    if as_table or with_description:
        from tabulate import tabulate
        headers = ['Path', 'Sub-Groups']
        if with_description:
            headers.append('Description')
        rows = []
        for child in sorted(children):
            if no_virtual and child.is_virtual:
                continue
            row = [
                child.path if child.is_virtual else click.style(child.path,
                                                                bold=True),
                len([c for c in child.walk() if not c.is_virtual])
            ]
            if with_description:
                row.append(
                    '-' if child.is_virtual else child.get_group().description)
            rows.append(row)
        echo.echo(tabulate(rows, headers=headers))
    else:
        for child in sorted(children):
            if no_virtual and child.is_virtual:
                continue
            echo.echo(child.path, bold=not child.is_virtual)
Beispiel #4
0
def test_walk(setup_groups):
    """Test the ``GroupPath.walk()`` function."""
    group_path = GroupPath()
    assert [c.path for c in sorted(group_path.walk())
            ] == ['a', 'a/b', 'a/c', 'a/c/d', 'a/c/e', 'a/c/e/g', 'a/f']