# Path: pycddb/util.py
# def cddb_path(*comps):
#     return os.path.join(os.path.dirname(__file__), os.pardir, *comps)
# 
# def load_languages(delimiter='\t', return_type='dict'):
# 
#     with UnicodeReader(cddb_path('varieties', 'languages.tsv'), delimiter='\t') as reader:
#         data = list(reader)
#         
#     if return_type == 'dict':
#         out = {}
#         for line in data[1:]:
#             out[line[0]] = OrderedDict(zip(
#                 [h.lower() for h in data[0]],
#                 line))
#         return out
#     else:
#         return data
# 
# def get_transformer(profile, exception=None):
#     
#     profile = lp.csv2list(cddb_path('profiles', profile), strip_lines=False)
#     for i, line in enumerate(profile):
#         profile[i] = [unicodedata.normalize('NFD', clpa.normalize(x)) for x in line]
#     tokenizer = Tokenizer(profile, errors_replace=lambda x: "«{0}»".format(x))
#     
#     return lambda x, y: unicodedata.normalize(
#             'NFC',
#             tokenizer.transform(clpa.normalize(x), column=y, separator=' + ')
#             )


from clldutils.dsv import UnicodeReader
from clldutils.misc import cached_property
from pycddb.util import cddb_path, load_languages, get_transformer
from collections import OrderedDict
from pyconcepticon.api import Concepticon, Conceptlist 
from clldutils.path import Path, import_module
from lingpy import *
from sinopy import sinopy
import json
import os
_languages = load_languages(return_type='dict')


def csv2dict(path, key='id', prefix=''):
    d = {}
    with UnicodeReader(path, delimiter='\t') as reader:
        data = list(reader)
    
    idx = 0
    header = [h.lower() for h in data[0]]
    if key in header:
        idx = header.index(key.lower())
    for line in data[1:]:
        d[line[idx]] = OrderedDict(zip(
            [prefix+'_'+h if prefix else h for h in header],
            line))
    return d

def csv2list(path):
    with UnicodeReader(path, delimiter='\t') as reader:
        data = list(reader)
    header = [h.lower() for h in data[0]]
    return [dict(zip(header,line)) for line in data[1:]]

def json2dict(path):
    with open(path) as f:
        return json.load(f)


def data_path(path):

# Path: pycddb/util.py
# def cddb_path(*comps):
#     return os.path.join(os.path.dirname(__file__), os.pardir, *comps)
# 
# def load_languages(delimiter='\t', return_type='dict'):
# 
#     with UnicodeReader(cddb_path('varieties', 'languages.tsv'), delimiter='\t') as reader:
#         data = list(reader)
#         
#     if return_type == 'dict':
#         out = {}
#         for line in data[1:]:
#             out[line[0]] = OrderedDict(zip(
#                 [h.lower() for h in data[0]],
#                 line))
#         return out
#     else:
#         return data
# 
# def get_transformer(profile, exception=None):
#     
#     profile = lp.csv2list(cddb_path('profiles', profile), strip_lines=False)
#     for i, line in enumerate(profile):
#         profile[i] = [unicodedata.normalize('NFD', clpa.normalize(x)) for x in line]
#     tokenizer = Tokenizer(profile, errors_replace=lambda x: "«{0}»".format(x))
#     
#     return lambda x, y: unicodedata.normalize(
#             'NFC',
#             tokenizer.transform(clpa.normalize(x), column=y, separator=' + ')
#             )


# Path: pycddb/dataset.py
from clldutils.dsv import UnicodeReader
from clldutils.misc import cached_property
from pycddb.util import cddb_path, load_languages, get_transformer
from collections import OrderedDict
from pyconcepticon.api import Concepticon, Conceptlist 
from clldutils.path import Path, import_module
from lingpy import *
from sinopy import sinopy
import json
import os

_languages = load_languages(return_type='dict')


def csv2dict(path, key='id', prefix=''):
    d = {}
    with UnicodeReader(path, delimiter='\t') as reader:
        data = list(reader)
    
    idx = 0
    header = [h.lower() for h in data[0]]
    if key in header:
        idx = header.index(key.lower())
    for line in data[1:]:
        d[line[idx]] = OrderedDict(zip(
            [prefix+'_'+h if prefix else h for h in header],
            line))
    return d

def csv2list(path):
    with UnicodeReader(path, delimiter='\t') as reader:
        data = list(reader)
    header = [h.lower() for h in data[0]]
    return [dict(zip(header,line)) for line in data[1:]]

def json2dict(path):
    with open(path) as f:
        return json.load(f)


def data_path(path):

    return lambda *x: cddb_path('datasets', path, *x)