]> www.vanbest.org Git - motioneye-debian/commitdiff
added command line settings support
authorCalin Crisan <ccrisan@gmail.com>
Sat, 5 Oct 2013 15:37:27 +0000 (18:37 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 5 Oct 2013 15:37:27 +0000 (18:37 +0300)
motioneye.py [changed mode: 0644->0755]
settings.py

old mode 100644 (file)
new mode 100755 (executable)
index 8e45f10..995ec57
@@ -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()
index 4e348a6dddbe1ac8f01f9e09ec072884f6c3409c..6eeb272d01f2cb00af03a77e2574fddc4382f94b 100644 (file)
@@ -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