Technical discussions | Discussions techniques
Subscription date : 28 October 2009
Messages : 415
|
I'm looking into the DBus applet model and i came across this:
loop = gobject.MainLoop()
loop.run()
sys.exit(0)
I'm not really sure to understand what this does (since the applet seems to function anyway). Can you tell me more about it? |
matttbe, Sunday 17 April 2011 à 23:25
|
|
Subscription date : 24 January 2009
Messages : 12573
|
It's just the "MainLoop"
The idea is to keep the process alive because your applet has to listen to some signals, it waits for something (a click, a notification, etc.) and it can't be stopped. And when you stop the mainloop, you stop the program. |
Subscription date : 28 October 2009
Messages : 415
|
OK i'd really like to make some proper programming and learn so i'm still looking into threading. The problem, as far as i can tell occurs when the Applet() instance is created, then the threads seem to freeze. I made a small script to test this and you should try and see for yourself:
You can also download this Dummy applet, which contains this script: http://dl.free.fr/pxkNEZ5SF
from CDApplet import CDApplet
import threading
import time
class MakeApplet(threading.Thread):
def run(self):
print "...thread 04 making applet!"
facebook = Applet()
facebook.run()
time.sleep(3)
print "...facebook applet should be initiated!"
class MyThread(threading.Thread):
def run(self):
print("%s started!" % self.getName())
time.sleep(3)
print("%s finished!" % self.getName())
class Applet(CDApplet):
def __init__(self):
CDApplet.__init__(self)
def begin(self):
print "...It's alive!"
if __name__ == '__main__':
thread01 = MyThread(name= "Thread 01")
thread01.start()
time.sleep(1)
thread02 = MyThread(name= "Thread 02")
thread02.start()
time.sleep(1)
thread03 = MyThread(name= "Thread 03")
thread03.start()
time.sleep(1)
thread04 = MakeApplet()
thread04.start()
If i can't find anything i'll try to make a main pogram for applet() and another which checks FaceBook and reloads the applet via DBUS. |
Subscription date : 28 October 2009
Messages : 415
|
Haaa i found my solution:
see: http://www.jejik.com/articles/2007/01/python-gstreamer_threading_and_the_main_loop/
and: http://wiki.python.org/moin/DbusExamples
I started from scratch with the following script and will continue from there:
from CDApplet import CDApplet
import os
import threading
import time
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
import sys
import thread
DBusGMainLoop(set_as_default=True)
gobject.threads_init()
class FaceBook():
def __init__(self):
print "...FaceBook instance initiated."
bus = dbus.SessionBus()
applet_name = os.path.basename(os.path.abspath("."))
applet_path = "/org/cairodock/CairoDock/%s" % applet_name
try:
applet_object = bus.get_object("org.cairodock.CairoDock", applet_path)
except dbus.DBusException:
print "!!! applet '"+applet_name+"' can't be found on the bus, exit."
sys.exit(2)
icon = dbus.Interface(applet_object, "org.cairodock.CairoDock.applet")
fb = Applet()
print "...created applet"
t = MyThread()
t.start()
print "...while the thread is running"
time.sleep(3)
fb.speak()
loop = gobject.MainLoop()
loop.run()
sys.exit(0)
class Applet(CDApplet):
def __init__(self):
self.log = "...the applet is running"
CDApplet.__init__(self)
def speak(self):
print self.log
def on_click(self, iState):
print "Hello!"
class MyThread(threading.Thread):
def run(self):
print "...thread started"
time.sleep(10)
print "...thread finished"
if __name__ == '__main__':
FB = FaceBook()
|
Subscription date : 05 August 2009
Messages : 285
|
jesuisbenjamin I would try, at first, a gobject idle functions, or the one related to timeout to keep polling on Facebook. Even though I really hate polling process, the one from gobject can be scheduled when there is not other process with higher priority. Threading could hang the IO easily if you are not really sure how CD deals with applets. One question, are you accessing Facebook through some third-party module like pyfacebook? |
Subscription date : 28 October 2009
Messages : 415
|
Eduardo Mucelli : jesuisbenjamin I would try, at first, a gobject idle functions, or the one related to timeout to keep polling on Facebook. Even though I really hate polling process, the one from gobject can be scheduled when there is not other process with higher priority. Threading could hang the IO easily if you are not really sure how CD deals with applets. One question, are you accessing Facebook through some third-party module like pyfacebook?
Hello Eduardo,
Yes i am using fbcmd, third party software.
The threads_init() seems to work fine. I am just new to programming and i'm trying to stop the loop i created when closing the applet, but it keeps running even after Ctrl C. I thought giving the condition "while appplet" would do the trick, but i must misunderstand something cause it keeps spinning even the dock's gone.
I'm not quite sure to understand gobject.idle, does it perform an action when the applet's callbacks are not solicited? This means i could make it check FB when there is no activity rather than on a regular timing? If so that'd be more clever i think
Thanks |
Subscription date : 30 November 2007
Messages : 17118
|
Threading could hang the IO easily if you are not really sure how CD deals with applets
it just sends some Dbus signals, which are treated inside the main loop.
so making an html request in a g_idle would freeze the main loop, and therefore prevents the applet from responding to the Dbus calls.
therefore using a thread for potentially heavy tasks is a good solution, although I'm not sure how Dbus behaves with threads; maybe you will have to make the Dbus calls (like setting the icon image) outside of the thread. |
Subscription date : 28 October 2009
Messages : 415
|
@ Eduardo:
I've looked into the glib library (i found the gobject library will become deprecated), which basically is the same as gobject's (http://developer.gnome.org/pygobject/stable/glib-functions.html#function-glib--timeout-add).
The glib.timeout_add(interval, callback) looks appropriate for the applet i am making (it also has a priority attribute, so it can be set to idle as well). However i have not been able to run this function. Even in a small test code here below it didn't work out. Do you have any idea why no?
!import glib
class main():
def sayhi(self):
print "hello"
def repeat(self):
glib.timeout_add(3000, self.sayhi)
if __name__ == '__main__':
thing = main()
thing.repeat()
|
Subscription date : 28 October 2009
Messages : 415
|
OK i'm going forward:
Using glib.timeout_add() is initiated by gtk.main() and it keeps on repeating if its callback returns True. See following code:
!import glib
import gtk
class main():
def sayhi(self):
print "hello"
return True
def repeat(self):
print "here!"
glib.timeout_add(3000, self.sayhi)
if __name__ == '__main__':
thing = main()
thing.repeat()
gtk.main()
No i need to find some condition for the callback to return False, basically when the application is terminated. I'm not sure as to how to proceed though |
Subscription date : 28 October 2009
Messages : 415
|
OK, i'll keep updating for the sake of documentation:
i didn't realise that cairo-dock is a separate process from the applet
so now i tell the applet to check if cairo-dock exists, in which case it continues its repeated process, otherwise it stops.
import glib
import gtk
import os
import sys
class main():
def sayhi(self):
rollon = self.findcairo()
print "the affirmation that cair-dock exists is", rollon
return rollon
def repeat(self):
print "here!"
glib.timeout_add(3000, self.sayhi)
def findcairo(self):
try:
net = os.popen("ps -C cairo-dock | grep cairo-dock").readlines()[0].split()
if net[-1] == "cairo-dock":
return True
else:
return False
except:
return False
if __name__ == '__main__':
thing = main()
thing.repeat()
gtk.main()
|
Subscription date : 28 October 2009
Messages : 415
|
Well, my Facebook Applet is almost finished
I need to find out how to properly end the self.icon.DemandsAttention(True,"bounce"). I'd like it to bounce 4 times and stop. Using the False argument makes the bouncing too short to be noticable and even as i use time.sleep() it stops the bouncing while it's up and hangs there above the dock.
Any tip?
Also i want to play an alarm: os.popen("mplayer ./alarm.wav") should have done the trick i'd hoped. But it doesn't work
Is the path the problem?
Finally i need to find out how to change the configuration menu, but that shouldn't be hard i believe |
Guest, Tuesday 19 April 2011 à 03:27
|
|
|
I'll be looking forward to your release Thanks |
matttbe, Tuesday 19 April 2011 à 03:31
|
|
Subscription date : 24 January 2009
Messages : 12573
|
I need to find out how to properly end the self.icon.DemandsAttention(True,"bounce"). I'd like it to bounce 4 times and stop. Using the False argument makes the bouncing too short to be noticable and even as i use time.sleep() it stops the bouncing while it's up and hangs there above the dock.
Any tip? You can also use "Animate"
Also i want to play an alarm: os.popen("mplayer ./alarm.wav") should have done the trick i'd hoped. But it doesn't work
Is the path the problem? I guess it's just because you're not in the good directory. But I think it's not a good idea to use mplayer (it's not installed by default in a lot of distribution, aplay is maybe better but still not the best solution). There are a few librairies to read a .wav file (example)
Finally i need to find out how to change the configuration menu, but that shouldn't be hard i believe Do you want to use a new submenu? You can have a look to other applets |
Subscription date : 28 October 2009
Messages : 415
|
matttbe : You can also use "Animate"
I tried, but animate does not show if the dock's hidden -- and it's precisely when i want it to bounce.
mattbe : I think it's not a good idea to use mplayer (it's not installed by default in a lot of distribution, aplay is maybe better but still not the best solution). There are a few librairies to read a .wav file ( example)
Thanks
mattbe : Do you want to use a new submenu? You can have a look to other applets ;)
I mean the configuration tab in the applet configuration's window.
mysterious guest : I'll be looking forward to your release Thanks
A'right then! |
matttbe, Tuesday 19 April 2011 à 09:46
|
|
Subscription date : 24 January 2009
Messages : 12573
|
I tried, but animate does not show if the dock's hidden -- and it's precisely when i want it to bounce. you can demand the attention and also use animate... or demand the attention two time, etc.
I mean the configuration tab in the applet configuration's window. simply have a look in all .conf |
Subscription date : 28 October 2009
Messages : 415
|
matttbe : you can demand the attention and also use animate... or demand the attention two time, etc.
I'm afraid i don't follow. I can either:
- self.icon.DemandsAttention(True, 'bounce')
or
- self.icon.Animate('bounce', 4)
In the first case the animation is also visible when the doc is hidden/not in focus but it keeps on running till the icon is clicked.
In the second case i can control how long i want the animation to run, but it is not visible when the cairo is hidden/not in focus.
Did you mean there are alternatives to this? |
matttbe, Tuesday 19 April 2011 à 13:08
|
|
Subscription date : 24 January 2009
Messages : 12573
|
Yes, you can use self.icon.DemandsAttention(True, 'bounce') and self.icon.Animate('bounce', 4). Or you can use DemandsAttention two time if you want, no? |
Subscription date : 28 October 2009
Messages : 415
|
Not really.
self.icon.DemandsAttention(True, 'bounce') will start the bouncing till self.icon.DemandsAttention(False, '') is thrown.
I tried:
self.icon.DemandsAttention(True, 'bounce')
time.sleep(3)
self.icon.DemandsAttention(False, '')
But then it stays up and hangs above the dock as i mentioned before.
I tried (contrary to what the Wiki explains) to repeat the effect in the closing line:
icon.DemandsAttention(False, 'bounce') but it doesn't help (although it seems to occur less often than without).
I also have another issue with the icon.PopupDialog(). Throwing the Popup is easy, but how do i catch the answer?
The documentation says:
Wiki : PopupDialog
Pops up a dialog bubble on our icon (since 2.2.0). The dialog can contain a message, an icon, some buttons, and a widget the user can act on.
To get the answer, you need to connect to the "on_answer_dialog" signal.
But how do i "connect" to on_answer_dialog?
Shouldn't there be an ID value to connect sender and listener? I will use several such dialogues, and they need their respective response.
Thanks
B. |
matttbe, Tuesday 19 April 2011 à 13:33
|
|
Subscription date : 24 January 2009
Messages : 12573
|
I also have another issue with the icon.PopupDialog(). Throwing the Popup is easy, but how do i catch the answer? Simply have a look to the demo applet (and also Translator, Lifera, Pidgin and Quote) |
Subscription date : 30 November 2007
Messages : 17118
|
I don't think you need to stop the animation, since it is made to catch the user attention, and he may not be watching his screen at the moment.
adding an option to disable the animation is a good solution IMHO
But then it stays up and hangs above the dock as i mentioned before.
it should stop animating and becomt invisible if the dock is hidden. what exactly appears ?
But how do i "connect" to on_answer_dialog?
you don't have to, the Interface does it for you. you just have to redefine the function on_answer_dialog in your class |
Technical discussions | Discussions techniques
|