from rdflib import Graph
import pytest
import json


def test_wrap():
    """
    Example of intercepting a JSON-LD structure and performing some
    in-memory manipulation and then passing that structure to Graph.parse
    lists in the shacl graph.
    """

    data = """
    {
        "@context" : {
            "ngff" : "http://example.com/ns#"
        },
        "@graph": [{
            "@type": "ngff:ItemList",
            "ngff:collectionType": {"@type": "ngff:Image"},
            "ngff:itemListElement": [
                {
                    "@type": "ngff:Image",
                    "path": "image1",
                    "name": "Image 1"
                },
                {
                    "@type": "ngff:Image",
                    "path": "something-else",
                    "name": "bob"
                }
            ]
        }]
    }
    """


    # Current workaround
    data = json.loads(data)
    data = walk(data)
    data = json.dumps(data)  ## wasteful
    Graph().parse(data=data, format="json-ld")

    # Desired behavior
    data = json.loads(data)
    data = walk(data)
    Graph().parse(data=data, format="json-ld")


def walk(data, path=None):
    """
    Some arbitrary operation on a Python data structure.
    """

    if path is None:
        path = []

    if isinstance(data, dict):
        for k, v in data.items():
            data[k] = walk(v, path + [k])

    elif isinstance(data, list):
        replacement = list()
        for idx, item in enumerate(data):
            if path[-1] == "@graph":
                replacement.append(walk(item, path))
            else:
                wrapper = {
                    "@type": "ListItemWrapper",
                    "ngff:position": idx
                }
                wrapper["ngff:item"] = walk(item, path + [idx])
                replacement.append(wrapper)
        data = replacement

    return data

from rdflib import Graph
import json


def test_wrap():
    """
    Example of intercepting a JSON-LD structure and performing some
    in-memory manipulation and then passing that structure to Graph.parse
    lists in the shacl graph.
    """

    data = """
    {
        "@context" : {
            "ngff" : "http://example.com/ns#"
        },
        "@graph": [{
            "@type": "ngff:ItemList",
            "ngff:collectionType": {"@type": "ngff:Image"},
            "ngff:itemListElement": [
                {
                    "@type": "ngff:Image",
                    "path": "image1",
                    "name": "Image 1"
                },
                {
                    "@type": "ngff:Image",
                    "path": "something-else",
                    "name": "bob"
                }
            ]
        }]
    }
    """


    # Current workaround
    data = json.loads(data)
    data = walk(data)
    data = json.dumps(data)  ## wasteful
    Graph().parse(data=data, format="json-ld")

    # Desired behavior
    data = json.loads(data)
    data = walk(data)
    Graph().parse(data=data, format="json-ld")


def walk(data, path=None):
    """
    Some arbitrary operation on a Python data structure.
    """

    if path is None:
        path = []

    if isinstance(data, dict):
        for k, v in data.items():
            data[k] = walk(v, path + [k])

    elif isinstance(data, list):
        replacement = list()
        for idx, item in enumerate(data):
            if path[-1] == "@graph":
                replacement.append(walk(item, path))
            else:
                wrapper = {
                    "@type": "ListItemWrapper",
                    "ngff:position": idx
                }
                wrapper["ngff:item"] = walk(item, path + [idx])
                replacement.append(wrapper)
        data = replacement

    return data

Remove use of pytest