From: Calin Crisan Date: Sat, 5 Oct 2013 15:37:27 +0000 (+0300) Subject: added command line settings support X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=a1f029718a4f8031b3a4e917e0f5a94dd5fc8301;p=motioneye-debian added command line settings support --- diff --git a/motioneye.py b/motioneye.py old mode 100644 new mode 100755 index 8e45f10..995ec57 --- a/motioneye.py +++ b/motioneye.py @@ -1,8 +1,9 @@ #!/usr/bin/env python2 +import inspect import logging -import motionctl import os.path +import re import signal import sys import tornado.ioloop @@ -11,12 +12,11 @@ import settings sys.path.append(os.path.join(settings.PROJECT_PATH, 'src')) -import config -import server - def _configure_signals(): def bye_handler(signal, frame): + import motionctl + logging.info('interrupt signal received, shutting down...') # shut down the IO loop if it has been started @@ -40,7 +40,82 @@ def _configure_logging(): format='%(asctime)s: %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') +def _configure_settings(): + length = len(sys.argv) - 1 + for i in xrange(length): + arg = sys.argv[i + 1] + + if not arg.startswith('--'): + continue + + next_arg = None + if i < length - 1: + next_arg = sys.argv[i + 2] + + name = arg[2:].upper().replace('-', '_') + + if name == 'HELP': + _print_help() + sys.exit(0) + + if hasattr(settings, name): + curr_value = getattr(settings, name) + + if next_arg.lower() == 'debug': + next_arg = logging.DEBUG + + elif next_arg.lower() == 'info': + next_arg = logging.INFO + + elif next_arg.lower() == 'warn': + next_arg = logging.WARN + + elif next_arg.lower() == 'error': + next_arg = logging.ERROR + + elif next_arg.lower() == 'fatal': + next_arg = logging.FATAL + + elif next_arg.lower() == 'true': + next_arg = True + + elif next_arg.lower() == 'false': + next_arg = False + + elif isinstance(curr_value, int): + next_arg = int(next_arg) + + elif isinstance(curr_value, float): + next_arg = float(next_arg) + + setattr(settings, name, next_arg) + + else: + print('unknown command line option: ' + arg) + _print_help() + sys.exit(-1) + + +def _print_help(): + print('Usage: ' + sys.argv[0] + ' [option1 value1] ...') + print('available options: ') + + for (name, value) in sorted(inspect.getmembers(settings)): # @UnusedVariable + if name.upper() != name: + continue + + if not re.match('^[A-Z0-9_]+$', name): + continue + + name = '--' + name.lower().replace('_', '-') + print(' ' + name) + + print('') + + def _start_server(): + import server + server.application.listen(settings.PORT, settings.LISTEN) logging.info('server started') @@ -48,12 +123,16 @@ def _start_server(): def _start_motion(): + import config + import motionctl + if not motionctl.running() and config.has_enabled_cameras(): motionctl.start() logging.info('motion started') if __name__ == '__main__': + _configure_settings() _configure_signals() _configure_logging() _start_motion() diff --git a/settings.py b/settings.py index 4e348a6..6eeb272 100644 --- a/settings.py +++ b/settings.py @@ -5,10 +5,8 @@ import sys PROJECT_PATH = os.path.dirname(sys.argv[0]) -#CONF_PATH = os.path.join(PROJECT_PATH, 'conf') -CONF_PATH = '/media/data/projects/motioneye/conf/' -RUN_PATH = '/media/data/projects/motioneye/' -#RUN_PATH = PROJECT_PATH +CONF_PATH = os.path.join(PROJECT_PATH, 'conf') +RUN_PATH = PROJECT_PATH DEBUG = True LOG_LEVEL = logging.DEBUG