import sys
import re
import os.path
from setuptools import setup, find_packages, Extension
import numpy as np
from Cython.Distutils import build_ext
from distutils.errors import CCompilerError, DistutilsExecError, DistutilsPlatformError
import Cython.Compiler.Options



# a define the version sting inside the package
# see https://stackoverflow.com/questions/458550/standard-way-to-embed-version-into-python-package 
VERSIONFILE="abel/_version.py"
verstrline = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
if mo:
    version = mo.group(1)
else:
    raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))



Cython.Compiler.Options.annotate = False

if sys.platform != 'win32':
    compile_args =  dict( extra_compile_args=['-O2', '-march=native'],
                 extra_link_args=['-O2', '-march=native'])
else:
    compile_args = {}

# Optional compilation of Cython modules adapted from
# https://github.com/bsmurphy/PyKrige which was itself adapted from a StackOverflow post


ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError)

class TryBuildExt(build_ext):
    def build_extensions(self):
        try:
            build_ext.build_extensions(self)
        except ext_errors:
            print("**************************************************")
            print("WARNING: Cython extensions failed to build (used in abel.direct).\n"
                  "Typical reasons for this problem are:\n"
                  "  - the C compiler is not installed or not found\n"
                  "  - issues using mingw compiler on Windows 64bit (experimental support for now)\n"
                  "This only means that the abel.direct implementation will not be available.\n")
            print("**************************************************")
            # continue the install
            pass
        except:
            raise


ext_modules=[
    Extension("abel.lib.direct",
             [os.path.join("abel","lib","direct.pyx")],
             libraries=["m"],
             **compile_args),
    ]


setup(name='PyAbel',
      version=version,
      description='A Python package for inverse Abel transforms',
      author='Dan Hickstein',
      packages=find_packages(),
      package_data={'abel': ['tests/data/*' ]},
      cmdclass= {'build_ext': TryBuildExt},
      ext_modules= ext_modules,
      include_dirs=[ np.get_include() ],
      install_requires=[
              "numpy >= 1.6",
              "setuptools >= 16.0",
              "scipy >= 0.14",
              "cython >= 0.22"
              ],
      test_suite="abel.tests.run_cli"
     )


@@ -84,16 +84,104 @@
 tension%0A
+from distutils.errors import CCompilerError, DistutilsExecError, DistutilsPlatformError%0A
 import n
@@ -191,16 +191,26 @@
 y as np%0A
+%0Atry:%0A    
 from Cyt
@@ -244,126 +244,474 @@
 ext%0A
-from distutils.errors import CCompilerError, DistutilsExecError, DistutilsPlatformError%0Aimport Cython.Compiler.Options
+    import Cython.Compiler.Options%0A    Cython.Compiler.Options.annotate = False%0A    _cython_installed = True%0Aexcept ImportError:%0A    _cython_installed = False%0A    build_ext = object # just avoid a syntax error in TryBuildExt, this is not used anyway%0A    print('='*80)%0A    print('Warning: Cython extensions will not be built as Cython is not installed!%5Cn'%5C%0A          '         This means that the abel.direct implementation will not be available.')%0A    print('='*80)%0A
 %0A%0A%0A%0A
@@ -1142,51 +1142,8 @@
 )%0A%0A%0A
-%0ACython.Compiler.Options.annotate = False%0A%0A
 if s
@@ -2513,16 +2513,225 @@
 %0A    %5D%0A%0A
+if _cython_installed:%0A    setup_args = %7B'cmdclass': %7B'build_ext': TryBuildExt%7D,%0A                  'include_dirs': %5B np.get_include() %5D,%0A                  'ext_modules': ext_modules%7D%0Aelse:%0A    setup_args = %7B%7D%0A%0A
 %0Asetup(n
@@ -2947,125 +2947,8 @@
 %5D%7D,%0A
-      cmdclass= %7B'build_ext': TryBuildExt%7D,%0A      ext_modules= ext_modules,%0A      include_dirs=%5B np.get_include() %5D,%0A
     
@@ -3069,39 +3069,8 @@
 4%22,%0A
-              %22cython %3E= 0.22%22%0A
     
@@ -3119,16 +3119,36 @@
 run_cli%22
+,%0A      **setup_args
 %0A     )%0A

Make Cython an optional dependency