<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2014, Vispy Development Team.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.

"""
vispy backend for pyglet.
"""

from __future__ import division

from distutils.version import LooseVersion
from time import sleep

from ..base import (BaseApplicationBackend, BaseCanvasBackend,
                    BaseTimerBackend)
from ...util import keys
from ...util.ptime import time


# -------------------------------------------------------------------- init ---

try:
    import pyglet
    version = pyglet.version
    if LooseVersion(version) < LooseVersion('1.2'):
        help_ = ('You can install the latest pyglet using:\n    '
                 'pip install http://pyglet.googlecode.com/archive/tip.zip')
        raise ImportError('Pyglet version too old (%s), need >= 1.2\n%s'
                          % (version, help_))

    # Map native keys to vispy keys
    KEYMAP = {
        pyglet.window.key.LSHIFT: keys.SHIFT,
        pyglet.window.key.RSHIFT: keys.SHIFT,
        pyglet.window.key.LCTRL: keys.CONTROL,
        pyglet.window.key.RCTRL: keys.CONTROL,
        pyglet.window.key.LALT: keys.ALT,
        pyglet.window.key.RALT: keys.ALT,
        pyglet.window.key.LMETA: keys.META,
        pyglet.window.key.RMETA: keys.META,

        pyglet.window.key.LEFT: keys.LEFT,
        pyglet.window.key.UP: keys.UP,
        pyglet.window.key.RIGHT: keys.RIGHT,
        pyglet.window.key.DOWN: keys.DOWN,
        pyglet.window.key.PAGEUP: keys.PAGEUP,
        pyglet.window.key.PAGEDOWN: keys.PAGEDOWN,

        pyglet.window.key.INSERT: keys.INSERT,
        pyglet.window.key.DELETE: keys.DELETE,
        pyglet.window.key.HOME: keys.HOME,
        pyglet.window.key.END: keys.END,

        pyglet.window.key.ESCAPE: keys.ESCAPE,
        pyglet.window.key.BACKSPACE: keys.BACKSPACE,

        pyglet.window.key.F1: keys.F1,
        pyglet.window.key.F2: keys.F2,
        pyglet.window.key.F3: keys.F3,
        pyglet.window.key.F4: keys.F4,
        pyglet.window.key.F5: keys.F5,
        pyglet.window.key.F6: keys.F6,
        pyglet.window.key.F7: keys.F7,
        pyglet.window.key.F8: keys.F8,
        pyglet.window.key.F9: keys.F9,
        pyglet.window.key.F10: keys.F10,
        pyglet.window.key.F11: keys.F11,
        pyglet.window.key.F12: keys.F12,

        pyglet.window.key.SPACE: keys.SPACE,
        pyglet.window.key.ENTER: keys.ENTER,  # == pyglet.window.key.RETURN
        pyglet.window.key.NUM_ENTER: keys.ENTER,
        pyglet.window.key.TAB: keys.TAB,
    }

    BUTTONMAP = {pyglet.window.mouse.LEFT: 1,
                 pyglet.window.mouse.RIGHT: 2,
                 pyglet.window.mouse.MIDDLE: 3
                 }
except Exception as exp:
    available, testable, why_not, which = False, False, str(exp), None

    class _Window(object):
        pass
else:
    available, testable, why_not = True, True, None
    which = 'pyglet ' + str(pyglet.version)
    _Window = pyglet.window.Window


# -------------------------------------------------------------- capability ---

capability = dict(  # things that can be set by the backend
    title=True,
    size=True,
    position=True,
    show=True,
    vsync=True,
    resizable=True,
    decorate=True,
    fullscreen=True,
    context=True,
    multi_window=True,
    scroll=True,
    parent=False,
)


# ------------------------------------------------------- set_configuration ---

def _set_config(config):
    """Set gl configuration"""
    pyglet_config = pyglet.gl.Config()

    pyglet_config.red_size = config['red_size']
    pyglet_config.green_size = config['green_size']
    pyglet_config.blue_size = config['blue_size']
    pyglet_config.alpha_size = config['alpha_size']

    pyglet_config.accum_red_size = 0
    pyglet_config.accum_green_size = 0
    pyglet_config.accum_blue_size = 0
    pyglet_config.accum_alpha_size = 0

    pyglet_config.depth_size = config['depth_size']
    pyglet_config.stencil_size = config['stencil_size']
    pyglet_config.double_buffer = config['double_buffer']
    pyglet_config.stereo = config['stereo']
    pyglet_config.samples = config['samples']
    return pyglet_config


# ------------------------------------------------------------- application ---

