Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if self.app.config.getboolean('WINDOW', 'animate') and self.app.capture_nbr > 1:
with timeit("Asyncronously generate pictures for animation"):
for capture in captures:
maker = get_picture_maker((capture,), self.app.config.get('PICTURE', 'orientation'), force_pil=True)
_setup_maker(maker)
self.app.makers_pool.add(maker)
def validate_transition(self, events):
if self.app.printer.is_installed() and self.app.config.getfloat('PRINTER', 'printer_delay') > 0 \
and not self.app.printer_unavailable:
return 'print'
else:
return 'finish' # Can not print
class StatePrint(State):
def __init__(self):
State.__init__(self, 'print')
self.timer = PoolingTimer(self.app.config.getfloat('PRINTER', 'printer_delay'))
self.printed = False
def entry_actions(self):
self.printed = False
with timeit("Display the final picture"):
self.app.window.set_print_number(len(self.app.printer.get_all_tasks()), self.app.printer_unavailable)
self.app.window.show_print(self.app.previous_picture)
self.app.led_print.blink()
# Reset timeout in case of settings changed
self.timer.timeout = self.app.config.getfloat('PRINTER', 'printer_delay')
self.count += 1
if self.app.config.getboolean('WINDOW', 'preview_stop_on_capture') and self.count < self.app.capture_nbr:
# Restart preview only if other captures needed
self.app.camera.preview(self.app.window)
def exit_actions(self):
self.app.camera.stop_preview()
self.app.led_preview.switch_off()
def validate_transition(self, events):
if self.count >= self.app.capture_nbr:
return 'processing'
class StateProcessing(State):
def __init__(self):
State.__init__(self, 'processing')
def entry_actions(self):
self.app.window.show_work_in_progress()
self.app.makers_pool.clear()
self.app.previous_animated = []
def do_actions(self, events):
with timeit("Creating the final picture"):
captures = self.app.camera.get_captures()
backgrounds = self.app.config.gettuple('PICTURE', 'backgrounds', ('color', 'path'), 2)
if self.app.capture_nbr == self.app.capture_choices[0]:
background = backgrounds[0]
self.app.led_capture.switch_off()
self.app.led_print.switch_off()
# Clear currently displayed image
self.app.window.show_image(None)
def validate_transition(self, events):
if self.app.find_capture_event(events):
if len(self.app.capture_choices) > 1:
return 'choose'
else:
self.app.capture_nbr = self.app.capture_choices[0]
return 'capture'
class StateChoose(State):
def __init__(self, timeout):
State.__init__(self, 'choose')
self.timer = PoolingTimer(timeout)
def entry_actions(self):
with timeit("Show picture choice (nothing selected)"):
self.app.window.set_print_number(0) # Hide printer status
self.app.window.show_choice(self.app.capture_choices)
self.app.capture_nbr = None
self.app.led_capture.blink()
self.app.led_print.blink()
self.timer.start()
def do_actions(self, events):
event = self.app.find_choice_event(events)
def entry_actions(self):
self.app.dirname = None
self.app.capture_nbr = None
self.app.nbr_duplicates = 0
self.app.previous_animated = []
self.app.camera.drop_captures() # Flush previous captures
self.app.window.show_oops()
self.timer.start()
def validate_transition(self, events):
if self.timer.is_timeout():
return 'wait'
class StateWait(State):
def __init__(self):
State.__init__(self, 'wait')
self.timer = PoolingTimer(self.app.config.getfloat('WINDOW', 'animate_delay'))
def entry_actions(self):
animated = self.app.makers_pool.get()
if self.app.config.getboolean('WINDOW', 'animate') and animated:
self.app.previous_animated = itertools.cycle(animated)
previous_picture = next(self.app.previous_animated)
self.timer.timeout = self.app.config.getfloat('WINDOW', 'animate_delay')
self.timer.start()
else:
previous_picture = self.app.previous_picture
self.app.window.show_intro(previous_picture, self.app.printer.is_installed() and
self.app.led_print.switch_off()
elif self.app.capture_nbr == self.app.capture_choices[1]:
self.app.led_print.switch_on()
self.app.led_capture.switch_off()
else:
self.app.led_print.switch_off()
self.app.led_capture.switch_off()
def validate_transition(self, events):
if self.app.capture_nbr:
return 'chosen'
elif self.timer.is_timeout():
return 'wait'
class StateChosen(State):
def __init__(self, timeout):
State.__init__(self, 'chosen')
self.timer = PoolingTimer(timeout)
def entry_actions(self):
with timeit("Show picture choice ({} captures selected)".format(self.app.capture_nbr)):
self.app.window.show_choice(self.app.capture_choices, selected=self.app.capture_nbr)
self.timer.start()
def exit_actions(self):
self.app.led_capture.switch_off()
self.app.led_print.switch_off()
def validate_transition(self, events):
if self.timer.is_timeout():
self.app.printer.print_file(self.app.previous_picture_file,
self.app.config.getint('PRINTER', 'pictures_per_page'))
time.sleep(1) # Just to let the LED switched on
self.app.nbr_duplicates += 1
self.app.led_print.blink()
self.printed = True
def validate_transition(self, events):
if self.timer.is_timeout() or self.printed:
if self.printed:
self.app.window.set_print_number(len(self.app.printer.get_all_tasks()), self.app.printer_unavailable)
return 'finish'
class StateFinish(State):
def __init__(self, timeout):
State.__init__(self, 'finish')
self.timer = PoolingTimer(timeout)
def entry_actions(self):
self.app.window.show_finished()
self.timer.start()
def validate_transition(self, events):
if self.timer.is_timeout():
return 'wait'
class PiApplication(object):
def __init__(self, application):
self.states = {}
self.failsafe_state = None
self.active_state = None
# Share the application to manage between states
State.app = application
from pibooth import diagnostic
from pibooth.utils import (LOGGER, timeit, PoolingTimer, configure_logging,
set_logging_level, print_columns_words)
from pibooth.states import StateMachine, State
from pibooth.view import PtbWindow
from pibooth.config import PiConfigParser, PiConfigMenu
from pibooth.controls import GPIO, camera
from pibooth.fonts import get_available_fonts
from pibooth.pictures import get_picture_maker
from pibooth.pictures.pool import PicturesMakersPool
from pibooth.controls.light import PtbLed
from pibooth.controls.button import BUTTON_DOWN, PtbButton
from pibooth.controls.printer import PRINTER_TASKS_UPDATED, PtbPrinter
class StateFailSafe(State):
def __init__(self, timeout):
State.__init__(self, 'failsafe')
self.timer = PoolingTimer(timeout)
def entry_actions(self):
self.app.dirname = None
self.app.capture_nbr = None
self.app.nbr_duplicates = 0
self.app.previous_animated = []
self.app.camera.drop_captures() # Flush previous captures
self.app.window.show_oops()
self.timer.start()
def validate_transition(self, events):
if self.timer.is_timeout():
def __init__(self, timeout):
State.__init__(self, 'choose')
self.timer = PoolingTimer(timeout)
def __init__(self, timeout):
State.__init__(self, 'failsafe')
self.timer = PoolingTimer(timeout)