Added command line parameter '--debug' to enable debug output and moved parsing of arguments to main()

This commit is contained in:
Balthasar Reuter
2018-07-30 10:13:21 +02:00
parent a5f35e20a4
commit cafa0749be
2 changed files with 44 additions and 25 deletions

View File

@@ -17,7 +17,6 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import logging import logging
import os import os
@@ -41,39 +40,25 @@ from . import Worker
class PyQt5Gui(GuiSkeleton): class PyQt5Gui(GuiSkeleton):
def __init__(self, argv, config, communicator): def __init__(self, argv, config, comm):
super().__init__(communicator) super().__init__(comm)
self._cfg = config self._cfg = config
is_start, unparsed_args = self._parseArgs() self._initUI(argv)
self._initUI(argv[:1] + unparsed_args)
self._initReceiver() self._initReceiver()
self._initWorker() self._initWorker()
self._picture = None self._picture = None
self._postprocess = GuiPostprocessor(self._cfg) self._postprocess = GuiPostprocessor(self._cfg)
if is_start:
self._comm.send(Workers.MASTER, GuiEvent('start'))
def run(self): def run(self):
exit_code = self._app.exec_() exit_code = self._app.exec_()
self._gui = None self._gui = None
return exit_code return exit_code
def _parseArgs(self):
# Add parameter for direct startup
parser = argparse.ArgumentParser()
parser.add_argument('--run', action='store_true',
help='omit welcome screen and run photobooth')
parsed_args, unparsed_args = parser.parse_known_args()
return (parsed_args.run, unparsed_args)
def _initUI(self, argv): def _initUI(self, argv):
self._disableTrigger() self._disableTrigger()

View File

@@ -24,6 +24,7 @@ try:
except DistributionNotFound: except DistributionNotFound:
__version__ = 'unknown' __version__ = 'unknown'
import argparse
import gettext import gettext
import logging import logging
import logging.handlers import logging.handlers
@@ -53,6 +54,8 @@ class CameraProcess(mp.Process):
def run(self): def run(self):
logging.info('Start CameraProcess')
CameraModule = lookup_and_import( CameraModule = lookup_and_import(
camera.modules, self._cfg.get('Camera', 'module'), 'camera') camera.modules, self._cfg.get('Camera', 'module'), 'camera')
cap = camera.Camera(self._cfg, self._comm, CameraModule) cap = camera.Camera(self._cfg, self._comm, CameraModule)
@@ -64,22 +67,27 @@ class CameraProcess(mp.Process):
except Exception as e: except Exception as e:
self._comm.send(Workers.MASTER, ErrorEvent('Camera', str(e))) self._comm.send(Workers.MASTER, ErrorEvent('Camera', str(e)))
logging.debug('Exit CameraProcess')
class GuiProcess(mp.Process): class GuiProcess(mp.Process):
def __init__(self, argv, config, communicator): def __init__(self, argv, config, comm):
super().__init__() super().__init__()
self._argv = argv self._argv = argv
self._cfg = config self._cfg = config
self._comm = communicator self._comm = comm
def run(self): def run(self):
logging.debug('Start GuiProcess')
Gui = lookup_and_import(gui.modules, self._cfg.get('Gui', 'module'), Gui = lookup_and_import(gui.modules, self._cfg.get('Gui', 'module'),
'gui') 'gui')
return Gui(self._argv, self._cfg, self._comm).run() retval = Gui(self._argv, self._cfg, self._comm).run()
logging.debug('Exit GuiProcess')
return retval
class WorkerProcess(mp.Process): class WorkerProcess(mp.Process):
@@ -94,6 +102,8 @@ class WorkerProcess(mp.Process):
def run(self): def run(self):
logging.debug('Start WorkerProcess')
while True: while True:
try: try:
if Worker(self._cfg, self._comm).run(): if Worker(self._cfg, self._comm).run():
@@ -101,6 +111,8 @@ class WorkerProcess(mp.Process):
except Exception as e: except Exception as e:
self._comm.send(Workers.MASTER, ErrorEvent('Worker', str(e))) self._comm.send(Workers.MASTER, ErrorEvent('Worker', str(e)))
logging.debug('Exit WorkerProcess')
class GpioProcess(mp.Process): class GpioProcess(mp.Process):
@@ -114,6 +126,8 @@ class GpioProcess(mp.Process):
def run(self): def run(self):
logging.debug('Start GpioProcess')
while True: while True:
try: try:
if Gpio(self._cfg, self._comm).run(): if Gpio(self._cfg, self._comm).run():
@@ -121,8 +135,21 @@ class GpioProcess(mp.Process):
except Exception as e: except Exception as e:
self._comm.send(Workers.MASTER, ErrorEvent('Gpio', str(e))) self._comm.send(Workers.MASTER, ErrorEvent('Gpio', str(e)))
logging.debug('Exit GpioProcess')
def run(argv):
def parseArgs(argv):
# Add parameter for direct startup
parser = argparse.ArgumentParser()
parser.add_argument('--run', action='store_true',
help='omit welcome screen and run photobooth')
parser.add_argument('--debug', action='store_true',
help='enable additional debug output')
return parser.parse_known_args()
def run(argv, is_run):
logging.info('Photobooth version: %s', __version__) logging.info('Photobooth version: %s', __version__)
@@ -130,7 +157,7 @@ def run(argv):
config = Config('photobooth.cfg') config = Config('photobooth.cfg')
comm = Communicator() comm = Communicator()
context = Context(comm) context = Context(comm, is_run)
# Initialize processes: We use five processes here: # Initialize processes: We use five processes here:
# 1. Master that collects events and distributes state changes # 1. Master that collects events and distributes state changes
@@ -161,8 +188,15 @@ def run(argv):
def main(argv): def main(argv):
# Parse command line arguments
parsed_args, unparsed_args = parseArgs(argv)
argv = argv[:1] + unparsed_args
# Setup log level and format # Setup log level and format
log_level = logging.INFO if parsed_args.debug:
log_level = logging.DEBUG
else:
log_level = logging.INFO
formatter = logging.Formatter( formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
@@ -190,7 +224,7 @@ def main(argv):
while status_code in known_status_codes: while status_code in known_status_codes:
logging.info(known_status_codes[status_code]) logging.info(known_status_codes[status_code])
status_code = run(argv) status_code = run(argv, parsed_args.run)
logging.info('Exiting photobooth with status code %d', status_code) logging.info('Exiting photobooth with status code %d', status_code)