class ApplicationBackend(BaseApplicationBackend):

    def __init__(self):
        BaseApplicationBackend.__init__(self)

    def _vispy_get_backend_name(self):
        return 'Pyglet'

    def _vispy_process_events(self):
        # pyglet.app.platform_event_loop.step(0.0)
        pyglet.clock.tick()
        for window in pyglet.app.windows:
            window.switch_to()
            window.dispatch_events()
            window.dispatch_event('on_draw')

    def _vispy_run(self):
        return pyglet.app.run()

    def _vispy_quit(self):
        return pyglet.app.exit()

    def _vispy_get_native_app(self):
        return pyglet.app


# ------------------------------------------------------------------ canvas ---

class CanvasBackend(_Window, BaseCanvasBackend):

    """ Pyglet backend for Canvas abstract class."""

    # args are for BaseCanvasBackend, kwargs are for us.
    def __init__(self, *args, **kwargs):
        BaseCanvasBackend.__init__(self, *args)
        title, size, position, show, vsync, resize, dec, fs, parent, context, \
            = self._process_backend_kwargs(kwargs)
        
        # Deal with context
        if not context.istaken:
            context.take('pyglet', self)
            config = context.config  # Also used further below
            _set_config(config)
        elif context.istaken == 'pyglet':
            config = None  # contexts are shared by default in Pyglet
        else:
            raise RuntimeError('Cannot share context between backends.')
        
        style = (pyglet.window.Window.WINDOW_STYLE_DEFAULT if dec else
                 pyglet.window.Window.WINDOW_STYLE_BORDERLESS)
        # We keep track of modifier keys so we can pass them to mouse_motion
        self._current_modifiers = set()
        #self._buttons_accepted = 0
        self._draw_ok = False  # whether it is ok to draw yet
        self._pending_position = None
        if fs is not False:
            screen = pyglet.window.get_platform().get_default_display()
            self._vispy_fullscreen = True
            if fs is True:
                self._vispy_screen = screen.get_default_screen()
            else:
                screen = screen.get_screens()
                if fs >= len(screen):
                    raise RuntimeError('fullscreen must be < %s'
                                       % len(screen))
                self._vispy_screen = screen[fs]
        else:
            self._vispy_fullscreen = False
            self._vispy_screen = None
        self._initialize_sent = False
        pyglet.window.Window.__init__(self, width=size[0], height=size[1],<commit_msg>app: fix pyglet after seeing travis error<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2014, Vispy Development Team.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.

"""
vispy backend for pyglet.
"""

from __future__ import division

from distutils.version import LooseVersion
from time import sleep

from ..base import (BaseApplicationBackend, BaseCanvasBackend,
                    BaseTimerBackend)
from ...util import keys
from ...util.ptime import time


# -------------------------------------------------------------------- init ---

try:
    import pyglet
    version = pyglet.version
    if LooseVersion(version) < LooseVersion('1.2'):
        help_ = ('You can install the latest pyglet using:\n    '
                 'pip install http://pyglet.googlecode.com/archive/tip.zip')
        raise ImportError('Pyglet version too old (%s), need >= 1.2\n%s'
                          % (version, help_))

    # Map native keys to vispy keys
    KEYMAP = {
        pyglet.window.key.LSHIFT: keys.SHIFT,
        pyglet.window.key.RSHIFT: keys.SHIFT,
        pyglet.window.key.LCTRL: keys.CONTROL,
        pyglet.window.key.RCTRL: keys.CONTROL,
        pyglet.window.key.LALT: keys.ALT,
        pyglet.window.key.RALT: keys.ALT,
        pyglet.window.key.LMETA: keys.META,
        pyglet.window.key.RMETA: keys.META,

        pyglet.window.key.LEFT: keys.LEFT,
        pyglet.window.key.UP: keys.UP,
        pyglet.window.key.RIGHT: keys.RIGHT,
        pyglet.window.key.DOWN: keys.DOWN,
        pyglet.window.key.PAGEUP: keys.PAGEUP,
        pyglet.window.key.PAGEDOWN: keys.PAGEDOWN,

        pyglet.window.key.INSERT: keys.INSERT,
        pyglet.window.key.DELETE: keys.DELETE,
        pyglet.window.key.HOME: keys.HOME,
        pyglet.window.key.END: keys.END,

        pyglet.window.key.ESCAPE: keys.ESCAPE,
        pyglet.window.key.BACKSPACE: keys.BACKSPACE,

        pyglet.window.key.F1: keys.F1,
        pyglet.window.key.F2: keys.F2,
        pyglet.window.key.F3: keys.F3,
        pyglet.window.key.F4: keys.F4,
        pyglet.window.key.F5: keys.F5,
        pyglet.window.key.F6: keys.F6,
        pyglet.window.key.F7: keys.F7,
        pyglet.window.key.F8: keys.F8,
        pyglet.window.key.F9: keys.F9,
        pyglet.window.key.F10: keys.F10,
        pyglet.window.key.F11: keys.F11,
        pyglet.window.key.F12: keys.F12,

        pyglet.window.key.SPACE: keys.SPACE,
        pyglet.window.key.ENTER: keys.ENTER,  # == pyglet.window.key.RETURN
        pyglet.window.key.NUM_ENTER: keys.ENTER,
        pyglet.window.key.TAB: keys.TAB,
    }

    BUTTONMAP = {pyglet.window.mouse.LEFT: 1,
                 pyglet.window.mouse.RIGHT: 2,
                 pyglet.window.mouse.MIDDLE: 3
                 }
except Exception as exp:
    available, testable, why_not, which = False, False, str(exp), None

    class _Window(object):
        pass
else:
    available, testable, why_not = True, True, None
    which = 'pyglet ' + str(pyglet.version)
    _Window = pyglet.window.Window


# -------------------------------------------------------------- capability ---

capability = dict(  # things that can be set by the backend
    title=True,
    size=True,
    position=True,
    show=True,
    vsync=True,
    resizable=True,
    decorate=True,
    fullscreen=True,
    context=True,
    multi_window=True,
    scroll=True,
    parent=False,
)


# ------------------------------------------------------- set_configuration ---

def _set_config(config):
    """Set gl configuration"""
    pyglet_config = pyglet.gl.Config()

    pyglet_config.red_size = config['red_size']
    pyglet_config.green_size = config['green_size']
    pyglet_config.blue_size = config['blue_size']
    pyglet_config.alpha_size = config['alpha_size']

    pyglet_config.accum_red_size = 0
    pyglet_config.accum_green_size = 0
    pyglet_config.accum_blue_size = 0
    pyglet_config.accum_alpha_size = 0

    pyglet_config.depth_size = config['depth_size']
    pyglet_config.stencil_size = config['stencil_size']
    pyglet_config.double_buffer = config['double_buffer']
    pyglet_config.stereo = config['stereo']
    pyglet_config.samples = config['samples']
    return pyglet_config


# ------------------------------------------------------------- application ---

class ApplicationBackend(BaseApplicationBackend):

    def __init__(self):
        BaseApplicationBackend.__init__(self)

    def _vispy_get_backend_name(self):
        return 'Pyglet'

    def _vispy_process_events(self):
        # pyglet.app.platform_event_loop.step(0.0)
        pyglet.clock.tick()
        for window in pyglet.app.windows:
            window.switch_to()
            window.dispatch_events()
            window.dispatch_event('on_draw')

    def _vispy_run(self):
        return pyglet.app.run()

    def _vispy_quit(self):
        return pyglet.app.exit()

    def _vispy_get_native_app(self):
        return pyglet.app


# ------------------------------------------------------------------ canvas ---

class CanvasBackend(_Window, BaseCanvasBackend):

    """ Pyglet backend for Canvas abstract class."""

    # args are for BaseCanvasBackend, kwargs are for us.
    def __init__(self, *args, **kwargs):
        BaseCanvasBackend.__init__(self, *args)
        title, size, position, show, vsync, resize, dec, fs, parent, context, \
            = self._process_backend_kwargs(kwargs)
        
        # Deal with context
        if not context.istaken:
            context.take('pyglet', self)
            config = _set_config(context.config)  # Also used further below
        elif context.istaken == 'pyglet':
            config = None  # contexts are shared by default in Pyglet
        else:
            raise RuntimeError('Cannot share context between backends.')
        
        style = (pyglet.window.Window.WINDOW_STYLE_DEFAULT if dec else
                 pyglet.window.Window.WINDOW_STYLE_BORDERLESS)
        # We keep track of modifier keys so we can pass them to mouse_motion
        self._current_modifiers = set()
        #self._buttons_accepted = 0
        self._draw_ok = False  # whether it is ok to draw yet
        self._pending_position = None
        if fs is not False:
            screen = pyglet.window.get_platform().get_default_display()
            self._vispy_fullscreen = True
            if fs is True:
                self._vispy_screen = screen.get_default_screen()
            else:
                screen = screen.get_screens()
                if fs >= len(screen):
                    raise RuntimeError('fullscreen must be < %s'
                                       % len(screen))
                self._vispy_screen = screen[fs]
        else:
            self._vispy_fullscreen = False
            self._vispy_screen = None
        self._initialize_sent = False
        pyglet.window.Window.__init__(self, width=size[0], height=size[1],
{'fraction_non_alphanumeric': 0.14686432672044183, 'fraction_numerical': 0.007412252016568564, 'mean_word_length': 2.0008722197993896, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5499991', 'n_tokens_mistral': 4400, 'n_tokens_neox': 4283, 'n_words': 860}