~phcteam/slidewall/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python2
# -*- encoding:utf-8 -*-

# System Modules
import subprocess, shlex
import argparse
from time import sleep
import sys
import signal
from random import shuffle

from xml.dom import minidom

# Local Modules
import conf
from xmlmanage import *

class SlidewallDaemon():

    xml_file = ""
    files = []
    picture_options = 'scaled'
    hold = 3000
    
    d = {}
    old_wallpaper = {'img' : "", 'options' : ""}

    def __init__(self):
        # INIT section:
        self.op = argparse.ArgumentParser(description="SlidewallDaemon")
        self.op.add_argument('-t', '--hold-time')
        self.op.add_argument('-r', '--random', action='store_true')
        self.op.add_argument('-v', '--verbose', action='store_true')
        self.op.add_argument('xml_file', nargs=1)
        parsing = self.op.parse_args()
        self.d = vars(parsing)
        if self.d['verbose']: print(self.d)          # Debug
        
        self.xml_file = self.d['xml_file'][0]
        self.get_old_wallpaper()
        if self.d['verbose']: print(self.old_wallpaper)          # Debug
        signal.signal(signal.SIGINT, self.sigINT_handler)
        signal.signal(signal.SIGTERM, self.sigINT_handler)
        signal.signal(signal.SIGQUIT, self.sigQUIT_handler)
        self.Run()
        
    def Run(self):
        # RUN section:
        n = 1
        i = 0
        while True:
            if i%n == 0:        # ogni giro di tutte le immagini rilegge il file, tante volte fosse stato aggiornato
                self.read_files()
                n = len(self.files)
                self.read_hold()
                if self.d['random']: shuffle(self.files) # shuffle the images list
                if self.d['verbose']: self.verbose()
            self.set_wallpaper(self.files[i%n], self.picture_options)
            self.wait()
            i += 1
    
    def sigQUIT_handler(self, signal, frame):
        print("\nSIGQUIT caught...nice! :)")

    def sigINT_handler(self, signal, frame):
        print("\nSIGINT caught!!!\nResetting old wallpaper and exiting...")
        self.set_wallpaper(self.old_wallpaper["img"], self.old_wallpaper["options"])
        sys.exit(0)
    
    def read_xml(self, tag):
        xmldoc = minidom.parse(self.xml_file)
        f_elements = xmldoc.getElementsByTagName(tag) # possibili tags 'file' 'duration' (?)
        values = map(lambda x: x.firstChild.nodeValue, f_elements)
        self.tmp = values
        
    def read_files(self):
        self.read_xml("file")
        self.files = self.tmp
    
    def read_hold(self):
        if self.d['hold_time']: # overread hold time from commandline
            self.hold = float(self.d['hold_time'])
        else:
            self.read_xml("duration")
            n = float(len(self.tmp))
            s = 0
            for x in self.tmp:
                s += float(x)
                self.hold = s/n
    
    def get_old_wallpaper(self):
        cmd = ['gsettings get org.gnome.desktop.background picture-uri', 'gsettings get org.gnome.desktop.background picture-options']
        key_list = ['img', 'options']
        cmd_lst = map(lambda x: shlex.split(x), cmd)
        i = 0
        for c in cmd_lst:
            p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            (out, err) = p.communicate()
            if err: print(err)
            if out:
                out = out.replace("file://", '')
                out = out.replace('\n', '')
                out = out.replace("'", '')
                k = key_list[i]
                self.old_wallpaper[k] = out
            i += 1
    
    def set_wallpaper(self, img, opt):
        # gsettings set org.gnome.desktop.background picture-uri $uri
        cmd = [ 'gsettings set org.gnome.desktop.background picture-uri file://%s' %(img.encode("utf-8")),
               'gsettings set org.gnome.desktop.background picture-options %s' %(opt) ]
        cmd_lst = map(lambda x: shlex.split(x), cmd)
        for c in cmd_lst:
            p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            (out, err) = p.communicate()
            if err: print err
    
    def wait(self):
        sleep(self.hold)
    
    def verbose(self):
        print("File_list: %s" %(str(self.files)))
        print("Hold_time: %s" %(str(self.hold)))
        

# --

if __name__ == '__main__':
    p = SlidewallDaemon()

# EOF