-
Notifications
You must be signed in to change notification settings - Fork 1
/
reference-watcher.py
executable file
·126 lines (97 loc) · 3.35 KB
/
reference-watcher.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# reference-watcher - An example watcher for alan2
# Copyright (C) 2013 Semplice Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Authors:
# Eugenio "g7" Paolantonio <me@medesimo.eu>
#
# This file contains a reference python watcher based on python-nala libraries.
#
import sys, os, time
import subprocess
from gi.repository import GLib
from nala.watchers import WatcherPool
from nala.queue import Queue
from nala.applications import Application
from ConfigParser import SafeConfigParser
DEFAULT_PATH=os.path.expanduser("~/.config/alan-menus")
ALAN2_BUILDER_PATH=os.path.expanduser("/usr/bin/alan-menu-updater")
def add_to_queue(pool, watcher, trigger, event, queue):
""" Add to queue. """
return queue.add_to_queue(watcher, trigger, event)
def do_something(queue, apps, lst):
""" Fired when ready to regenerate the menus """
processes = []
print "GOT QUEUE!", apps, lst
# Regenerate!
for app in apps:
name = app.path
processes.append(subprocess.Popen([ALAN2_BUILDER_PATH, name], shell=False))
while processes != []:
# Poll every active process
for proc in processes:
if proc.poll != None:
# Yeah, remove it
processes.remove(proc)
time.sleep(0.1)
# Reconfigure openbox
subprocess.Popen(["openbox", "--reconfigure"], shell=False)
# Hello all...
# Should setup?
if not os.path.exists(DEFAULT_PATH):
# Yeah babe!
ret = subprocess.call(["/usr/share/alan2/alan2-setup.sh", "semplice"]) # FIXME, we don't like hardcoded things
if ret > 0:
# :/
raise Exception("Unable to setup alan2.")
# Reconfigure openbox
subprocess.Popen(["openbox", "--reconfigure"], shell=False)
# parse the watchers/
applications = []
files = {}
applications_objects = {}
pool = WatcherPool()
queue = Queue(3)
for application in os.listdir("/etc/alan/watchers/"):
_file = os.path.join("/etc/alan/watchers", application)
application = application.replace(".watcher","")
cfg = SafeConfigParser()
cfg.read(_file)
applications.append(cfg.get("nala", "application"))
# Get files
_files = []
if cfg.has_option("nala", "files"):
__files = cfg.get("nala", "files").split(" ")
else:
__files = []
__files += ["/etc/alan/alan.conf", os.path.expanduser("~/.gtkrc-2.0")]
for _file in __files:
path = os.path.expanduser(_file)
if os.path.islink(path):
path = os.readlink(path)
_files.append(path)
files[application] = _files
# Add to pool and queue
applications_objects[application] = Application(application, files[application])
pool.add_watcher(applications_objects[application].triggers)
queue.add_application(applications_objects[application])
pool.connect("watcher-changed", add_to_queue, queue)
queue.connect("processable", do_something)
print files
if __name__ == "__main__":
loop = GLib.MainLoop()
loop.run()