# Path: glottolog3/models.py
# def get_parameter(pid):
# def get_source(key):
#     def github_url(self):
#     def __str__(self):
#     def get_stats(cls):
#     def get_identifier_objs(self, type_):
#     def get_ancestors(self, session=None):
#     def github_url(self):
#     def __json__(self, req=None, core=False):
#         def ancestor(l):
#     def get_geocoords(self):
#     def valueset_dict(self):
#     def classification(self, type_):
#     def fc(self):
#     def sc(self):
#     def _crefs(self, t):
#     def crefs(self):
#     def screfs(self):
#     def __rdf__(self, request):
#     def jqtree(self, icon_map=None):
#     def __rdf__(self, request):
#     def __bibtex__(self):
#     def url(self, req):
# class Provider(Base, IdNameDescriptionMixin):
# class Doctype(Base, IdNameDescriptionMixin):
# class Refdoctype(Base):
# class Refprovider(Base):
# class LanguoidLevel(DeclEnum):
# class Languoid(CustomModelMixin, Language):
# class Ref(CustomModelMixin, Source):
# class TreeClosureTable(Base):
# class LegacyCode(Base):
#     GLOTTOLOG_NAME = u'glottolog'
# 
# Path: glottolog3/__main__.py
# def main(args=None, catch_all=False, parsed_args=None, log=None):
#     try:
#         repos = Config.from_file().get_clone('glottolog')
#     except KeyError:  # pragma: no cover
#         repos = pathlib.Path('.')
#     parser, subparsers = get_parser_and_subparsers('glottolog3')
#     parser.add_argument(
#         '--repos',
#         help="clone of glottolog/glottolog",
#         default=repos,
#         type=pathlib.Path)
#     parser.add_argument(
#         '--repos-version',
#         help="version of repository data. Requires a git clone!",
#         default=None)
#     parser.add_argument(
#         '--pkg-dir',
#         help=argparse.SUPPRESS,
#         default=pathlib.Path(__file__).parent)
#     register_subcommands(subparsers, glottolog3.commands)
# 
#     args = parsed_args or parser.parse_args(args=args)
# 
#     if not hasattr(args, "main"):  # pragma: no cover
#         parser.print_help()
#         return 1
# 
#     with contextlib.ExitStack() as stack:
#         if not log:  # pragma: no cover
#             stack.enter_context(Logging(args.log, level=args.log_level))
#         else:
#             args.log = log
#         if args.repos_version:  # pragma: no cover
#             # If a specific version of the data is to be used, we make
#             # use of a Catalog as context manager:
#             stack.enter_context(Catalog(args.repos, tag=args.repos_version))
#         args.repos = Glottolog(args.repos)
#         args.log.info('glottolog/glottolog at {0}'.format(args.repos.repos))
#         try:
#             return args.main(args) or 0
#         except KeyboardInterrupt:  # pragma: no cover
#             return 0
#         except ParserError as e:  # pragma: no cover
#             print(e)
#             return main([args._command, '-h'])
#         except Exception as e:  # pragma: no cover
#             if catch_all:
#                 print(e)
#                 return 1
#             raise


import logging
import pathlib
import subprocess
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import close_all_sessions
from clld.db.meta import DBSession, Base
from glottolog3 import models
from glottolog3.__main__ import main
    from glottolog3.scripts import check_db_consistency
    from glottolog3.scripts import initializedb

TEST_DB = 'glottolog3-test'


@pytest.fixture
def repos():
    return pathlib.Path(__file__).parent / 'repos'


@pytest.fixture
def testdb(mocker):
    subprocess.check_call(['dropdb', '-U', 'postgres', '--if-exists', TEST_DB])
    subprocess.check_call(['createdb', '-U', 'postgres', TEST_DB])
    engine = create_engine('postgresql://postgres@/' + TEST_DB)
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)

    yield DBSession
    close_all_sessions()
    DBSession.remove()
    engine.dispose()
    subprocess.check_call(['dropdb', '-U', 'postgres', '--if-exists', TEST_DB])


def test_dbinit(mocker, testdb, capsys, repos):

    mocker.patch(
        'glottolog3.scripts.initializedb.assert_release', mocker.Mock(return_value='1.0'))
    initializedb.main(mocker.Mock(), repos=repos)
    initializedb.prime_cache(mocker.Mock(), repos=repos)

