#!/usr/bin/env python3

"""
Template by pypi-mobans
"""

import os
import sys
import codecs
import locale
import platform
from shutil import rmtree

from distutils.core import setup, Extension


# Work around mbcs bug in distutils.
# http://bugs.python.org/issue10945
# This work around is only if a project supports Python < 3.4

# Work around for locale not being set
try:
    lc = locale.getlocale()
    pf = platform.system()
    if pf != "Windows" and lc == (None, None):
        locale.setlocale(locale.LC_ALL, "C.UTF-8")
except (ValueError, UnicodeError, locale.Error):
    locale.setlocale(locale.LC_ALL, "en_US.UTF-8")

NAME = "libxlsxwpy"
AUTHOR = "chfw"
VERSION = ""
EMAIL = "info@pyexcel.org"
LICENSE = "New BSD"
DESCRIPTION = (
    "A plain python wrapper for libxlsxwriter, a C library."
)
URL = "https://github.com/pyexcel/libxlsxwpy"
DOWNLOAD_URL = "%s/archive/0.0.1.tar.gz" % URL
FILES = ["README.rst", "CHANGELOG.rst"]
KEYWORDS = [
    "python",
    'xlsx'
]

CLASSIFIERS = [
    "Topic :: Software Development :: Libraries",
    "Programming Language :: Python",
    "Intended Audience :: Developers",

    "Programming Language :: Python :: 3 :: Only",



    "Programming Language :: Python :: 3.6",
    "Programming Language :: Python :: 3.7",
    "Programming Language :: Python :: 3.8",

    'Development Status :: 3 - Alpha',
    'Programming Language :: Python :: 2.6',
    'Programming Language :: Python :: 2.7',
    'Programming Language :: Python :: 3.3',
    'Programming Language :: Python :: 3.4',
    'Programming Language :: Python :: Implementation :: PyPy'
]

PYTHON_REQUIRES = ">=3.6"

INSTALL_REQUIRES = [
    "xlsxwriter",
]
SETUP_COMMANDS = {}

PYMODULE = Extension(
    'libxlsxwpy',
    sources=[
        "pymodule.c",
        "book.c",
        "sheet.c",
    ],
    libraries=INSTALL_REQUIRES
)
# You do not need to read beyond this line
PUBLISH_COMMAND = "{0} setup.py sdist bdist_wheel upload -r pypi".format(sys.executable)
GS_COMMAND = ("gs libxlsxwpy v0.0.1 " +
              "Find 0.0.1 in changelog for more details")
NO_GS_MESSAGE = ("Automatic github release is disabled. " +
                 "Please install gease to enable it.")
UPLOAD_FAILED_MSG = (
    'Upload failed. please run "%s" yourself.' % PUBLISH_COMMAND)
HERE = os.path.abspath(os.path.dirname(__file__))


class PublishCommand(Command):
    """Support setup.py upload."""

    description = "Build and publish the package on github and pypi"
    user_options = []

    @staticmethod
    def status(s):
        """Prints things in bold."""
        print("\033[1m{0}\033[0m".format(s))

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        try:
            self.status("Removing previous builds...")
            rmtree(os.path.join(HERE, "dist"))
            rmtree(os.path.join(HERE, "build"))
            rmtree(os.path.join(HERE, "libxlsxwpy.egg-info"))
        except OSError:
            pass

        self.status("Building Source and Wheel (universal) distribution...")
        run_status = True
        if has_gease():
            run_status = os.system(GS_COMMAND) == 0
        else:
            self.status(NO_GS_MESSAGE)
        if run_status:
            if os.system(PUBLISH_COMMAND) != 0:
                self.status(UPLOAD_FAILED_MSG)

        sys.exit()


SETUP_COMMANDS.update({
    "publish": PublishCommand
})


def has_gease():
    """
    test if github release command is installed

    visit http://github.com/moremoban/gease for more info
    """
    try:
        import gease  # noqa
        return True
    except ImportError:
        return False


def read_files(*files):
    """Read files into setup"""
    text = ""
    for single_file in files:
        content = read(single_file)
        text = text + content + "\n"
    return text


def read(afile):
    """Read a file into setup"""
    the_relative_file = os.path.join(HERE, afile)
    with codecs.open(the_relative_file, "r", "utf-8") as opened_file:
        content = filter_out_test_code(opened_file)
        content = "".join(list(content))
        return content


def filter_out_test_code(file_handle):
    found_test_code = False
    for line in file_handle.readlines():
        if line.startswith(".. testcode:"):
            found_test_code = True
            continue
        if found_test_code is True:
            if line.startswith("  "):
                continue
            else:
                empty_line = line.strip()
                if len(empty_line) == 0:
                    continue
                else:
                    found_test_code = False
                    yield line
        else:
            for keyword in ["|version|", "|today|"]:
                if keyword in line:
                    break
            else:
                yield line


if __name__ == "__main__":
    setup(
        test_suite="tests",
        name=NAME,
        author=AUTHOR,
        version=VERSION,
        author_email=EMAIL,
        description=DESCRIPTION,
        url=URL,
        download_url=DOWNLOAD_URL,
        long_description=read_files(*FILES),
        license=LICENSE,
        keywords=KEYWORDS,
        ext_modules=[PYMODULE],
        classifiers=CLASSIFIERS,
        cmdclass=SETUP_COMMANDS
    )

@@ -1968,1447 +1968,56 @@
 le)%0A
-GS_COMMAND = (%22gs libxlsxwpy v0.0.1 %22 +%0A              %22Find 0.0.1 in changelog for more details%22)%0ANO_GS_MESSAGE = (%22Automatic github release is disabled. %22 +%0A                 %22Please install gease to enable it.%22)%0AUPLOAD_FAILED_MSG = (%0A    'Upload failed. please run %22%25s%22 yourself.' %25 PUBLISH_COMMAND)%0AHERE = os.path.abspath(os.path.dirname(__file__))%0A%0A%0Aclass PublishCommand(Command):%0A    %22%22%22Support setup.py upload.%22%22%22%0A%0A    description = %22Build and publish the package on github and pypi%22%0A    user_options = %5B%5D%0A%0A    @staticmethod%0A    def status(s):%0A        %22%22%22Prints things in bold.%22%22%22%0A        print(%22%5C033%5B1m%7B0%7D%5C033%5B0m%22.format(s))%0A%0A    def initialize_options(self):%0A        pass%0A%0A    def finalize_options(self):%0A        pass%0A%0A    def run(self):%0A        try:%0A            self.status(%22Removing previous builds...%22)%0A            rmtree(os.path.join(HERE, %22dist%22))%0A            rmtree(os.path.join(HERE, %22build%22))%0A            rmtree(os.path.join(HERE, %22libxlsxwpy.egg-info%22))%0A        except OSError:%0A            pass%0A%0A        self.status(%22Building Source and Wheel (universal) distribution...%22)%0A        run_status = True%0A        if has_gease():%0A            run_status = os.system(GS_COMMAND) == 0%0A        else:%0A            self.status(NO_GS_MESSAGE)%0A        if run_status:%0A            if os.system(PUBLISH_COMMAND) != 0:%0A                self.status(UPLOAD_FAILED_MSG)%0A%0A        sys.exit()%0A%0A%0ASETUP_COMMANDS.update(%7B%0A    %22publish%22: PublishCommand%0A%7D
+HERE = os.path.abspath(os.path.dirname(__file__)
 )%0A%0A%0A
@@ -3862,40 +3862,8 @@
 RS,%0A
-        cmdclass=SETUP_COMMANDS%0A
     

update setup.py