Refactored entry routines
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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):
|
||||
|
||||
74
photobooth/main.py
Normal file
74
photobooth/main.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user