Advertisement
Guest User

FVWM Tasklist for Menu

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