View Single Post
Posts: 277 | Thanked: 319 times | Joined on Jan 2010
#501
Originally Posted by Win7Mac View Post
Big thanks slarti!

I couldn't get it to work by simply adding import sys at the top too. So what about the syntax errors?
You wrote the command in shell, not in the python interpreter. If you add import sys to where import dbus is and run the modified script with:
Code:
python myscript.py
it will tell you where the problem is.

Originally Posted by Win7Mac View Post
Yeah! Now I got the picture about the special chars in python. Works great.
Great!

Originally Posted by Win7Mac View Post
Almost done now, just get rid of the 'No alarms' msg in a way that the line in BB disappears (and won't leave a blank line), if there's no alarm. Add the second alarm probably?
Just replace the command that prints the "No alarms" with pass.

I'll have to think about the second alarm. I'm only learning python and scripting myself and this script was meant for just the next one. It sorts the alarms chronologically and prints the next one (from now) in the list. It could print the next two but then this next part would be more difficult:

Originally Posted by Win7Mac View Post
Edit: I'm already using your script for short weekday, could this be implemented in the above script to adopt to my language? I prefer short weekday very much over the "+1" from {events}, good idea!
I added a "translator" to this version and changed the No alarms part to what you wanted. It's in finnish, though, so you'll have to change abb_weekdays to what you want. Just make sure you start from sunday.

Code:
#!/usr/bin/python

import dbus
from datetime import datetime, timedelta
import time

bus = dbus.SystemBus()

time_obj = bus.get_object('com.nokia.time', '/com/nokia/time')
time_intf = dbus.Interface(time_obj, 'com.nokia.time')

cookies = time_intf.get_cookies_by_attributes({'enabled': '1'})
today = datetime.now()
tomorrow = today + timedelta(days=1)

def list_queued_alarms():
    for cookie in cookies:
        attributes = time_intf.query_attributes(cookie)
        alarmtime = attributes['alarmtime']
        if attributes['STATE'] == 'QUEUED':
            if 'recurrence' in attributes:
                days = tuple(attributes['recurrence'])
            else:
                if datetime.time(datetime.strptime(alarmtime, ("%H:%M"))) > datetime.time(datetime.now()):
                    days = time.strftime("%w")
                else:
                    days = tomorrow.strftime("%w")
            weekdays = dict([(day, time.strptime((day + " " + alarmtime),'%w %H:%M' )) for day in days])
            for day in days:
                yield ' '.join((day,time.strftime('%H:%M',weekdays[day]),attributes['TITLE']))


findme_list = [' '.join((time.strftime('%w %H:%M'),'findme'))]
L = list(list_queued_alarms()) + findme_list
L.sort()
findme_string = ' '.join((time.strftime('%w %H:%M'),'findme'))
findme_int = L.index(findme_string)
if len(L) == 1:
    pass
else:
    if findme_int == (len(L) - 1):
        next_alarm = L[0]
    else:
        next_alarm = L[findme_int + 1]

abb_weekdays = ['Su','Ma','Ti','Ke','To','Pe','La']
next_alarm_list = next_alarm.split(' ' ,1)

print ' '.join((abb_weekdays[int(next_alarm_list[0])],next_alarm_list[1])).encode('utf-8') + '\xe2\x98\x9a'
 

The Following User Says Thank You to slarti For This Useful Post: