Technical discussions | Discussions techniques
Subscription date : 28 October 2009
Messages : 415
|
Hi there!
I'm working with Python to make a FaceBook applet. I have a configuration file from which i want to retrieve option settings for the applet. For this i use the get_config() process.
The odd thing is it is working for 6 options and not for the 7th i am adding, the "NoOptionError" is raised. Since my 7th option has an integer as a value rather than a boolean, i thought it was due to that, even though i use the keyfile.getint(). But changing the value to boolean or text does not make any difference. I don't understand why it doesn't want to pick up that one... Any idea?
Here is the section:
def get_config(self,keyfile):
print "...getting user-configuration"
# get user configuration:
## which notifications should be added to counter?
self.config['messages'] = keyfile.get('Configuration', 'messages')
self.config['pokes'] = keyfile.get('Configuration', 'pokes')
self.config['shares'] = keyfile.get('Configuration', 'shares')
self.config['friends'] = keyfile.get('Configuration', 'friends')
self.config['groups'] = keyfile.get('Configuration', 'groups')
self.config['events'] = keyfile.get('Configuration', 'events')
## how often should notifications be checked?
self.config['refresh'] = keyfile.getint('Configuration', 'refresh')
print self.config
And here is the error:
...getting user-configuration
Traceback (most recent call last):
File "./faceBook", line 77, in <module>
Applet().run()
File "./faceBook", line 37, in __init__
CDApplet.__init__(self)
File "/usr/lib/python2.6/dist-packages/CDApplet.py", line 82, in __init__
self._get_config()
File "/usr/lib/python2.6/dist-packages/CDApplet.py", line 216, in _get_config
self.get_config(keyfile)
File "./faceBook", line 56, in get_config
self.config['refresh'] = keyfile.getint('Configuration', 'refresh')
File "/usr/lib/python2.6/ConfigParser.py", line 340, in getint
return self._get(section, int, option)
File "/usr/lib/python2.6/ConfigParser.py", line 337, in _get
return conv(self.get(section, option))
File "/usr/lib/python2.6/ConfigParser.py", line 321, in get
raise NoOptionError(option, section)
ConfigParser.NoOptionError: No option 'refresh' in section: 'Configuration'
Finally, the .conf file section with configuration settings:
[Configuration]
#k Shortkey
shortkey =
messages = true
pokes = false
shares = true
friends = false
groups = false
events = false
refresh = 1 |
|
matttbe, Thursday 14 April 2011 à 15:34
|
|
Subscription date : 24 January 2009
Messages : 12573
|
Hello,
Finally, the .conf file section with configuration settings: Is it a preview of this file? => ~/.config/cairo-dock/current_theme/plug-ins/faceBook/faceBook.conf
PS: you can use a getboolean for other parameters. |
Subscription date : 28 October 2009
Messages : 415
|
Ooooh nooo
The file i'm editing is in ~/.config/cairo-dock/third-party/faceBook/faceBook.conf
How's this working? I'm getting confused between what's in the /current_theme section and outside of it now... |
|
matttbe, Thursday 14 April 2011 à 19:20
|
|
Subscription date : 24 January 2009
Messages : 12573
|
All .conf files in this directory (~/.config/cairo-dock/third-party/ but also /usr/share/cairo-dock/plug-ins/) are used to update conf files of the user. If you want to add some parameters, do it in .conf files of these folders and .conf files linked to your current theme will be automatically updated next time that you launch the dock.
Note that if you modify some parameters, you have to increase the version of your applet. |
Subscription date : 28 October 2009
Messages : 415
|
Well...when i restart the dock (or even reboot my computer) it does not see the changes in the .conf in the /third-party/faceBook/ folder ...
I was guessing: the /third-party/faceBook/ folder's conf is the default conf and loads in the /current_theme/ fro then on.
Right now i am working with the /current_theme/ folder since changes occur when reloading Cairo, i'll copy it in the /third-party/ where it'll be safe.
The applet is moving forward (albeit slowly), but less slowly than the thesis i should be writing! |
|
matttbe, Thursday 14 April 2011 à 19:41
|
|
Subscription date : 24 January 2009
Messages : 12573
|
The applet is moving forward (albeit slowly), but less slowly than the thesis i should be writing!
Well...when i restart the dock (or even reboot my computer) it does not see the changes in the .conf in the /third-party/faceBook/ folder ... No, you've to edit this .conf file and the .conf file in current_theme/plug-ins/... will be automatically updated (don't forget to change the version number but before the first version of your applet, it's maybe easier to directly edit this file in current_theme/plug-ins/...) |
Subscription date : 28 October 2009
Messages : 415
|
haaa i understand: changing the version number tells Cairo to reload the conf!
I thought it was just some fancy thing to do |
Subscription date : 28 October 2009
Messages : 415
|
PS i have question:
i am now thinking of how i should create a loop and wait to check the FB status. Should i summon a wait() process in the begin() process of the applet? Or somewhere else?
[EDIT] let me put it in other words:
i want to put in begin(self) something like:
while applet == True:
check_facebook()
time.sleep(sometime)
would that work?
if a callback is triggered during the sleep time will it still work? |
Subscription date : 28 October 2009
Messages : 415
|
Well i tried to put the time.sleep(sometime) in the begin() process but then there is not response to callbacks...
I've no experience with that, please let me know what to do? |
|
matttbe, Thursday 14 April 2011 à 22:21
|
|
Subscription date : 24 January 2009
Messages : 12573
|
I guess you've to create a new thread (or a new process?). It's not so hard to do that in Python, just have a look into the doc or somewhere else  |
Subscription date : 28 October 2009
Messages : 415
|
I looked into threading http://en.wikibooks.org/wiki/Python_Programming/Threading and more, but it didn't work out for me.
I tried to define the thread as a process of Applet, as a global function or as an object, in each case it failed, (without error message).
I'm not sure how to deal with threads in the case of OO programming.
Here is the current version of my code:
from CDApplet import CDApplet
import os
import threading
import time
class countdown(threading.Thread):
def run(self):
print "...thread initialised"
class Applet(CDApplet):
def __init__(self):
self.counter = 1
self.fb = {}
self.timer = 1
CDApplet.__init__(self)
def read_fb(self):
print "...reading data from faceBook"
fblist = os.popen("fbcmd NOTIFY").readlines()
for i in fblist:
ii = i.split()
try:
number = int(ii[-1])
self.fb[ii[0]] = number
except:
pass
print self.fb
def get_config(self,keyfile):
print "...getting user-configuration"
self.config['MESSAGES_UNREAD'] = keyfile.getboolean('Configuration', 'MESSAGES_UNREAD')
self.config['POKES'] = keyfile.getboolean('Configuration', 'POKES')
self.config['SHARES_UNREAD'] = keyfile.getboolean('Configuration', 'SHARES_UNREAD')
self.config['FRIEND_REQUESTS'] = keyfile.getboolean('Configuration', 'FRIEND_REQUESTS')
self.config['GROUP_INVITES'] = keyfile.getboolean('Configuration', 'GROUP_INVITES')
self.config['EVENT_INVITES'] = keyfile.getboolean('Configuration', 'EVENT_INVITES')
self.config['REFRESH'] = keyfile.getint('Configuration', 'REFRESH')
self.timer = self.config['REFRESH'] * 6
self.config['ATTENTION_WHEN'] = keyfile.get('Configuration', 'ATTENTION_WHEN')
print self.config
def begin(self):
self.read_fb()
self.set_counter()
print "...instantiating thread"
clock = countdown()
print "...starting thread"
clock.start()
def set_counter(self):
print "...setting counter"
new_counter = 0
for i in self.config:
if self.config[i].__class__ != bool:
pass
elif self.config[i] == False:
pass
else:
new_counter += self.fb[i]
diff = self.counter - new_counter
self.counter = new_counter
if self.counter != 0:
self.icon.SetQuickInfo(format(self.counter))
else:
pass
att = self.config["ATTENTION_WHEN"]
if att == "always" and self.counter != 0:
self.icon.DemandsAttention(True,"bounce")
time.sleep(2)
self.icon.DemandsAttention(False,"bounce")
elif att == "different" and diff != 0:
self.icon.Animate("bounce",4)
elif att == "superior" and diff > 0:
self.icon.Animate("bounce",4)
else:
pass
def on_click(self, iState):
print "You have", self.counter, "unread message(s) on your faceBook account."
if __name__ == '__main__':
Applet().run()
|
|
matttbe, Friday 15 April 2011 à 10:38
|
|
Subscription date : 24 January 2009
Messages : 12573
|
I'm not a Python expert but I think it's maybe better to use a Timer: import threading
import time
def CustomTimer(temp = 1.0):
threading.Timer(temp, CustomTimer, [temp]).start()
print "Hello World %d second" % temp
CustomTimer(2.0)
|
Subscription date : 28 October 2009
Messages : 415
|
Thanks! But there is still something going wrong, i can't think why.
I made a test run first (to see how to run the timer from within a process) and it works fine:
import time
import threading
def timer(sometime, obj):
print "...beginning timer"
t = threading.Timer(sometime, timer, [sometime, obj])
t.start()
obj.poke()
class thing(object):
def __init__(self):
self.time = 5
def run_timer(self):
timer(self.time, self)
def poke(self):
print "ouch!"
if __name__ == '__main__':
o = thing()
o.run_timer()
But when i apply this to my Applet's code, it appears the countdown() function runs only once
Current code:
from CDApplet import CDApplet
import os
import threading
import time
def countdown(sometime, app):
print "...countdown started"
t = threading.Timer(sometime, countdown, [sometime, app])
t.start()
app.read_fb()
class Applet(CDApplet):
def __init__(self):
self.counter = 1
self.fb = {}
self.timer = 1
CDApplet.__init__(self)
def read_fb(self):
print "...reading data from faceBook"
fblist = os.popen("fbcmd NOTIFY").readlines()
for i in fblist:
ii = i.split()
try:
number = int(ii[-1])
self.fb[ii[0]] = number
except:
pass
print self.fb
def get_config(self,keyfile):
print "...getting user-configuration"
self.config['MESSAGES_UNREAD'] = keyfile.getboolean('Configuration', 'MESSAGES_UNREAD')
self.config['POKES'] = keyfile.getboolean('Configuration', 'POKES')
self.config['SHARES_UNREAD'] = keyfile.getboolean('Configuration', 'SHARES_UNREAD')
self.config['FRIEND_REQUESTS'] = keyfile.getboolean('Configuration', 'FRIEND_REQUESTS')
self.config['GROUP_INVITES'] = keyfile.getboolean('Configuration', 'GROUP_INVITES')
self.config['EVENT_INVITES'] = keyfile.getboolean('Configuration', 'EVENT_INVITES')
self.config['REFRESH'] = keyfile.getint('Configuration', 'REFRESH')
self.timer = self.config['REFRESH'] * 6
self.config['ATTENTION_WHEN'] = keyfile.get('Configuration', 'ATTENTION_WHEN')
print self.config
def begin(self):
self.read_fb()
self.set_counter()
print "...summoning countdown"
countdown(5, self)
def set_counter(self):
print "...setting counter"
new_counter = 0
for i in self.config:
if self.config[i].__class__ != bool:
pass
elif self.config[i] == False:
pass
else:
new_counter += self.fb[i]
diff = self.counter - new_counter
self.counter = new_counter
if self.counter != 0:
self.icon.SetQuickInfo(format(self.counter))
else:
pass
att = self.config["ATTENTION_WHEN"]
if att == "always" and self.counter != 0:
self.icon.DemandsAttention(True,"bounce")
time.sleep(2)
self.icon.DemandsAttention(False,"bounce")
elif att == "different" and diff != 0:
self.icon.Animate("bounce",4)
elif att == "superior" and diff > 0:
self.icon.Animate("bounce",4)
else:
pass
def on_click(self, iState):
print "You have", self.counter, "unread message(s) on your faceBook account."
if __name__ == '__main__':
Applet().run()
I posted this too on the Ubuntu Forum, hoping to get some guidance.
Cheers
Benjamin |
|
matttbe, Saturday 16 April 2011 à 10:09
|
|
Subscription date : 24 January 2009
Messages : 12573
|
I think (but it's maybe much better to wait for an answer on the Ubuntu forum ) that you've to define your countdown function in the Applet class.
And if you want to stop this thread, you've to access to this t variable which is not currently possible I think.
PS: don't forget to hid your mail address and it's better to add =C next to the code balise => code=C] (...) [/code] |
Subscription date : 28 October 2009
Messages : 415
|
Thanks  |
Subscription date : 28 October 2009
Messages : 415
|
I tried many things and have exhausted most possibilities (at least i can't think of any other).
With the Timer.isAlive() process i can tell the Timer thread is running.
I tested this Timer by telling it to os.system("gedit") to see if anything happened outside the applet. Indeed it worked out, but only after i killed the applet (Ctrl+C from Terminal). This suggests the Timer thread is enqueued somehow and cannot start till the applet's killed.
I have no understanding of the API, but i believe its structure prevents a thread from running simultaneously as an attribute of the applet.
So the idea comes to my mind to make another script which can run on parallel with it and triggers it every now and then. Only i don't know how to reach the running applet from outside the script. Is there any way to do that? |
Subscription date : 30 November 2007
Messages : 17118
|
did you try to take a look at the other python applets ?
Eduardo made several ones and I think he may have needed threads in one of them (maybe Quote of the Day, or Translator, or Google, since they make html requests) |
Subscription date : 28 October 2009
Messages : 415
|
fabounet : did you try to take a look at the other python applets ?
Eduardo made several ones and I think he may have needed threads in one of them (maybe Quote of the Day, or Translator, or Google, since they make html requests)
I did look, saw nothing -- but will look again, i may have missed it  |
Subscription date : 30 November 2007
Messages : 17118
|
if you use the http module of Python (I believe it's called urllib), maybe there are already some functions that are threaded ?
I'm sorry I can't help you more, I'm still a beginner in Python |
Subscription date : 28 October 2009
Messages : 415
|
Ha! it seems he used dbus to loop back into the applet That's what i also concluded last night.
That's going to be fun!  |
Technical discussions | Discussions techniques
|