]> Frank Brehm's Git Trees - my-stuff/python.git/commitdiff
Extended
authorFrank Brehm <frank@brehm-online.com>
Sun, 18 May 2014 18:27:41 +0000 (20:27 +0200)
committerFrank Brehm <frank@brehm-online.com>
Sun, 18 May 2014 18:27:41 +0000 (20:27 +0200)
DE-env.py

index 1a6d6c334d6069161005f4c553ce09f565f2c3b4..4162a11ca49bd4834548825682993f741e4e7576 100755 (executable)
--- a/DE-env.py
+++ b/DE-env.py
@@ -16,16 +16,63 @@ import re
 #import gconf
 import random
 from gi.repository import Gio
+import logging
 
 from wand.image import Image
 from wand.color import Color
 from wand.drawing import Drawing
 
-APP_NAME = 'bluberblubb'
+APP_NAME = 'chwallpaper'
+
+log = logging.getLogger(APP_NAME)
+verbose = 1
 
 fsize_title = 16
 fsize_subtitle = 11
 font = '/usr/share/fonts/corefonts/verdanab.ttf'
+if not os.path.exists(font):
+    font = '/usr/share/fonts/truetype/msttcorefonts/Verdana_Bold.ttf'
+
+def init_logging():
+    """
+    Initialize the logger object.
+    It creates a colored loghandler with all output to STDERR.
+    Maybe overridden in descendant classes.
+
+    @return: None
+    """
+
+    root_log = logging.getLogger()
+    root_log.setLevel(logging.INFO)
+    if verbose:
+        root_log.setLevel(logging.DEBUG)
+
+    # create formatter
+    format_str = ''
+    if verbose > 1:
+        format_str = '[%(asctime)s]: '
+    format_str += APP_NAME + ': '
+    if verbose:
+        if verbose > 1:
+            format_str += '%(name)s(%(lineno)d) %(funcName)s() '
+        else:
+            format_str += '%(name)s '
+    format_str += '%(levelname)s - %(message)s'
+    formatter = logging.Formatter(format_str)
+
+    # create log handler for console output
+    lh_console = logging.StreamHandler(sys.stderr)
+    if verbose:
+        lh_console.setLevel(logging.DEBUG)
+    else:
+        lh_console.setLevel(logging.INFO)
+    lh_console.setFormatter(formatter)
+
+    root_log.addHandler(lh_console)
+
+    return
+
+init_logging()
 
 def get_desktop_environment():
     #From http://stackoverflow.com/questions/2035657/what-is-my-current-desktop-environment
@@ -33,11 +80,15 @@ def get_desktop_environment():
     # and http://ubuntuforums.org/showthread.php?t=652320
     # and http://ubuntuforums.org/showthread.php?t=1139057
     if sys.platform in ["win32", "cygwin"]:
+        log.debug("DE is 'windows'")
         return "windows"
     elif sys.platform == "darwin":
+        log.debug("DE is 'mac'")
         return "mac"
     else: #Most likely either a POSIX system or something not much common
+        log.debug("DE is none of %r", ("win32", "cygwin", "mac"))
         desktop_session = os.environ.get("DESKTOP_SESSION")
+        log.debug("$DESKTOP_SESSION is %r", desktop_session)
         if desktop_session is not None: #easier to match if we doesn't have  to deal with caracter cases
             desktop_session = desktop_session.lower()
             if desktop_session in ["gnome","unity", "cinnamon", "mate", "xfce4", "lxde", "fluxbox", 
@@ -58,11 +109,19 @@ def get_desktop_environment():
                 return "razor-qt"
             elif desktop_session.startswith("wmaker"): # e.g. wmaker-common
                 return "windowmaker"
-        if os.environ.get('KDE_FULL_SESSION') == 'true':
+        kde_full_session = os.environ.get('KDE_FULL_SESSION')
+        gnome_desktop_session_id = os.environ.get('GNOME_DESKTOP_SESSION_ID')
+        mate_desktop_session_id = os.environ.get('MATE_DESKTOP_SESSION_ID')
+        log.debug("$KDE_FULL_SESSION is %r", kde_full_session)
+        log.debug("$GNOME_DESKTOP_SESSION_ID is %r", gnome_desktop_session_id)
+        log.debug("$MATE_DESKTOP_SESSION_ID is %r", mate_desktop_session_id)
+        if kde_full_session == 'true':
             return "kde"
-        elif os.environ.get('GNOME_DESKTOP_SESSION_ID'):
+        elif gnome_desktop_session_id:
             if not "deprecated" in os.environ.get('GNOME_DESKTOP_SESSION_ID'):
                 return "gnome2"
+        elif mate_desktop_session_id:
+            return 'mate'
         #From http://ubuntuforums.org/showthread.php?t=652320
         elif is_running("xfce-mcs-manage"):
             return "xfce4"
@@ -73,6 +132,7 @@ def get_desktop_environment():
 def is_running(process):
     #From http://www.bloggerpolis.com/2011/05/how-to-check-if-a-process-is-running-using-python/
     # and http://richarddingwall.name/2009/06/18/windows-equivalents-of-ps-and-kill-commands/
+    log.debug("Check for running process %r ...", process)
     try: #Linux/Unix
         s = subprocess.Popen(["ps", "axw"],stdout=subprocess.PIPE)
     except: #Windows
@@ -252,6 +312,7 @@ def check_img(arg, dirname, names):
 
 for img_dir in bg_dirs:
     if os.path.isdir(img_dir):
+        log.debug("Registering wallpapers in %r ...", img_dir)
         os.path.walk(img_dir, check_img, None)
 
 wpaper = '/home/fbrehm/Bilder/Wallpaper/Sonstiges/More than Meets the Eye.jpg'
@@ -260,13 +321,13 @@ if found_files:
 SCHEMA = "org.mate.background"
 KEY = "picture-filename"
 
-print(get_desktop_environment())
+log.info("Current Desktop Environment: %r", get_desktop_environment())
 
 gsettings = Gio.Settings.new(SCHEMA)
 cur_wpaper = gsettings.get_string(KEY)
 
-print("Current Wallpaper: %r." % (cur_wpaper))
-print("Setting Wallpaper to: %r." % (wpaper))
+log.debug("Current Wallpaper: %r.", cur_wpaper)
+log.debug("Setting Wallpaper to: %r.", wpaper)
 if os.path.exists(wpaper) and os.path.isfile(wpaper):
     gsettings.set_string(KEY, wpaper)
 
@@ -294,21 +355,18 @@ draw.font_size = fsize_title
 fm = draw.get_font_metrics(new_img, title, False)
 x = int(new_img.width / 2)
 y = int(fm.text_height)
-#print("Koordinaten: x %r, y %r." % (x, y))
+log.debug("Koordinaten: x %r, y %r.", x, y)
 draw.text(x, y, title)
 
 draw.font_size = fsize_subtitle
 fm = draw.get_font_metrics(new_img, wpaper, False)
 x = int(new_img.width / 2)
 y = int(fm.text_height) + y + 5
-#print("Koordinaten: x %r, y %r." % (x, y))
+log.debug("Koordinaten: x %r, y %r.", x, y)
 draw.text(x, y, wpaper)
 
 draw(new_img)
 
 new_img.save(filename = new_img_fname)
 
-
-
-
 # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4