from os import path

from setuptools import setup, find_packages
from setuptools.extension import Extension

try:
    pkg_name = 'gnes'
    libinfo_py = path.join(pkg_name, '__init__.py')
    libinfo_content = open(libinfo_py, 'r').readlines()
    version_line = [l.strip() for l in libinfo_content if l.startswith('__version__')][0]
    exec(version_line)  # produce __version__
except FileNotFoundError:
    __version__ = '0.0.0'

try:
    with open('README.md') as fp:
        _long_description = fp.read()
except FileNotFoundError:
    _long_description = ''

extensions = [
    Extension(
        'gnes.indexer.vector.bindexer.cython',
        ['gnes/indexer/vector/bindexer/bindexer.pyx'],
        extra_compile_args=['-O3'],
    ),
    Extension(
        'gnes.indexer.vector.hbindexer.cython',
        ['gnes/indexer/vector/hbindexer/hbindexer.pyx'],
        extra_compile_args=['-O3'],
    ),
]

base_dep = [
    'numpy',
    'scipy',
    'termcolor',
    'protobuf',
    'grpcio',
    'ruamel.yaml>=0.15.89',
    'pyzmq>=17.1.0',
]

extras_dep = {
    'bert': ['bert-serving-server>=1.8.6', 'bert-serving-client>=1.8.6'],
    # 'elmo': [
    #     'elmoformanylangs @ git+https://github.com/HIT-SCIR/ELMoForManyLangs.git@master#egg=elmoformanylangs-0.0.2',
    #     'paramiko', 'pattern3'],
    'flair': ['flair>=0.4.1'],
    'annoy': ['annoy==1.15.2'],
    'chinese': ['jieba'],
    'vision': ['opencv-python>=4.0.0', 'imagehash>=4.0'],
    'leveldb': ['plyvel>=1.0.5'],
    'test': ['pylint', 'memory_profiler>=0.55.0', 'psutil>=5.6.1', 'gputil>=1.4.0'],
    'http': ['flask', 'flask-compress', 'flask-cors', 'flask-json', 'aiohttp==3.5.4'],
    'onnx': ['onnxruntime'],
    'librosa': ['librosa>=0.7.0']
}


def combine_dep(new_key, base_keys):
    extras_dep[new_key] = list(set(k for v in base_keys for k in extras_dep[v]))


combine_dep('nlp', ['bert', 'flair'])
combine_dep('cn_nlp', ['chinese', 'nlp'])
combine_dep('all', [k for k in extras_dep if k != 'elmo'])

setup(
    name=pkg_name,
    packages=find_packages(),
    version=__version__,
    include_package_data=True,
    description='GNES is Generic Neural Elastic Search,'
                ' a cloud-native semantic search system based on deep neural network.',
    author='GNES team',
    author_email='team@gnes.ai',
    license='Apache 2.0',
    url='https://gnes.ai',
    download_url='https://github.com/gnes-ai/gnes/tags',
    long_description=_long_description,
    long_description_content_type='text/markdown',
    zip_safe=False,
    setup_requires=[
        'setuptools>=18.0',
        'cython',
    ],
    ext_modules=extensions,
    install_requires=base_dep,
    extras_require=extras_dep,
    entry_points={
        'console_scripts': ['gnes=gnes.cli:main'],
    },
    classifiers=(
        'Intended Audience :: Developers',
        'Intended Audience :: Education',
        'Intended Audience :: Science/Research',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Cython',
        'Programming Language :: Unix Shell',
        'Environment :: Console',
        'License :: OSI Approved :: Apache Software License',
        'Operating System :: OS Independent',
        'Topic :: Database :: Database Engines/Servers',
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
        'Topic :: Internet :: WWW/HTTP :: Indexing/Search',
        'Topic :: Scientific/Engineering :: Image Recognition',
        'Topic :: Multimedia :: Video',
        'Topic :: Scientific/Engineering',
        'Topic :: Scientific/Engineering :: Mathematics',
        'Topic :: Software Development',
        'Topic :: Software Development :: Libraries',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ),
    keywords='gnes cloud-native semantic search elastic neural-network encoding embedding serving',
)
