Explorar o código

multiple arguments on commandline

devnull hai 3 semanas
pai
achega
4c841cb6f5
Modificáronse 1 ficheiros con 28 adicións e 22 borrados
  1. 28 22
      kbdLED

+ 28 - 22
kbdLED

@@ -21,9 +21,6 @@ import argparse
 import json
 import logging
 
-log = logging.getLogger("kbdLED")
-
-
 
 class kbdLED_class:
 
@@ -38,7 +35,7 @@ class kbdLED_class:
                             sysfs_color_fp='/sys/devices/platform/tuxedo_keyboard/leds/rgb:kbd_backlight/multi_intensity',
                             sysfs_bri_fp='/sys/devices/platform/tuxedo_keyboard/leds/rgb:kbd_backlight/brightness'):
 
-
+        self.log = log
         self.config = {"sysfs" :
                            {
                                 "color" : sysfs_color_fp ,
@@ -46,11 +43,11 @@ class kbdLED_class:
                            },
                        "state" : kbd_state_fp}
 
-        #DEBUGprint("debug: " + str(self.config))
+        self.log.debug(str(self.config))
 
         if cmd=="start":
             self.readStateFile(self.config['state'])
-            #print("debug: " + str(self.config))
+            self.log.debug(str(self.config))
             self.writeColor()
             self.writeBrightness()
 
@@ -75,9 +72,9 @@ class kbdLED_class:
                 self.config = json.load(kbd_state_fh)
 
         except FileNotFoundError:
-            print("keine Statusdatei gefunden, nutze Standardwerte")
+            self.log.error("keine Statusdatei gefunden, nutze Standardwerte")
         except PermissionError:
-            print("Fehler beim lesen")
+            self.log.error("Fehler beim lesen")
 
     def writeStateFile(self,kbd_state_fp):
         try:
@@ -85,7 +82,7 @@ class kbdLED_class:
                 json.dump(self.config,kbd_state_fh)
 
         except FileNotFoundError:
-            print("Verzeichnis nicht gefunden")
+            self.log.error("Verzeichnis oder Datei nicht gefunden")
 
     # color String nach Config Dict
     def color2dict(self,color_string):
@@ -105,10 +102,10 @@ class kbdLED_class:
                 self.color2dict(fh.read())
 
         except FileNotFoundError:
-            print("- fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['color'])
+            self.log.error("fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['color'])
             sys.exit(1)
         except PermissionError:
-            print("- keine Rechte die Datei zu lesen " + self.config['sysfs']['color'])
+            self.log.error("keine Rechte die Datei zu lesen " + self.config['sysfs']['color'])
             sys.exit(1)
 
 
@@ -122,11 +119,11 @@ class kbdLED_class:
             fh.close()
 
         except FileNotFoundError:
-            print("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['color'])
+            self.log.error("fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['color'])
             sys.exit(1)
 
         except PermissionError:
-            print("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['color'])
+            self.log.error("keine Rechte die Datei zu schreiben " + self.config['sysfs']['color'])
             sys.exit(1)
 
     # reads Brightness from sysfs
@@ -136,10 +133,10 @@ class kbdLED_class:
                 self.config['Brightness'] = int(fh.read())
 
         except FileNotFoundError:
-            print("- fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['bri'])
+            self.log.error("fehler beim Öffnen der Sysfs Datei " + self.config['sysfs']['bri'])
             sys.exit(1)
         except PermissionError:
-            print("- keine Rechte die Datei zu lesen " + self.config['sysfs']['bri'])
+            self.log.error("keine Rechte die Datei zu lesen " + self.config['sysfs']['bri'])
             sys.exit(1)
 
     # writes Brightness to sysfs
@@ -152,33 +149,42 @@ class kbdLED_class:
             fh.close()
 
         except FileNotFoundError:
-            print("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['bri'])
+            self.log.error("- fehler beim Schreiben der Sysfs Datei " + self.config['sysfs']['bri'])
             sys.exit(1)
         except PermissionError:
-            print("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['bri'])
+            self.log.error("- keine Rechte die Datei zu schreiben " + self.config['sysfs']['bri'])
             sys.exit(1)
 
 
 # Press the green button in the gutter to run the script.
+# noinspection aK
 if __name__ == '__main__':
 
+    #https: // argparse.readthedocs.io
     parser = argparse.ArgumentParser(
                     prog='T1500_KeyboardLED',
                     description='saves and restore LED Colors',
                     epilog='GPL')
+    output = parser.add_mutually_exclusive_group(required=False)
+    output.add_argument('--verbose', action='store_true', help='extended logging', required=False)
+    output.add_argument('--debug', action='store_true', help='debug output', required=False)
+
     startstop = parser.add_mutually_exclusive_group()
     startstop.add_argument('--start', action='store_true', help='will be used for system starts')
     startstop.add_argument('--stop', action='store_true', help='will be used for system shutdowns')
-    startstop.add_argument('--verbose', action='store_true', help='extended Logging')
-    startstop.add_argument('--debug', action='store_true', help='debugging information')
     startstop.add_argument('--version', action='version', version='%(prog)s ' + ver, default='d')
 
     args = parser.parse_args()
 
     if args.verbose:
-        log.setLevel(logging.INFO)
-    if args.debug:
-        log.setLevel(logging.DEBUG)
+        logging.basicConfig(level=logging.INFO)
+    elif args.debug:
+        logging.basicConfig(level=logging.DEBUG)
+    else:
+        logging.basicConfig(level=logging.ERROR)
+
+    logging.basicConfig(format='%(asctime)s %(message)s')
+    log = logging.getLogger(__name__)
 
     if args.start:
         print("Keyboard: read and set Color and Brightness")