From e296d7997ceae85ea4724955ba21f484eaad278e Mon Sep 17 00:00:00 2001 From: Balthasar Reuter Date: Tue, 17 Apr 2018 23:55:59 +0200 Subject: [PATCH] Refactored entry routines --- photobooth/Photobooth.py | 73 +------------------------------------ photobooth/__init__.py | 4 +-- photobooth/__main__.py | 4 +-- photobooth/gui/PyQt5Gui.py | 7 ++-- photobooth/main.py | 74 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 78 deletions(-) create mode 100644 photobooth/main.py diff --git a/photobooth/Photobooth.py b/photobooth/Photobooth.py index 4fd31da..757ebdd 100644 --- a/photobooth/Photobooth.py +++ b/photobooth/Photobooth.py @@ -1,19 +1,14 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from multiprocessing import Pipe, Process - from time import time, sleep, localtime, strftime -import importlib - from PIL import Image, ImageOps -from Config import Config from PictureList import PictureList from PictureDimensions import PictureDimensions -import camera, gui +import gui class Photobooth: @@ -169,69 +164,3 @@ class Photobooth: self._send.send(gui.IdleState()) - -def lookup_and_import(module_list, name, package=None): - - result = next(((mod_name, class_name) - for config_name, mod_name, class_name in module_list - if name == config_name), None) - print(result) - - if package == None: - import_module = importlib.import_module(result[0]) - else: - import_module = importlib.import_module('.' + result[0], package) - - if result[1] == None: - return import_module - else: - return getattr(import_module, result[1]) - - -def main_photobooth(config, send, recv): - - while True: - try: - Camera = lookup_and_import(camera.modules, config.get('Camera', 'module'), 'camera') - - with Camera() as cap: - photobooth = Photobooth(config, cap) - return photobooth.run(send, recv) - - except BaseException as e: - send.send( gui.ErrorState('Camera error', str(e)) ) - event = recv.recv() - if str(event) != 'ack': - print('Unknown event received: ' + str(event)) - raise RuntimeError('Unknown event received', str(event)) - - -def run(argv): - - config = Config('photobooth.cfg') - - event_recv, event_send = Pipe(duplex=False) - gui_recv, gui_send = Pipe(duplex=False) - - photobooth = Process(target=main_photobooth, args=(config, gui_send, event_recv), daemon=True) - photobooth.start() - - Gui = lookup_and_import(gui.modules, config.get('Gui', 'module'), 'gui') - return Gui(argv, config).run(event_send, gui_recv) - - -def main(argv): - - known_status_codes = { - -1: 'Initializing photobooth', - -2: 'Restarting photobooth and reloading config' - } - - status_code = -1 - - while status_code in known_status_codes: - print(known_status_codes[status_code]) - - status_code = run(argv) - - return status_code diff --git a/photobooth/__init__.py b/photobooth/__init__.py index 3c3bd52..48d5ace 100644 --- a/photobooth/__init__.py +++ b/photobooth/__init__.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import sys, Photobooth +import sys, main if __name__ == "__main__": - sys.exit(Photobooth.main(sys.argv)) + sys.exit(main.main(sys.argv)) \ No newline at end of file diff --git a/photobooth/__main__.py b/photobooth/__main__.py index 19f672a..49bce41 100644 --- a/photobooth/__main__.py +++ b/photobooth/__main__.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import sys, Photobooth +import sys, main -sys.exit(Photobooth.main(sys.argv)) +sys.exit(main.main(sys.argv)) diff --git a/photobooth/gui/PyQt5Gui.py b/photobooth/gui/PyQt5Gui.py index 59e4c35..a2b6166 100644 --- a/photobooth/gui/PyQt5Gui.py +++ b/photobooth/gui/PyQt5Gui.py @@ -117,10 +117,13 @@ class PyQt5Gui(Gui): def showError(self, title, message): - if QMessageBox.warning(self._p, title,message, QMessageBox.Ok, - QMessageBox.Ok) == QMessageBox.Ok: + reply = QMessageBox.warning(self._p, title,message, QMessageBox.Close | QMessageBox.Retry, + QMessageBox.Retry) + if reply == QMessageBox.Retry: self._transport.send('ack') self._lastState() + else: + self.close() class PyQt5Receiver(QThread): diff --git a/photobooth/main.py b/photobooth/main.py new file mode 100644 index 0000000..26bf61b --- /dev/null +++ b/photobooth/main.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from multiprocessing import Pipe, Process +import importlib + +import camera, gui +from Config import Config +from Photobooth import Photobooth + +def lookup_and_import(module_list, name, package=None): + + result = next(((mod_name, class_name) + for config_name, mod_name, class_name in module_list + if name == config_name), None) + + if package == None: + import_module = importlib.import_module(result[0]) + else: + import_module = importlib.import_module('.' + result[0], package) + + if result[1] == None: + return import_module + else: + return getattr(import_module, result[1]) + + +def main_photobooth(config, send, recv): + + while True: + try: + Camera = lookup_and_import(camera.modules, config.get('Camera', 'module'), 'camera') + + with Camera() as cap: + photobooth = Photobooth(config, cap) + return photobooth.run(send, recv) + + except BaseException as e: + send.send( gui.ErrorState('Camera error', str(e)) ) + event = recv.recv() + if str(event) != 'ack': + print('Unknown event received: ' + str(event)) + raise RuntimeError('Unknown event received', str(event)) + + +def run(argv): + + config = Config('photobooth.cfg') + + event_recv, event_send = Pipe(duplex=False) + gui_recv, gui_send = Pipe(duplex=False) + + photobooth = Process(target=main_photobooth, args=(config, gui_send, event_recv), daemon=True) + photobooth.start() + + Gui = lookup_and_import(gui.modules, config.get('Gui', 'module'), 'gui') + return Gui(argv, config).run(event_send, gui_recv) + + +def main(argv): + + known_status_codes = { + -1: 'Initializing photobooth', + -2: 'Restarting photobooth and reloading config' + } + + status_code = -1 + + while status_code in known_status_codes: + print(known_status_codes[status_code]) + + status_code = run(argv) + + return status_code