# 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=_languages, _path=cddb_path):
        self.id = name
        self.get_path = data_path(name)
        self.path = self.get_path('')
        self._data = {}

        # get the languages
        self.languages = csv2dict(self.get_path('languages.csv'), key=base,
                prefix=self.id)
        self.lid2lang = dict([(self.languages[x][self.id+'_id'], x) for x in
            self.languages])
        for k in self.languages:
            for h, v in _languages[k].items():
                self.languages[k][h.lower()] = v

        self.metadata = json2dict(self.get_path('metadata.json'))
        #with with_sys_path(Path(_path('datasets'))) as f:
        self.commands = import_module(Path(_path('datasets', self.id)))
        
        # try to get a concept list
        clist = False
        if os.path.isfile(self.get_path('concepts.csv')):
            clist = Conceptlist.from_file(self.get_path('concepts.csv'))
        elif 'concepts' in self.metadata:
            clist = Concepticon().conceptlists[self.metadata['concepts']]
        if clist:
            self.concepts = {c.gloss or c.english : c for c in
                    clist.concepts.values()}

        if 'profile' in self.metadata:

# 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=_languages, _path=cddb_path):
        self.id = name
        self.get_path = data_path(name)
        self.path = self.get_path('')
        self._data = {}

        # get the languages
        self.languages = csv2dict(self.get_path('languages.csv'), key=base,
                prefix=self.id)
        self.lid2lang = dict([(self.languages[x][self.id+'_id'], x) for x in
            self.languages])
        for k in self.languages:
            for h, v in _languages[k].items():
                self.languages[k][h.lower()] = v

        self.metadata = json2dict(self.get_path('metadata.json'))
        #with with_sys_path(Path(_path('datasets'))) as f:
        self.commands = import_module(Path(_path('datasets', self.id)))
        
        # try to get a concept list
        clist = False
        if os.path.isfile(self.get_path('concepts.csv')):
            clist = Conceptlist.from_file(self.get_path('concepts.csv'))
        elif 'concepts' in self.metadata:
            clist = Concepticon().conceptlists[self.metadata['concepts']]
        if clist:
            self.concepts = {c.gloss or c.english : c for c in
                    clist.concepts.values()}

        if 'profile' in self.metadata:

            self.transform = get_transformer(self.metadata['profile'])