Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # pyTomboyRemind.py
- #
- # For description please see usage() function
- #
- # REQUIREMENTS:
- # python-dbus
- # python-lxml
- #
- import sys
- from getopt import getopt, GetoptError
- import dbus
- from lxml import etree
- from cStringIO import StringIO
- class SETTINGS:
- '''Stores global script settings'''
- noteTitle = None
- PADDING_INCREMENT = ' '
- PADDING_INCREMENT_LEN = 3
- def main():
- '''The main function'''
- parseCommandLine()
- obj = dbus.SessionBus().get_object("org.gnome.Tomboy", "/org/gnome/Tomboy/RemoteControl")
- tomboy = dbus.Interface(obj, "org.gnome.Tomboy.RemoteControl")
- noteUri = tomboy.FindNote(SETTINGS.noteTitle)
- if not noteUri:
- print >> sys.stderr, 'No notes with title\'' + SETTINGS.noteTitle + '\''
- exit(1)
- padding = ''
- firstNode = True # to handle note title
- xml = tomboy.GetNoteContentsXml(noteUri).encode('utf-8')
- print >> sys.stderr, xml
- for event, node in etree.iterparse(StringIO(xml), events=('start', 'end')):
- if node.tag == 'list':
- if event == 'start':
- padding += SETTINGS.PADDING_INCREMENT
- elif event == 'end':
- padding = padding[0:-SETTINGS.PADDING_INCREMENT_LEN]
- if node.tail:
- printText(node.tail.encode('utf-8'), padding)
- else:
- if event == 'start':
- if firstNode:
- firstNode = False
- text = node.text.encode('utf-8')[len(SETTINGS.noteTitle) + 2:] # 2 = number of \n
- printText(text, padding)
- else:
- printText(node.text.encode('utf-8'), padding)
- def printText(text, padding):
- text = text.strip('\n')
- if text.startswith('[type='):
- a = text.find(']', 6) # 6 = len('[type=')
- taskType = text[6:a]
- if taskType == 'important':
- text = '${color2}' + text[a+1:] + '${color0}'
- elif taskType == 'prepare':
- text = '${color3}' + text[a+1:] + '${color0}'
- elif taskType == 'freetime':
- text = '${color4}' + text[a+1:] + '${color0}'
- elif taskType == 'wait':
- text = '${color5}' + text[a+1:] + '${color0}'
- elif taskType == 'hard':
- text = '${color6}' + text[a+1:] + '${color0}'
- elif taskType == 'stats':
- text = '${color7}' + text[a+1:] + '${color0}'
- else:
- print >> sys.stderr, 'Unknown task type: \'' + taskType + '\''
- print padding + text
- def parseCommandLine():
- '''Parse command line arguments and fill SETTINGS.
- Calls exit() if an error occurs.
- '''
- try:
- options, arguments = getopt(sys.argv[1:], 'hn:', ['help', 'note='])
- except GetoptError, e:
- print >> sys.stderr, usage()
- exit(1)
- for option, argument in options:
- if option in ('-h', '--help'):
- print usage()
- exit(0)
- elif option in ('-n', '--note'):
- SETTINGS.noteTitle = argument
- else:
- assert False, 'unhandled option'
- if SETTINGS.noteTitle is None:
- print >> sys.stderr, usage()
- exit(1)
- def usage():
- '''Return text describing program usage and program arguments'''
- return \
- 'Usage: python pyTomboyRemind.py OPTIONS\n' \
- '\n' \
- 'Available OPTIONS:\n' \
- '\n' \
- '-h, --help show this information\n' \
- '-n, --note TITLE (required) specify a Tomboy note by title.\n' \
- '\n' \
- 'Program takes a Tomboy note specified in OPTIONS and returns corresponding\n' \
- 'text to be placed in a configuration file of conky somewhere after TEXT\n' \
- 'keyword. Here is an example configuration file for conky:\n' \
- '\n' \
- ' own_window no\n' \
- ' double_buffer yes\n' \
- ' text_buffer_size 1024\n' \
- ' \n' \
- ' use_xft yes\n' \
- ' xftfont sans serif:size=8\n' \
- ' \n' \
- ' color0 white # standard color\n' \
- ' \n' \
- ' color2 FFFF00 # important\n' \
- ' color3 00FF33 # prepare\n' \
- ' color4 33FFFF # freetime\n' \
- ' \n' \
- ' color5 FFFF99 # wait\n' \
- ' color6 FA87CE # hard\n' \
- ' color7 87CEFA # stats\n' \
- ' \n' \
- ' TEXT\n' \
- ' ${execp python /path/to/pyTomboyRemind.py -n Algebra\n' \
- '\n' \
- 'In your configuration file you should specify color variables. Here is the way\n' \
- 'they are used:\n' \
- '\n' \
- ' color0 - standard text color\n' \
- '\n' \
- ' color2 - text color for important tasks\n' \
- ' color3 - text color for tasks to prepare (e.g., tests at your college)\n' \
- ' color4 - text color that you will do at free time\n' \
- '\n' \
- ' color5 - text color for tasks that can not be completed at now\n' \
- ' color6 - text color for tasks that can not be completed at all :)\n' \
- ' color7 - text color for tasks that are rather statistics, not tasks :)\n' \
- '\n' \
- 'You may assign color to a task in a Tomboy note by placing an expression\n' \
- 'like \'[type=important]\' before task\'s contents (i.e.,\n' \
- '\'[type=prepare]Algebra test\'). Types correspond to colors:\n' \
- '\n' \
- ' type=important\n' \
- ' type=prepare\n' \
- ' type=freetime\n' \
- '\n' \
- ' type=wait\n' \
- ' type=hard\n' \
- ' type=stats\n'
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment