tasklist.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env python2
  2. #coding: UTF-8
  3. #
  4. # Author: Pedro Lobo <palobo@archlinux.us>
  5. # License: WTFPL 2.0
  6. #
  7. #######################################################################
  8. #
  9. # A simple Tasklist for Openbox and Conky. Displays tasks in OpenBox
  10. # pipe menu. Clicking on a task renders it complete and therefore
  11. # removes it from the list. # Tasks can also be displayed on your
  12. # Desktop via Conky
  13. #
  14. # For more details and instructions chech the wiki at:
  15. # http://bitbucket.org/palobo/simpletasks/wiki/Home
  16. #
  17. import sys
  18. import os
  19. import getopt
  20. import pickle
  21. ### Set some needed variables in order for the script to function
  22. tasklistpath = '/home/eddie/pipes/tasklist/' # Place location to store taskfile here.
  23. conky_title = 'Tasks' # Default title for your conky tasks.
  24. conky_max_tasks = 10 # Deafault max number of tasks to display at a time in conky.
  25. # tags = ['Important', 'Normal', 'Low'] # Array with possible tags to apply. Future feature maybe...
  26. ### Start working now
  27. tasklist = []
  28. def main(argv):
  29. """Reads arguments and options and executes accordingly"""
  30. global conky_title
  31. global conky_max_tasks
  32. action = "menu" # Define default action
  33. file = '.simpletasks' # Define default task list file
  34. task = ""
  35. try:
  36. opts, args = getopt.getopt(argv, "f:a:i:t:m:", [])
  37. except getopt.GetoptError:
  38. usage()
  39. sys.exit(2)
  40. for opt, arg in opts:
  41. if opt == '-f':
  42. file = arg # Task file other than default
  43. elif opt == '-a':
  44. action = arg # Action other than default. Possible correct values are menu, conky, del and new
  45. elif opt == '-i':
  46. task = arg # Task index, to be used in conjunction with del action
  47. elif opt == '-t':
  48. conky_title = arg # Title for conky tasks
  49. elif opt == '-m':
  50. conky_max_tasks = arg # Max number of tasks to display in conky
  51. if action == "menu":
  52. # Prints menu structure
  53. print_menu(file)
  54. elif action == "conky":
  55. # Prints conky structure
  56. print_conky(file, conky_title, conky_max_tasks)
  57. elif action == "new":
  58. # Adds new task
  59. add_task(file)
  60. elif action == "del":
  61. # Deletes task
  62. if task != "":
  63. del_task(file, task)
  64. else:
  65. sys.exit(2)
  66. def usage():
  67. """Print usage message"""
  68. pass
  69. def open_file(tf):
  70. """Unserialize info in file and load it into tasklist"""
  71. # Read back from the storage
  72. global tasklist
  73. global taskfile
  74. taskfile = os.path.join(tasklistpath, tf)
  75. if not os.path.exists(taskfile):
  76. open(taskfile, 'wb').close()
  77. tasklist = []
  78. else:
  79. f = open(taskfile, 'rb')
  80. try:
  81. tasklist = pickle.load(f)
  82. except EOFError:
  83. tasklist = []
  84. f.close()
  85. def save_file(tf):
  86. """Serialize and save info into tasklistfile"""
  87. global tasklist
  88. global taskfile
  89. f = open(taskfile, 'wb')
  90. pickle.dump(tasklist, f)
  91. f.close()
  92. def add_task(tf):
  93. """Add a new task"""
  94. global tasklist
  95. open_file(tf) # Read current tasklist file
  96. task = raw_input('New Task: ')
  97. tasklist.append(task) # Append new task to tasklist
  98. save_file(tf) # Save tasklist to file
  99. def del_task(tf, task):
  100. """Delete task that was clicked"""
  101. global tasklist
  102. open_file(tf)
  103. del tasklist[int(task)]
  104. save_file(tf)
  105. def print_menu(tf):
  106. """Print the pipe menu structure"""
  107. open_file(tf)
  108. print '<openbox_pipe_menu>'
  109. print '<item label="New Task">'
  110. print '<action name="Execute">'
  111. print '<command>%s</command>' % ('xterm -geometry 100x5+100+100 -e "python2 ~/pipes/tasklist.py -a new -f %s"' % tf)
  112. print '</action>'
  113. print '</item>'
  114. print '<separator />'
  115. i = 0 # Var for tasklist index needed for del_task()
  116. for t in tasklist:
  117. print '<item label="%s">' % t
  118. print '<action name="Execute">'
  119. print '<command>%s</command>' % ('python2 ~/pipes/tasklist.py -f %s -a del -i %u ' % (tf, i))
  120. print '</action>'
  121. print '</item>'
  122. i += 1
  123. print '</openbox_pipe_menu>'
  124. def print_conky(tf, title, max_tasks):
  125. """Print output for conky display"""
  126. open_file(tf)
  127. # Customize your conky output here. Change font, size, color etc.
  128. print '${font Radio Space Bold:size=15}${color 659fdb} %s' % title
  129. print '${voffset -10}${color 999999}${hr}'
  130. t = iter(tasklist)
  131. i = 0
  132. while i <= max_tasks:
  133. try:
  134. # Here you may customize how the indivual tasks will appear. Maybe place a bullet, diferent color, you choose.
  135. print '${font}» %s' % t.next()
  136. except StopIteration:
  137. break
  138. if __name__ == "__main__":
  139. main(sys.argv[1:])