# coding=utf-8
"""
Internal tools for NimLime development & testing.
"""
from pprint import pprint

import sublime

try:
    from cProfile import Profile
except ImportError:
    from profile import Profile
from functools import wraps
from pstats import Stats

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

debug_on = False
if debug_on:
    sublime.message_dialog("NimLime running in debug mode.")


# Debug printer
def print_debug(*args, **kwargs):
    """
    Print when debugging.
    :type args: Any
    :type kwargs: Any
    """
    if debug_on:
        pprint(*args, **kwargs)

# Profiling functions
profiler = Profile()
profiler_running = False


def profile_func(func):
    """
    Decorator which profiles a single function.
    Call print_profile_data to print the collected data.
    :type func: Callable
    :rtype: Callable
    """

    @wraps(func)
    def _profile_wrapper(*args, **kwargs):
        global profiler_running
        if not profiler_running:
            profiler_running = True
            try:
                profiler.enable()
                return func(*args, **kwargs)
            finally:
                profiler.disable()
                profiler_running = False

    return _profile_wrapper


def print_profile_data():
    """
    Print the collected profile data.
    """
    stream = StringIO()
    statistics = Stats(profiler, stream=stream)
    statistics.sort_stats('cumulative')
    statistics.print_stats()
    print(stream.getvalue())

[((658, 667), 'profile.Profile', 'Profile', ([], {}), '()\n', (665, 667), False, 'from profile import Profile\n'), ((382, 438), 'sublime.message_dialog', 'sublime.message_dialog', (['"""NimLime running in debug mode."""'], {}), "('NimLime running in debug mode.')\n", (404, 438), False, 'import sublime\n'), ((892, 903), 'functools.wraps', 'wraps', (['func'], {}), '(func)\n', (897, 903), False, 'from functools import wraps\n'), ((1365, 1375), 'io.StringIO', 'StringIO', ([], {}), '()\n', (1373, 1375), False, 'from io import StringIO\n'), ((1393, 1423), 'pstats.Stats', 'Stats', (['profiler'], {'stream': 'stream'}), '(profiler, stream=stream)\n', (1398, 1423), False, 'from pstats import Stats\n'), ((600, 623), 'pprint.pprint', 'pprint', (['*args'], {}), '(*args, **kwargs)\n', (606, 623), False, 'from pprint import pprint\n')]