# 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 csv
import logging
import operator
import inspect
from operator import truth
from itertools import chain, islice, repeat, starmap, takewhile
from amara3 import iri
from versa import I, VERSA_BASEIRI, ORIGIN, RELATIONSHIP, TARGET
from versa.terms import VERSA_BASEIRI, RDF_NS, RDFS_NS, VERSA_TYPE_REL, RDF_TYPE_REL
from versa.util import all_origins, lookup, resourcetypes
from versa import I, VERSA_BASEIRI
from versa.driver.memory import newmodel
from .literate import parse as markdown_parse
                while adapted in adapted_keys:
                    adapted_keys += '_'
                adapted_keys[k] = adapted
            first_proper_row = False

        for k, ad_k in adapted_keys.items():
            row[ad_k] = row[k]
        if inspect.isgeneratorfunction(prerow):
            yield from process_rows(prerow(row))
        elif prerow:
            yield from process_rows([prerow(row)])
        else:
            yield from process_rows([row])


# 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/csv.py
import re
import csv
import logging
import operator
import inspect
from operator import truth
from itertools import chain, islice, repeat, starmap, takewhile
from amara3 import iri
from versa import I, VERSA_BASEIRI, ORIGIN, RELATIONSHIP, TARGET
from versa.terms import VERSA_BASEIRI, RDF_NS, RDFS_NS, VERSA_TYPE_REL, RDF_TYPE_REL
from versa.util import all_origins, lookup, resourcetypes
from versa import I, VERSA_BASEIRI
from versa.driver.memory import newmodel
from .literate import parse as markdown_parse

                while adapted in adapted_keys:
                    adapted_keys += '_'
                adapted_keys[k] = adapted
            first_proper_row = False

        for k, ad_k in adapted_keys.items():
            row[ad_k] = row[k]
        if inspect.isgeneratorfunction(prerow):
            yield from process_rows(prerow(row))
        elif prerow:
            yield from process_rows([prerow(row)])
        else:
            yield from process_rows([row])


# 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)