# Path: glottolog3/models.py
# def get_parameter(pid):
# def get_source(key):
#     def github_url(self):
#     def __str__(self):
#     def get_stats(cls):
#     def get_identifier_objs(self, type_):
#     def get_ancestors(self, session=None):
#     def github_url(self):
#     def __json__(self, req=None, core=False):
#         def ancestor(l):
#     def get_geocoords(self):
#     def valueset_dict(self):
#     def classification(self, type_):
#     def fc(self):
#     def sc(self):
#     def _crefs(self, t):
#     def crefs(self):
#     def screfs(self):
#     def __rdf__(self, request):
#     def jqtree(self, icon_map=None):
#     def __rdf__(self, request):
#     def __bibtex__(self):
#     def url(self, req):
# class Provider(Base, IdNameDescriptionMixin):
# class Doctype(Base, IdNameDescriptionMixin):
# class Refdoctype(Base):
# class Refprovider(Base):
# class LanguoidLevel(DeclEnum):
# class Languoid(CustomModelMixin, Language):
# class Ref(CustomModelMixin, Source):
# class TreeClosureTable(Base):
# class LegacyCode(Base):
#     GLOTTOLOG_NAME = u'glottolog'
# 
# Path: glottolog3/__main__.py
# def main(args=None, catch_all=False, parsed_args=None, log=None):
#     try:
#         repos = Config.from_file().get_clone('glottolog')
#     except KeyError:  # pragma: no cover
#         repos = pathlib.Path('.')
#     parser, subparsers = get_parser_and_subparsers('glottolog3')
#     parser.add_argument(
#         '--repos',
#         help="clone of glottolog/glottolog",
#         default=repos,
#         type=pathlib.Path)
#     parser.add_argument(
#         '--repos-version',
#         help="version of repository data. Requires a git clone!",
#         default=None)
#     parser.add_argument(
#         '--pkg-dir',
#         help=argparse.SUPPRESS,
#         default=pathlib.Path(__file__).parent)
#     register_subcommands(subparsers, glottolog3.commands)
# 
#     args = parsed_args or parser.parse_args(args=args)
# 
#     if not hasattr(args, "main"):  # pragma: no cover
#         parser.print_help()
#         return 1
# 
#     with contextlib.ExitStack() as stack:
#         if not log:  # pragma: no cover
#             stack.enter_context(Logging(args.log, level=args.log_level))
#         else:
#             args.log = log
#         if args.repos_version:  # pragma: no cover
#             # If a specific version of the data is to be used, we make
#             # use of a Catalog as context manager:
#             stack.enter_context(Catalog(args.repos, tag=args.repos_version))
#         args.repos = Glottolog(args.repos)
#         args.log.info('glottolog/glottolog at {0}'.format(args.repos.repos))
#         try:
#             return args.main(args) or 0
#         except KeyboardInterrupt:  # pragma: no cover
#             return 0
#         except ParserError as e:  # pragma: no cover
#             print(e)
#             return main([args._command, '-h'])
#         except Exception as e:  # pragma: no cover
#             if catch_all:
#                 print(e)
#                 return 1
#             raise


# Path: tests/test_cli.py
import logging
import pathlib
import subprocess
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import close_all_sessions
from clld.db.meta import DBSession, Base
from glottolog3 import models
from glottolog3.__main__ import main
    from glottolog3.scripts import check_db_consistency
    from glottolog3.scripts import initializedb


TEST_DB = 'glottolog3-test'


@pytest.fixture
def repos():
    return pathlib.Path(__file__).parent / 'repos'


@pytest.fixture
def testdb(mocker):
    subprocess.check_call(['dropdb', '-U', 'postgres', '--if-exists', TEST_DB])
    subprocess.check_call(['createdb', '-U', 'postgres', TEST_DB])
    engine = create_engine('postgresql://postgres@/' + TEST_DB)
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)

    yield DBSession
    close_all_sessions()
    DBSession.remove()
    engine.dispose()
    subprocess.check_call(['dropdb', '-U', 'postgres', '--if-exists', TEST_DB])


def test_dbinit(mocker, testdb, capsys, repos):

    mocker.patch(
        'glottolog3.scripts.initializedb.assert_release', mocker.Mock(return_value='1.0'))
    initializedb.main(mocker.Mock(), repos=repos)
    initializedb.prime_cache(mocker.Mock(), repos=repos)

    assert testdb.query(models.Ref).one().name == 'Huang, Shuanfan 2013'