rozen, Saturday 08 September 2012 à 18:02
|
|
Subscription date : 20 December 2009
Messages : 19
|
Hi,
I have been thinking about this posting for several days because I am not sure exactly how to put it. I am concerned that I might ask for something that upon careful consideration I really would not want.
The spot where I am coming from is:
(1) I really want to build any applets in Python. I have done my share of programming in the shell and in C.
(2) Most of the applets I want to construct require that the display be updated periodically. This seems to apply to a large group of applets like timers, calendars, moon phase applets, as well as system monitoring applets.
This requirement for periodic updates does not seem to be addressed well in Cairo-Dock as shown by the fact that the included Accessory Applet Calendar built its own update mechanism but it contained errors and did not function while the Moon Accessory Applet didn't even try.
In order to get a better understanding of the situation, I rewrote a simplified version of Calendar in Python. But trying to provide the updating mechanism when midnight occurs as well as, testing the date when a suspended system is restarted drove me into threads, which are confusing. While it appears to work, I am not sure it is a good implementation. I am attaching my implementation, NewCal.py for comment. Even then I was not able to invoke dbus via Python; I had to resort to os.system() to invoke a CLI command. (More on that with another post.)
I think that I was looking for something like the following. You provide the Python programmer the functions: begin, end, reload, and get_config. I think it would be helpful to add an "update" method supported with a periodic call. It would seem that it would be better to provide that at the CD level rather than requiring an "ad hoc" solution for each applet requiring it. A parameter in the applet configure file could specify the period in seconds or milliseconds with a value of zero meaning that no update function is desired.
I am running Cairo-Dock 3.0.0, on Mint 13 with Xfce
I would really appreciate hearing your thoughts,
Respectfully,
Don
import icon_template
import datetime, time
import os
import threading
from CDApplet import CDApplet
from CairoDock import CairoDock
reloadicon_command = "dbus-send --session --dest=org.cairodock.CairoDock /org/cairodock/CairoDock org.cairodock.CairoDock.ReloadIcon string:\"config-file=NewCal.conf\""
class NewCal(CDApplet):
def __init__(self):
CDApplet.__init__(self)
self.display_date = ''
self.die = 0
self.duration = 10 def end(self):
self.die = 1
def begin(self):
today = datetime.date.today()
if self.display_date != today:
self.update_icon(today)
self.display_date = today
self.t = threading.Thread(target=update_timer,
args=(self, self.duration))
self.t.start()
def update(self):
today = datetime.date.today()
if self.display_date != today:
self.update_icon(today)
self.display_date = today
def restart(self):
self.update()
def update_icon(self, today):
(Year, Month, Day) = str(today).split('-')
if Day[0] == '0':
Day = Day[1]
icon_string = icon_template.template % (Month, Day)
icon_file = open('icon', 'w')
print >> icon_file, icon_string
cmd = reloadicon_command
os.system(cmd)
def update_timer(c, duration):
print('update_timer starting')
while 1:
if c.die: return
time.sleep(duration)
c.update()
return
if __name__ == '__main__':
NewCal().run()
|
matttbe, Sunday 09 September 2012 à 13:20
|
|
Subscription date : 24 January 2009
Messages : 12573
|
Hello,
Yes, maybe it can be interesting to add this 'update' function to be able to use a cairo-dock-task.
I guess it's maybe too late to add it on the next version but it's not too late to start talking about this (possible) future feature
@fabounet: what do you think about that?
PS: @rozen: I think 'Twitter' applet is using threads and timers (from gobject) if you want another example  |
Subscription date : 05 August 2009
Messages : 285
|
I think it is better to use GObject's timeout_add function.
From the doc, "The gobject.timeout_add() function sets a function (specified by callback) to be called at regular intervals (specified by interval, with the default priority, gobject.PRIORITY_DEFAULT. Additional arguments to pass to callback can be specified after callback. The idle priority may be specified as a keyword-value pair with the keyword "priority"." |
fabounet, Friday 14 September 2012 à 22:48
|
|
Subscription date : 30 November 2007
Messages : 17118
|
+1, if you want to make a periodic task, use a gobject timer, and if you want to use threads, it's relatively easy in Python (at least, more than in C )
so Cairo-Dock doesn't need to provide anything here, not like in C where it provide the CairoTask. |
|