<commit_before>import locale
import pygame
import os.path

HAS_LIRC = True
try:
    import lirc
except ImportError:
    HAS_LIRC = False

from modes.weather import WeatherMode
from modes.ball import BallMode
from modes.clock import ClockMode

script_path = os.path.dirname(os.path.realpath(__file__))
assets_path = os.path.join(script_path, "assets")

class MainLoop(object):

    def __init__(self, fullscreen=False):
        self.bg_color = (0, 0, 0)
        self.clock = pygame.time.Clock()

        if fullscreen:
            self.screen = pygame.display.set_mode((800, 600), pygame.FULLSCREEN)
        else:
            self.screen = pygame.display.set_mode((800, 600))

        self.modes = [WeatherMode(assets_path),
                      ClockMode(assets_path),
                      BallMode(assets_path)]
        self.current_mode = 0

        self.running = True

    def lirc2pygame(self, keys):
        k = keys[0]
        if k == "KEY_POWER":
            return pygame.event.Event(pygame.QUIT)
        if k == "KEY_LEFT":
            return pygame.event.Event(pygame.QUIT, key=pygame.K_LEFT)
        if k == "KEY_RIGHT":
            return pygame.event.Event(pygame.QUIT, key=pygame.K_RIGHT)
        if k == "KEY_UP":
            return pygame.event.Event(pygame.QUIT, key=pygame.K_UP)
        if k == "KEY_DOWN":
            return pygame.event.Event(pygame.QUIT, key=pygame.K_DOWN)

    def process_event(self, event):
        if event.type == pygame.QUIT:
            self.running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.running = False
            elif event.key == pygame.K_RIGHT:
                self.current_mode = (self.current_mode + 1) % len(self.modes)
            elif event.key == pygame.K_LEFT:
                self.current_mode = (self.current_mode - 1) % len(self.modes)


    def main_loop(self, lirc_socket=None):
        # Generate an artificial event every 10 seconds to perform
        # updates if a mode does not require repainting
        pygame.time.set_timer(pygame.USEREVENT, 10000)
        pygame.event.set_blocked(pygame.MOUSEMOTION)

        clear = self.screen.copy()
        clear.fill((0, 0, 0))

        while self.running:
            mode = self.modes[self.current_mode]

            for event in pygame.event.get():
                self.process_event(event)
            if lirc_socket:
                keys = lirc.nextcode()
                if keys:
                    process_event(self.lirc2pygame(keys))

            self.screen.blit(clear, (0, 0))

            mode.loop(self.screen)

            pygame.display.flip()
            self.clock.tick(mode.preferred_fps())
            pygame.display.set_caption("fps: %.2f" % self.clock.get_fps())

        pygame.time.set_timer(pygame.USEREVENT, 0)

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--fullscreen", action="store_true")
    args = parser.parse_args()

    locale.setlocale(locale.LC_ALL, 'it_IT.utf8')

    pygame.init()
    lirc_socket = None
    if HAS_LIRC:
        lirc_socket = lirc.init("pySmartMirror",
                                config_filename=os.path.join(assets_path, "lircrc"),
                                blocking=False)
    main = MainLoop(fullscreen=args.fullscreen)
    try:
        main.main_loop(lirc_socket)
    finally:
        if HAS_LIRC:
            lirc.deinit()
        pygame.quit()
<commit_msg>Fixed lirc support<commit_after>import locale
import pygame
import os.path

HAS_LIRC = True
try:
    import lirc
except ImportError:
    HAS_LIRC = False

from modes.weather import WeatherMode
from modes.ball import BallMode
from modes.clock import ClockMode

script_path = os.path.dirname(os.path.realpath(__file__))
assets_path = os.path.join(script_path, "assets")

class MainLoop(object):

    def __init__(self, fullscreen=False):
        self.bg_color = (0, 0, 0)
        self.clock = pygame.time.Clock()

        if fullscreen:
            self.screen = pygame.display.set_mode((800, 600), pygame.FULLSCREEN)
        else:
            self.screen = pygame.display.set_mode((800, 600))

        self.modes = [WeatherMode(assets_path),
                      ClockMode(assets_path),
                      BallMode(assets_path)]
        self.current_mode = 0

        self.running = True

    def lirc2pygame(self, keys):
        k = keys[0]
        if k == "KEY_POWER":
            return pygame.event.Event(pygame.QUIT)

        # These keys are already handled by pygame!
        # if k == "KEY_LEFT":
        #     return pygame.event.Event(pygame.KEYDOWN, key=pygame.K_LEFT)
        # if k == "KEY_RIGHT":
        #     return pygame.event.Event(pygame.KEYDOWN, key=pygame.K_RIGHT)
        # if k == "KEY_UP":
        #     return pygame.event.Event(pygame.KEYDOWN, key=pygame.K_UP)
        # if k == "KEY_DOWN":
        #     return pygame.event.Event(pygame.KEYDOWN, key=pygame.K_DOWN)
        return None
        
    def process_event(self, event):
        if event.type == pygame.QUIT:
            self.running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                self.running = False
            elif event.key == pygame.K_RIGHT:
                self.current_mode = (self.current_mode + 1) % len(self.modes)
            elif event.key == pygame.K_LEFT:
                self.current_mode = (self.current_mode - 1) % len(self.modes)


    def main_loop(self, lirc_socket=None):
        # Generate an artificial event every 10 seconds to perform
        # updates if a mode does not require repainting
        pygame.time.set_timer(pygame.USEREVENT, 10000)
        pygame.event.set_blocked(pygame.MOUSEMOTION)

        clear = self.screen.copy()
        clear.fill((0, 0, 0))

        while self.running:
            mode = self.modes[self.current_mode]

            for event in pygame.event.get():
                self.process_event(event)
            if lirc_socket:
                keys = lirc.nextcode()
                if keys:
                    event = self.lirc2pygame(keys)
                    if event:
                        self.process_event(event)

            self.screen.blit(clear, (0, 0))

            mode.loop(self.screen)

            pygame.display.flip()
            self.clock.tick(mode.preferred_fps())
            pygame.display.set_caption("fps: %.2f" % self.clock.get_fps())

        pygame.time.set_timer(pygame.USEREVENT, 0)

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("-f", "--fullscreen", action="store_true")
    args = parser.parse_args()

    locale.setlocale(locale.LC_ALL, 'it_IT.utf8')

    pygame.init()
    lirc_socket = None
    if HAS_LIRC:
        lirc_socket = lirc.init("pySmartMirror",
                                config_filename=os.path.join(assets_path, "lircrc"),
                                blocking=False)
    main = MainLoop(fullscreen=args.fullscreen)
    try:
        main.main_loop(lirc_socket)
    finally:
        if HAS_LIRC:
            lirc.deinit()
        pygame.quit()

{'fraction_non_alphanumeric': 0.10829268292682927, 'fraction_numerical': 0.010034843205574913, 'mean_word_length': 2.0227464195450717, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1124068', 'n_tokens_mistral': 2390, 'n_tokens_neox': 2236, 'n_words': 441}