From ba9ea93a0f52178d24a606fddc2acc5dc85b7ff2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 19 Jun 2016 01:13:41 +0200 Subject: [PATCH] enhancing locks --- helpers/__init__.py | 8 +++----- helpers/action.py | 5 ++++- helpers/key.py | 31 +++++++++++++++++++++++++++---- helpers/lock.py | 15 +++++++++++++++ music_sampler.py | 20 +++++++++----------- 5 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 helpers/lock.py diff --git a/helpers/__init__.py b/helpers/__init__.py index 6935342..b1723ee 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -1,12 +1,10 @@ # -*- coding: utf-8 -*- -import threading from .music_file import * from .mapping import * - -draw_lock = threading.RLock() +from .lock import * +import yaml def parse_config(mapping): - import yaml stream = open("config.yml", "r") config = yaml.load(stream) stream.close() @@ -14,7 +12,7 @@ def parse_config(mapping): aliases = config['aliases'] seen_files = {} - file_lock = threading.RLock() + file_lock = Lock("file") for mapped_key in config['keys']: key = mapping.find_by_unicode(mapped_key) diff --git a/helpers/action.py b/helpers/action.py index 8a69fae..97873a0 100644 --- a/helpers/action.py +++ b/helpers/action.py @@ -28,9 +28,12 @@ class Action: return True def run(self): - print(getattr(self, self.action + "_print")(**self.arguments)) + print(self.description()) return getattr(self, self.action)(**self.arguments) + def description(self): + return getattr(self, self.action + "_print")(**self.arguments) + def command(self, command = "", **kwargs): pass diff --git a/helpers/key.py b/helpers/key.py index 2e4a313..6997e70 100644 --- a/helpers/key.py +++ b/helpers/key.py @@ -112,9 +112,32 @@ class Key: self.mapping.finished_running(self, start_time) - def list_actions(self, surface): - # FIXME: Todo - print("bouh", self.key_sym) - surface.fill((255, 0, 0)) + def list_actions(self, screen): + action_descriptions = [action.description() for action in self.actions] + print("actions linked to key {}:".format(self.key_sym)) + print("\t" + "\n\t".join(action_descriptions)) + self.draw_lock.acquire() + surface = pygame.Surface((800, 250)).convert() + surface.fill((250, 250, 250)) + if getattr(sys, 'frozen', False): + police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14) + else: + police = pygame.font.Font("Ubuntu-Regular.ttf", 14) + + offset = 0 + police.set_bold(True) + text = police.render("actions linked to key {}:".format(self.key_sym), True, (0,0,0)) + surface.blit(text, (0, offset)) + offset += police.get_linesize() + + police.set_bold(False) + for description in action_descriptions: + text = police.render(description, True, (0,0,0)) + surface.blit(text, (0, offset)) + offset += police.get_linesize() + + screen.blit(surface, (10, 330)) + pygame.display.flip() + self.draw_lock.release() diff --git a/helpers/lock.py b/helpers/lock.py new file mode 100644 index 0000000..dff8b1f --- /dev/null +++ b/helpers/lock.py @@ -0,0 +1,15 @@ +import threading + +class Lock: + def __init__(self, lock_type): + self.type = lock_type + self.lock = threading.RLock() + + def acquire(self, *args, **kwargs): + #print("acquiring lock for {}".format(self.type)) + self.lock.acquire(*args, **kwargs) + + def release(self, *args, **kwargs): + #print("releasing lock for {}".format(self.type)) + self.lock.release(*args, **kwargs) + diff --git a/music_sampler.py b/music_sampler.py index 9f14d56..fd03009 100644 --- a/music_sampler.py +++ b/music_sampler.py @@ -7,23 +7,19 @@ pygame.mixer.pre_init(frequency = 44100) pygame.init() size = width, height = 1024, 600 - -helpers.draw_lock.acquire() screen = pygame.display.set_mode(size) -mapping = helpers.Mapping(screen, helpers.draw_lock) +screen.fill((250, 250, 250)) + +draw_lock = helpers.Lock("draw") -action_surface = pygame.Surface((600, 250)).convert() -action_surface.fill((0,0,0)) +mapping = helpers.Mapping(screen, draw_lock) helpers.parse_config(mapping) -helpers.draw_lock.release() mapping.draw() -helpers.draw_lock.acquire() -screen.blit(action_surface, (10, 330)) - +draw_lock.acquire() pygame.display.flip() -helpers.draw_lock.release() +draw_lock.release() contexts = [ 'normal' @@ -54,7 +50,9 @@ while 1: elif event.type == pygame.MOUSEBUTTONUP: key = mapping.find_by_collidepoint(pygame.mouse.get_pos()) if key is not None: - key.list_actions(action_surface) + threading.Thread(target=key.list_actions, args = [screen]).start() + draw_lock.acquire() pygame.display.flip() + draw_lock.release() -- 2.41.0