]> Frank Brehm's Git Trees - my-stuff/python.git/commitdiff
Got title from inf file
authorFrank Brehm <frank@brehm-online.com>
Mon, 26 May 2014 22:18:14 +0000 (00:18 +0200)
committerFrank Brehm <frank@brehm-online.com>
Mon, 26 May 2014 22:18:14 +0000 (00:18 +0200)
DE-env.py

index 4a87564d3b5526dc99fc434a4c4fa7b65e40873e..dd3340b161fec517924af54cab5f662083de2e61 100755 (executable)
--- a/DE-env.py
+++ b/DE-env.py
@@ -19,6 +19,8 @@ from gi.repository import Gio
 from gi.repository import Gdk
 import logging
 import pprint
+import ConfigParser
+import StringIO
 
 from wand.image import Image
 from wand.color import Color
@@ -340,6 +342,41 @@ def get_max_monitor_geometry():
 
     return (max_width, max_height)
 
+#-------------------------------------------------------------------------
+def get_title_from_ini(ini_file):
+
+    log.debug("Reading %r ...", ini_file)
+    if not os.path.isfile(ini_file):
+        return None
+
+    f = None
+    content = None
+    try:
+        f = open(ini_file)
+        content = f.read()
+    finally:
+        f.close()
+
+    if not content:
+        return None
+
+    content = "[__config__]\n" + content
+    ini_fp = StringIO.StringIO(content)
+    config = ConfigParser.RawConfigParser()
+    try:
+        config.readfp(ini_fp)
+    except Exception as e:
+        log.error("Got a %s on evaluating %r: %s", e.__class__.__name__,
+                ini_file, e)
+        return None
+
+    if config.has_section('__config__') and config.has_option('__config__', 'title'):
+        ini_title = config.get('__config__', 'title').strip()
+        if ini_title:
+            return ini_title
+
+    return None
+
 #-------------------------------------------------------------------------
 bg_dirs = [
     #'/home/fbrehm/Bilder/Wallpaper',
@@ -406,6 +443,16 @@ if comment:
             log.debug("New title from comment: %r", title)
             break
 
+inf_file = re.sub(r'\.[^\.]*$', '.inf', wpaper)
+if os .path.exists(inf_file):
+    inf_title = get_title_from_ini(inf_file)
+    if inf_title:
+        inf_title = inf_title.strip()
+    if inf_title:
+        inf_title = inf_title.decode('utf-8')
+        log.debug("Got title %r from %r.", inf_title, inf_file)
+        title = inf_title
+
 (screen_width, screen_height) = get_max_monitor_geometry()
 new_height = screen_height
 new_width = int(float(old_img.width) / float(old_img.height) * float(screen_height))