def test_relative_path(
    simple_root,
    current_path: str,
    target_path: str,
    expected: str,
):
    from wired_components.request import (
        find_resource,
        relative_path,
    )
    current = find_resource(simple_root, current_path)
    target = find_resource(simple_root, target_path)
    result: str = relative_path(simple_root, current, target)
    assert result == expected
示例#2
0
def load_resources(contents: Path) -> Root:
    # Make the root resource
    root_yaml = load_yaml(contents / 'index.yaml')
    root_yaml.pop('type')
    root = Root(name='', parent=None, **root_yaml)

    # Next do all the folders by finding all 'index.yaml'
    for resource in Path(contents).glob('**/index.yaml'):
        if resource.parent == contents:
            # We hit top-level index.yaml which we already used for root
            continue

        # Load yaml for this folder and make an instance
        folder_yaml = load_yaml(resource)

        # Find the parent folder, which should already be in root
        parent_path = resource.parents[1].relative_to(contents)
        if str(parent_path) == '.':
            parent_path = Path('')
        parent = find_resource(root, '/' + str(parent_path))
        folder_name = str(resource.parent.stem)
        folder_yaml.pop('type')
        folder = Collection(name=folder_name, parent=parent, **folder_yaml)
        parent[folder_name] = folder

    # Finally, do all the documents, now that we have a place to put them
    for resource in Path(contents).glob('**/*.yaml'):
        if resource.name == 'index.yaml':
            # Skip this, we already did it
            continue
        resource_yaml = load_yaml(resource)
        resource_yaml.pop('type')
        name = str(resource.stem)

        # Get the parent via resource path
        parent_path = resource.parent.relative_to(contents)
        parent = find_resource(root, '/' + str(parent_path))
        resource = Document(name=name, parent=parent, **resource_yaml)
        parent[name] = resource

    return root
def test_static_relative_path(
    simple_root,
    current_path: str,
    expected: str,
):
    from wired_components.request import (
        find_resource,
        relative_static_path,
    )
    current = find_resource(simple_root, current_path)
    result: str = relative_static_path(current, 'static/foo.css')
    assert result == expected
def test_resource_path(
    simple_root,
    target_path: str,
    expected: str,
):
    from wired_components.request import (
        find_resource,
        resource_path,
    )
    resource = find_resource(simple_root, target_path)
    path = resource_path(resource)
    assert expected == path
def test_parents(
    simple_root,
    this_path: str,
    expected: Tuple[str],
):
    from wired_components.request import (
        find_resource,
        parents,
        resource_path,
    )
    resource = find_resource(simple_root, this_path)
    results = parents(resource)
    result = tuple(
        ((resource.title, resource_path(resource)) for resource in results))
    assert result == expected
示例#6
0
def render_path(registry: ServiceRegistry, resource_path: str) -> str:
    """ Render a resource at a path, to a string """

    # Get the root, then the context at the path
    container1: ServiceContainer = registry.create_container()
    root: Root = container1.get(IRoot)
    context: Resource = find_resource(root, resource_path)

    # Now make a container to process this context
    container2: ServiceContainer = registry.create_container(context=context)
    view: View = container2.get(IView)

    renderer: JinjaRenderer = container2.get(IJinjaRenderer)
    context_dict = as_dict(view)
    template_name = view.template
    markup: Markup = renderer.render(context_dict,
                                     template_name=template_name,
                                     container=container2)

    return str(markup)
def test_find_resource(simple_root, path: str, expected: str):
    from wired_components.request import find_resource
    resource = find_resource(simple_root, path)
    assert expected == resource.name