import os
import time
import shutil
import inspect
import warnings
import traceback
from functools import wraps
from contextlib import contextmanager

import amino
from amino.logging import amino_stdout_logging, Logging
from amino.test import path
from amino import Path, List


default_timeout = 20 if 'TRAVIS' in os.environ else 3


class SpecBase(Logging):

    def __init__(self) -> None:
        self._warnings = True

    def setup(self) -> None:
        if path.__base_dir__:
            shutil.rmtree(str(path.temp_path()), ignore_errors=True)
        if self._warnings:
            warnings.resetwarnings()
        amino.development = True
        amino_stdout_logging()

    def teardown(self, *a, **kw):
        warnings.simplefilter('ignore')

    def _wait(self, seconds):
        time.sleep(seconds)


class IntegrationSpecBase(SpecBase):

    def setup(self) -> None:
        os.environ['AMINO_INTEGRATION'] = '1'
        amino.integration_test = True
        SpecBase.setup(self)


def profiled(sort='time'):
    fname = 'prof'
    def dec(f):
        import cProfile
        import pstats
        @wraps(f)
        def wrap(*a, **kw):
            cProfile.runctx('f(*a, **kw)', dict(), dict(f=f, a=a, kw=kw),
                            filename=fname)
            stats = pstats.Stats(fname)
            stats.sort_stats(sort).print_stats(30)
            Path(fname).unlink()
        return wrap
    return dec


def callers(limit=20):
    stack = (List.wrap(inspect.stack())
             .filter_not(lambda a: 'amino' in a.filename))
    data = stack[:limit] / (lambda a: a[1:-2] + tuple(a[-2]))
    return ''.join(traceback.format_list(data))


def timed(f):
    @wraps(f)
    def wrap(*a, **kw):
        import time
        start = time.time()
        v = f(*a, **kw)
        from amino.logging import log
        log.info('{}: {:.3}'.format(f.__name__, time.time() - start))
        return v
    return wrap


@contextmanager
def timer(name='timer'):
    import time
    start = time.time()
    v = yield
    from ribosome.logging import log
    log.info('{}: {}'.format(name, time.time() - start))
    return v

__all__ = ('SpecBase', 'profiled', 'timed', 'timer', 'IntegrationSpecBase')

@@ -699,18 +699,8 @@
 self
-, *a, **kw
 ):%0A 
@@ -858,32 +858,61 @@
 (self) -%3E None:%0A
+        SpecBase.setup(self)%0A
         os.envir
@@ -970,32 +970,57 @@
 tion_test = True
+%0A%0A    def teardown(self):
 %0A        SpecBas
@@ -1017,29 +1017,32 @@
    SpecBase.
-setup
+teardown
 (self)%0A%0A%0Adef

fix super calls on spec classes