import re
import sys
from distutils.cmd import Command
from setuptools import setup, find_packages


class ApiDocs(Command):
    """
    A custom command that calls sphinx-apidoc
    see: http://www.sphinx-doc.org/en/latest/man/sphinx-apidoc.html
    """
    description = 'builds the api documentation using sphinx-apidoc'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        import sphinx
        from sphinx.apidoc import main

        command = [
            None,  # in Sphinx < 1.7.0 the first command-line argument was parsed, in 1.7.0 it became argv[1:]
            '--force',  # overwrite existing files
            '--module-first',  # put module documentation before submodule documentation
            '--separate',  # put documentation for each module on its own page
            '-o', './docs/_autosummary',  # where to save the output files
            'msl',  # the path to the Python package to document
        ]

        if sphinx.version_info[:2] >= (1, 7):
            command.pop(0)

        main(command)
        sys.exit(0)


class BuildDocs(Command):
    """
    A custom command that calls sphinx-build
    see: http://www.sphinx-doc.org/en/latest/man/sphinx-build.html
    """
    description = 'builds the documentation using sphinx-build'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        import sphinx

        command = [
            None,  # in Sphinx < 1.7.0 the first command-line argument was parsed, in 1.7.0 it became argv[1:]
            '-b', 'html',  # the builder to use, e.g., create a HTML version of the documentation
            '-a',  # generate output for all files
            '-E',  # ignore cached files, forces to re-read all source files from disk
            'docs',  # the source directory where the documentation files are located
            './docs/_build/html',  # where to save the output files
        ]

        if sphinx.version_info[:2] < (1, 7):
            from sphinx import build_main  # Sphinx also changed the location of build_main
        else:
            from sphinx.cmd.build import build_main
            command.pop(0)

        build_main(command)
        sys.exit(0)


def read(filename):
    with open(filename) as fp:
        text = fp.read()
    return text


def fetch_init(key):
    # open the __init__.py file to determine the value instead of importing the package to get the value
    init_text = read('msl/qt/__init__.py')
    return re.compile(r'{}\s*=\s*(.*)'.format(key)).search(init_text).group(1)[1:-1]


testing = {'test', 'tests', 'pytest'}.intersection(sys.argv)
pytest_runner = ['pytest-runner'] if testing else []

needs_sphinx = {'doc', 'docs', 'apidoc', 'apidocs', 'build_sphinx'}.intersection(sys.argv)
sphinx = ['sphinx', 'sphinx_rtd_theme'] if needs_sphinx else []

install_requires = [
    'pyqt5;python_version>="3.5"',
    'pythonnet;platform_system=="Windows"',
]

setup(
    name='msl-qt',
    version=fetch_init('__version__'),
    author=fetch_init('__author__'),
    author_email='joseph.borbely@measurement.govt.nz',
    url='https://github.com/MSLNZ/msl-qt',
    description='Custom Qt components for the user interface',
    long_description=read('README.rst'),
    license='MIT',
    platforms='any',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Science/Research',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Topic :: Scientific/Engineering :: Physics',
        'Topic :: Software Development :: User Interfaces'
    ],  # see: https://pypi.python.org/pypi?%3Aaction=list_classifiers
    setup_requires=sphinx + pytest_runner,
    tests_require=['pytest-cov', 'pytest'],
    install_requires=install_requires if not testing else [],
    cmdclass={'docs': BuildDocs, 'apidocs': ApiDocs},
    packages=find_packages(include=('msl*',)),
    include_package_data=True,
)

@@ -177,32 +177,33 @@
 oc%0A    see: http
+s
 ://www.sphinx-do
@@ -458,70 +458,8 @@
 f):%0A
-        import sphinx%0A        from sphinx.apidoc import main%0A%0A
     
@@ -947,32 +947,54 @@
 ment%0A        %5D%0A%0A
+        import sphinx%0A
         if sphin
@@ -1011,15 +1011,10 @@
 info
-%5B:2%5D %3E=
+ %3C
  (1,
@@ -1010,32 +1010,187 @@
 _info %3C (1, 7):%0A
+            from sphinx.apidoc import main%0A        else:%0A            from sphinx.ext.apidoc import main  # Sphinx also changed the location of apidoc.main%0A
             comm
@@ -1337,16 +1337,17 @@
 ee: http
+s
 ://www.s
@@ -2189,12 +2189,8 @@
 info
-%5B:2%5D
  %3C (
@@ -2241,58 +2241,8 @@
 main
-  # Sphinx also changed the location of build_main
 %0A   
@@ -2295,32 +2295,82 @@
 mport build_main
+  # Sphinx also changed the location of build_main
 %0A            com
@@ -3057,108 +3057,197 @@
 %5B%5D%0A%0A
-install_requires = %5B%0A    'pyqt5;python_version%3E=%223.5%22',%0A    'pythonnet;platform_system==%22Windows%22',%0A
+# pypi only has wheels for PyQt5 for Python 3.5+%0A# otherwise assume that the user already has some version of PyQt installed%0Ainstall_requires = %5B%5D if sys.version_info %3C (3, 5) else %5B'pyqt5'
 %5D%0A%0As
@@ -3745,24 +3745,71 @@
 T License',%0A
+        'Programming Language :: Python :: 2',%0A
         'Pro
@@ -3845,16 +3845,63 @@
 : 2.7',%0A
+        'Programming Language :: Python :: 3',%0A
         
@@ -4039,16 +4039,65 @@
 : 3.6',%0A
+        'Programming Language :: Python :: 3.7',%0A
         
@@ -4211,72 +4211,8 @@
   %5D,
-  # see: https://pypi.python.org/pypi?%253Aaction=list_classifiers
 %0A   
@@ -4336,31 +4336,8 @@
 ires
- if not testing else %5B%5D
 ,%0A  

update classifiers and install_requires