# Path: tools/py/serial/literate.py
# TYPE_REL = I(iri.absolutize('type', VERSA_BASEIRI))
# LINE_START_ESCAPE_PAT = re.compile(r'^(#|\*|-|=|<|_)')
# LINE_START_AFTER_SPACE_ESCAPE_PAT = re.compile(r'^(\s+)(\*|-)')
# def longtext(t):
# def abbreviate(rel, bases):
# def value_format(val):
# def write(model, out=sys.stdout, base=None, schema=None, shorteners=None):
# def md_escape(t):


import re
import json
import logging
import operator
from operator import truth
from itertools import chain, islice, repeat, starmap, takewhile
from versa import I, VERSA_BASEIRI, ORIGIN, RELATIONSHIP, TARGET
from versa.driver.memory import newmodel
from .literate import parse as markdown_parse
                    adapted_keys += '_'
                adapted_keys[k] = adapted
            first_proper_row = False

        for k, ad_k in adapted_keys.items():
            row[ad_k] = row[k]
        if isinstance(template_obj, str):
            vliterate_text = template_obj.format(**row)
        else:
            vliterate_text = template_obj(row)
        model = model_fact()
        markdown_parse(vliterate_text, model)
        yield model


# Optimized version courtesy https://stackoverflow.com/a/34935239
def chunker(n, iterable):  # n is size of each chunk; last chunk may be smaller
    # operator.truth is *significantly* faster than bool for the case of
    # exactly one positional argument
    return takewhile(truth, map(tuple, starmap(islice, repeat((iterable, n)))))


# New, consistent API
def do_parse(csvobj, adapted_keys, vliterate_template, model):
    at_least_one_row = False
    for row in csvobj:
        at_least_one_row = True
        for k, ad_k in adapted_keys.items():
            row[ad_k] = row[k]
        vliterate_text = vliterate_template.format(**row)

# Path: tools/py/serial/literate.py
# TYPE_REL = I(iri.absolutize('type', VERSA_BASEIRI))
# LINE_START_ESCAPE_PAT = re.compile(r'^(#|\*|-|=|<|_)')
# LINE_START_AFTER_SPACE_ESCAPE_PAT = re.compile(r'^(\s+)(\*|-)')
# def longtext(t):
# def abbreviate(rel, bases):
# def value_format(val):
# def write(model, out=sys.stdout, base=None, schema=None, shorteners=None):
# def md_escape(t):


# Path: tools/py/serial/simpleobj.py
import re
import json
import logging
import operator
from operator import truth
from itertools import chain, islice, repeat, starmap, takewhile
from versa import I, VERSA_BASEIRI, ORIGIN, RELATIONSHIP, TARGET
from versa.driver.memory import newmodel
from .literate import parse as markdown_parse

                    adapted_keys += '_'
                adapted_keys[k] = adapted
            first_proper_row = False

        for k, ad_k in adapted_keys.items():
            row[ad_k] = row[k]
        if isinstance(template_obj, str):
            vliterate_text = template_obj.format(**row)
        else:
            vliterate_text = template_obj(row)
        model = model_fact()
        markdown_parse(vliterate_text, model)
        yield model


# Optimized version courtesy https://stackoverflow.com/a/34935239
def chunker(n, iterable):  # n is size of each chunk; last chunk may be smaller
    # operator.truth is *significantly* faster than bool for the case of
    # exactly one positional argument
    return takewhile(truth, map(tuple, starmap(islice, repeat((iterable, n)))))


# New, consistent API
def do_parse(csvobj, adapted_keys, vliterate_template, model):
    at_least_one_row = False
    for row in csvobj:
        at_least_one_row = True
        for k, ad_k in adapted_keys.items():
            row[ad_k] = row[k]
        vliterate_text = vliterate_template.format(**row)

        markdown_parse(vliterate_text, model)