Guest User

Untitled

a guest
May 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #
  5. # pyTomboyRemind.py
  6. #
  7. # For description please see usage() function
  8. #
  9. # REQUIREMENTS:
  10. # python-dbus
  11. # python-lxml
  12. #
  13.  
  14. import sys
  15. from getopt import getopt, GetoptError
  16. import dbus
  17. from lxml import etree
  18. from cStringIO import StringIO
  19.  
  20.  
  21. class SETTINGS:
  22. '''Stores global script settings'''
  23.  
  24. noteTitle = None
  25.  
  26. PADDING_INCREMENT = ' '
  27. PADDING_INCREMENT_LEN = 3
  28.  
  29.  
  30.  
  31. def main():
  32. '''The main function'''
  33. parseCommandLine()
  34.  
  35. obj = dbus.SessionBus().get_object("org.gnome.Tomboy", "/org/gnome/Tomboy/RemoteControl")
  36. tomboy = dbus.Interface(obj, "org.gnome.Tomboy.RemoteControl")
  37.  
  38. noteUri = tomboy.FindNote(SETTINGS.noteTitle)
  39. if not noteUri:
  40. print >> sys.stderr, 'No notes with title\'' + SETTINGS.noteTitle + '\''
  41. exit(1)
  42.  
  43. padding = ''
  44. firstNode = True # to handle note title
  45.  
  46. xml = tomboy.GetNoteContentsXml(noteUri).encode('utf-8')
  47. print >> sys.stderr, xml
  48. for event, node in etree.iterparse(StringIO(xml), events=('start', 'end')):
  49. if node.tag == 'list':
  50.  
  51. if event == 'start':
  52. padding += SETTINGS.PADDING_INCREMENT
  53. elif event == 'end':
  54. padding = padding[0:-SETTINGS.PADDING_INCREMENT_LEN]
  55. if node.tail:
  56. printText(node.tail.encode('utf-8'), padding)
  57.  
  58. else:
  59. if event == 'start':
  60.  
  61. if firstNode:
  62. firstNode = False
  63. text = node.text.encode('utf-8')[len(SETTINGS.noteTitle) + 2:] # 2 = number of \n
  64. printText(text, padding)
  65. else:
  66. printText(node.text.encode('utf-8'), padding)
  67.  
  68.  
  69. def printText(text, padding):
  70. text = text.strip('\n')
  71.  
  72. if text.startswith('[type='):
  73. a = text.find(']', 6) # 6 = len('[type=')
  74. taskType = text[6:a]
  75.  
  76. if taskType == 'important':
  77. text = '${color2}' + text[a+1:] + '${color0}'
  78. elif taskType == 'prepare':
  79. text = '${color3}' + text[a+1:] + '${color0}'
  80. elif taskType == 'freetime':
  81. text = '${color4}' + text[a+1:] + '${color0}'
  82.  
  83. elif taskType == 'wait':
  84. text = '${color5}' + text[a+1:] + '${color0}'
  85. elif taskType == 'hard':
  86. text = '${color6}' + text[a+1:] + '${color0}'
  87. elif taskType == 'stats':
  88. text = '${color7}' + text[a+1:] + '${color0}'
  89.  
  90. else:
  91. print >> sys.stderr, 'Unknown task type: \'' + taskType + '\''
  92.  
  93. print padding + text
  94.  
  95.  
  96. def parseCommandLine():
  97. '''Parse command line arguments and fill SETTINGS.
  98.  
  99. Calls exit() if an error occurs.
  100. '''
  101. try:
  102. options, arguments = getopt(sys.argv[1:], 'hn:', ['help', 'note='])
  103. except GetoptError, e:
  104. print >> sys.stderr, usage()
  105. exit(1)
  106.  
  107. for option, argument in options:
  108. if option in ('-h', '--help'):
  109. print usage()
  110. exit(0)
  111. elif option in ('-n', '--note'):
  112. SETTINGS.noteTitle = argument
  113. else:
  114. assert False, 'unhandled option'
  115.  
  116. if SETTINGS.noteTitle is None:
  117. print >> sys.stderr, usage()
  118. exit(1)
  119.  
  120.  
  121. def usage():
  122. '''Return text describing program usage and program arguments'''
  123. return \
  124. 'Usage: python pyTomboyRemind.py OPTIONS\n' \
  125. '\n' \
  126. 'Available OPTIONS:\n' \
  127. '\n' \
  128. '-h, --help show this information\n' \
  129. '-n, --note TITLE (required) specify a Tomboy note by title.\n' \
  130. '\n' \
  131. 'Program takes a Tomboy note specified in OPTIONS and returns corresponding\n' \
  132. 'text to be placed in a configuration file of conky somewhere after TEXT\n' \
  133. 'keyword. Here is an example configuration file for conky:\n' \
  134. '\n' \
  135. ' own_window no\n' \
  136. ' double_buffer yes\n' \
  137. ' text_buffer_size 1024\n' \
  138. ' \n' \
  139. ' use_xft yes\n' \
  140. ' xftfont sans serif:size=8\n' \
  141. ' \n' \
  142. ' color0 white # standard color\n' \
  143. ' \n' \
  144. ' color2 FFFF00 # important\n' \
  145. ' color3 00FF33 # prepare\n' \
  146. ' color4 33FFFF # freetime\n' \
  147. ' \n' \
  148. ' color5 FFFF99 # wait\n' \
  149. ' color6 FA87CE # hard\n' \
  150. ' color7 87CEFA # stats\n' \
  151. ' \n' \
  152. ' TEXT\n' \
  153. ' ${execp python /path/to/pyTomboyRemind.py -n Algebra\n' \
  154. '\n' \
  155. 'In your configuration file you should specify color variables. Here is the way\n' \
  156. 'they are used:\n' \
  157. '\n' \
  158. ' color0 - standard text color\n' \
  159. '\n' \
  160. ' color2 - text color for important tasks\n' \
  161. ' color3 - text color for tasks to prepare (e.g., tests at your college)\n' \
  162. ' color4 - text color that you will do at free time\n' \
  163. '\n' \
  164. ' color5 - text color for tasks that can not be completed at now\n' \
  165. ' color6 - text color for tasks that can not be completed at all :)\n' \
  166. ' color7 - text color for tasks that are rather statistics, not tasks :)\n' \
  167. '\n' \
  168. 'You may assign color to a task in a Tomboy note by placing an expression\n' \
  169. 'like \'[type=important]\' before task\'s contents (i.e.,\n' \
  170. '\'[type=prepare]Algebra test\'). Types correspond to colors:\n' \
  171. '\n' \
  172. ' type=important\n' \
  173. ' type=prepare\n' \
  174. ' type=freetime\n' \
  175. '\n' \
  176. ' type=wait\n' \
  177. ' type=hard\n' \
  178. ' type=stats\n'
  179.  
  180. if __name__ == '__main__':
  181. main()
Add Comment
Please, Sign In to add comment