#
#  Created by Soldoskikh Kirill.
#  Copyright 2018 Intuition. All rights reserved.
#

import os
import platform
from setuptools import setup
from setuptools.command.build_ext import build_ext
from distutils.extension import Extension
from Cython.Build import cythonize
from rbp_setup_tools.code_generation import generate_from_cython_src
from rbp_setup_tools.types import TYPES


if platform.system() == 'Darwin':

    compile_opts = [ '-std=c++11', 
                     '-mmacosx-version-min={:}'.format( platform.mac_ver()[0] ),
                     '-Ofast' ]

elif platform.system() == 'Linux':

    compile_opts = [ '-std=c++11',  
                     '-Ofast' ]

elif platform.system() == 'Windows':
    
    compile_opts = [ '-std=c++11',  
                     '-Ofast' ]     

else:
    raise EnvironmentError( 'Not supported platform: {plat}'.format(plat=platform.system()) )
#--------------------------------------------------------------------------------------------
# Generate cython code for all supporting types
#--------------------------------------------------------------------------------------------
src_1 = './redblackpy/cython_source/__dtype_tree_processing.pxi'
src_2 = './redblackpy/cython_source/__tree_series_dtype.pxi'
src_3 = './redblackpy/cython_source/__interpolation.pxi'
src_4 = './redblackpy/cython_source/__arithmetic.pxi'

src_1 = open(src_1, 'r')
src_2 = open(src_2, 'r')
src_3 = open(src_3, 'r')
src_4 = open(src_4, 'r')

output_1 = open('./redblackpy/cython_source/dtype_tree_processing.pxi', 'w')
output_2 = open('./redblackpy/cython_source/tree_series_dtype.pxi', 'w')
output_3 = open('./redblackpy/cython_source/interpolation.pxi', 'w')
output_4 = open('./redblackpy/cython_source/arithmetic.pxi', 'w')

generate_from_cython_src(src_1, output_1, TYPES[:-1], 0)
generate_from_cython_src(src_2, output_2, TYPES, 14)
generate_from_cython_src(src_3, output_3, TYPES, 0)
generate_from_cython_src(src_4, output_4, TYPES, 0)

src_1.close()
src_2.close()
src_3.close()
src_4.close()

output_1.close()
output_2.close()
output_3.close()
output_4.close()
#--------------------------------------------------------------------------------------------

ext_modules=[ Extension( "redblackpy.series.tree_series",
                         sources=["redblackpy/series/tree_series.pyx"],
                         extra_compile_args=compile_opts,
                         extra_link_args=compile_opts[:-1],
                         language = "c++",
                         include_dirs=['./redblackpy'],
                         depends=[ 'core/tree/tree.hpp',
                                   'core/tree/rb_tree.tpp'
                                   'core/tree/rb_node.tpp',
                                   'core/tree/rb_node_valued.tpp',
                                   'core/trees_iterator/iterator.hpp',
                                   'core/trees_iterator/iterator.tpp' ], ),

              Extension( "redblackpy.series.series_iterator",
                         sources=["redblackpy/series/series_iterator.pyx"],
                         extra_compile_args=compile_opts,
                         extra_link_args=compile_opts[:-1],
                         language = "c++",
                         include_dirs=['./redblackpy'],
                         depends=[ 'core/tree/tree.hpp',
                                   'core/tree/rb_tree.tpp'
                                   'core/tree/rb_node.tpp',
                                   'core/tree/rb_node_valued.tpp',
                                   'core/trees_iterator/iterator.hpp',
                                   'core/trees_iterator/iterator.tpp' ], ),
              
              Extension( "redblackpy.benchmark.timer",
                         sources=["redblackpy/benchmark/timer.pyx"],
                         extra_compile_args=compile_opts,
                         extra_link_args=compile_opts[:-1],
                         language = "c++",
                         include_dirs=['./redblackpy'] ) ]

setup( name='redblackpy',
       ext_modules = cythonize(ext_modules),
       version='0.1.3.0',
       author='Solodskikh Kirill',
       author_email='hypo@intuition.engineering',
       maintainer='Intuition',
       maintainer_email='dev@intuition.engineering',
       install_requires=['cython'],
       description='Data structures based on red-black trees.',
       url='https://intuitionengineeringteam.github.io/RedBlackPy/',
       download_url='https://github.com/IntuitionEngineeringTeam/RedBlackPy/archive/master.zip',
       zip_safe=False,
       packages=[ 'redblackpy', 'redblackpy.series', 
                  'redblackpy.benchmark', 'redblackpy.tree_cython_api'],
       package_data={'redblackpy.series': ['*.pxd']},
       include_package_data=True,
       license='Apache License 2.0', 
       long_description='RedBlackPy is a light Python library that provides data structures \
       aimed to fast insertion, removal and self sorting to manipulating ordered data in efficient way.\
       The core part of the library had been written on C++ and then was wrapped in Cython. \
       Hope that many would find the primary data structures of this library very handy in working \
       with time series. One of the main feature of this structures is an access by arbitrary \
       key using interpolation, what makes processing of multiple non synchronized time series very simple.\
       All data structures based on red black trees.',
       classifiers = [ 'Programming Language :: Python :: 2.7',
                       'Programming Language :: Python :: 3' ] )

[((52, 0, 52, 56), 'rbp_setup_tools.code_generation.generate_from_cython_src', 'generate_from_cython_src', ({(52, 25, 52, 30): 'src_1', (52, 32, 52, 40): 'output_1', (52, 42, 52, 52): 'TYPES[:-1]', (52, 54, 52, 55): '(0)'}, {}), '(src_1, output_1, TYPES[:-1], 0)', False, 'from rbp_setup_tools.code_generation import generate_from_cython_src\n'), ((53, 0, 53, 52), 'rbp_setup_tools.code_generation.generate_from_cython_src', 'generate_from_cython_src', ({(53, 25, 53, 30): 'src_2', (53, 32, 53, 40): 'output_2', (53, 42, 53, 47): 'TYPES', (53, 49, 53, 51): '(14)'}, {}), '(src_2, output_2, TYPES, 14)', False, 'from rbp_setup_tools.code_generation import generate_from_cython_src\n'), ((54, 0, 54, 51), 'rbp_setup_tools.code_generation.generate_from_cython_src', 'generate_from_cython_src', ({(54, 25, 54, 30): 'src_3', (54, 32, 54, 40): 'output_3', (54, 42, 54, 47): 'TYPES', (54, 49, 54, 50): '(0)'}, {}), '(src_3, output_3, TYPES, 0)', False, 'from rbp_setup_tools.code_generation import generate_from_cython_src\n'), ((55, 0, 55, 51), 'rbp_setup_tools.code_generation.generate_from_cython_src', 'generate_from_cython_src', ({(55, 25, 55, 30): 'src_4', (55, 32, 55, 40): 'output_4', (55, 42, 55, 47): 'TYPES', (55, 49, 55, 50): '(0)'}, {}), '(src_4, output_4, TYPES, 0)', False, 'from rbp_setup_tools.code_generation import generate_from_cython_src\n'), ((16, 3, 16, 20), 'platform.system', 'platform.system', ({}, {}), '()', False, 'import platform\n'), ((68, 14, 79, 74), 'distutils.extension.Extension', 'Extension', (), '', False, 'from distutils.extension import Extension\n'), ((81, 14, 92, 74), 'distutils.extension.Extension', 'Extension', (), '', False, 'from distutils.extension import Extension\n'), ((94, 14, 99, 56), 'distutils.extension.Extension', 'Extension', (), '', False, 'from distutils.extension import Extension\n'), ((22, 5, 22, 22), 'platform.system', 'platform.system', ({}, {}), '()', False, 'import platform\n'), ((102, 21, 102, 43), 'Cython.Build.cythonize', 'cythonize', ({(102, 31, 102, 42): 'ext_modules'}, {}), '(ext_modules)', False, 'from Cython.Build import cythonize\n'), ((27, 5, 27, 22), 'platform.system', 'platform.system', ({}, {}), '()', False, 'import platform\n'), ((19, 56, 19, 74), 'platform.mac_ver', 'platform.mac_ver', ({}, {}), '()', False, 'import platform\n'), ((33, 73, 33, 90), 'platform.system', 'platform.system', ({}, {}), '()', False, 'import platform\n')]