# coding: utf-8
from __future__ import absolute_import, division, print_function

import re
import sys
from collections import defaultdict

from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand  # noqa


INIT_FILE = 'yourls/__init__.py'
init_data = open(INIT_FILE).read()

metadata = dict(re.findall("__([a-z]+)__ = '([^']+)'", init_data))

AUTHOR_EMAIL = metadata['author']
VERSION = metadata['version']
LICENSE = metadata['license']
DESCRIPTION = metadata['description']

AUTHOR, EMAIL = re.match(r'(.*) <(.*)>', AUTHOR_EMAIL).groups()

requires = [
    'click',
    'logbook>=0.10.0',
    'represent>=1.4.0',
    'requests',
    'six',
]


def add_to_extras(extras_require, dest, source):
    """Add dependencies from `source` extra to `dest` extra, handling
    conditional dependencies.
    """
    for key, deps in list(extras_require.items()):
        extra, _, condition = key.partition(':')
        if extra == source:
            if condition:
                extras_require[dest + ':' + condition] |= deps
            else:
                extras_require[dest] |= deps

extras_require = defaultdict(set)

extras_require['test'] = [
    'pytest>=2.7.3',
    'responses',
]

extras_require['dev'] = [
    'coverage',
    'doc8',
    'flake8',
    'flake8-coding',
    'flake8-future-import',
    'pep8-naming',
    'plumbum',
    'pyenchant',
    'shovel',
    'sphinx',
    'sphinx_rtd_theme',
    'sphinxcontrib-spelling',
    'tox',
    'watchdog',
]

extras_require['test:python_version<"3.3"'] = ['mock']

add_to_extras(extras_require, 'dev', 'test')

extras_require = dict(extras_require)


class PyTest(TestCommand):
    user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]

    def initialize_options(self):
        TestCommand.initialize_options(self)
        self.pytest_args = []

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        # import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(self.pytest_args)
        sys.exit(errno)


setup(
    name='yourls',
    version=VERSION,
    description=DESCRIPTION,
    long_description=open('README.rst').read(),
    author=AUTHOR,
    author_email=EMAIL,
    url='https://github.com/razerm/yourls-python',
    packages=find_packages(exclude=['tests']),
    cmdclass={'test': PyTest},
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
    ],
    license=LICENSE,
    install_requires=requires,
    extras_require=extras_require,
    entry_points={
        'console_scripts': [
            'yourls=yourls.__main__:cli'
        ]
    })

@@ -686,446 +686,8 @@
 %5D%0A%0A%0A
-def add_to_extras(extras_require, dest, source):%0A    %22%22%22Add dependencies from %60source%60 extra to %60dest%60 extra, handling%0A    conditional dependencies.%0A    %22%22%22%0A    for key, deps in list(extras_require.items()):%0A        extra, _, condition = key.partition(':')%0A        if extra == source:%0A            if condition:%0A                extras_require%5Bdest + ':' + condition%5D %7C= deps%0A            else:%0A                extras_require%5Bdest%5D %7C= deps%0A%0A
 extr
@@ -1062,16 +1062,54 @@
 chdog',%0A
+    'pytest%3E=2.7.3',%0A    'responses',%0A
 %5D%0A%0Aextra
@@ -1162,23 +1162,8 @@
 k'%5D%0A
-%0Aadd_to_extras(
 extr
@@ -1176,24 +1176,47 @@
 uire
-, 
+%5B
 'dev
-', 'test')
+:python_version%3C%223.3%22'%5D = %5B'mock'%5D
 %0A%0Aex

Remove set mutation