CrunchBang Linux Pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

CrunchBang Linux Pastebin

Posted by jmeb on Fri 15th Jul 19:19 (modification of post by view diff)
download | new post

  1. #! /usr/bin/env python
  2. #
  3. # A obpipemenu script for viewing a dir
  4. # with a few options
  5. #
  6.  
  7. import sys
  8. import os
  9. import re
  10.  
  11. ###
  12. # Global Variables
  13. ###
  14.  
  15. EXO = "exo-open"        #Opens file with default (XFCE)
  16. XDG = "xdg-open"        #Opens file with default (Openbox)
  17. PYPIPE = str(os.path.abspath(__file__)) + " "
  18.  
  19.    
  20. def recent_file_list(src):
  21.     ''' Return a list of full paths for files most recent first '''
  22.     files = []
  23.     os.chdir(src)
  24.     for fn in os.listdir(src):
  25.         #Create a tuple (time modified, filename path)
  26.         timename = os.path.getmtime(fn), os.path.abspath(fn)
  27.         files.append(timename)
  28.     # Sort the list by times, newest first
  29.     # To sort by oldest first, remove set reverse to False
  30.     recenttuples = sorted(files, reverse=True)
  31.     # Get the names only, in a new list
  32.     recents =  []
  33.     for t in recenttuples:
  34.       #Replace '&'s to avoid openbox xml problems
  35.       ampedpath = re.sub(r"&","&",t[1])
  36.       recents.append(ampedpath)
  37.     return recents
  38.  
  39. def printing(recents):
  40.     ''' Decide whether to print a file or menu listing '''
  41.     for f in recents:
  42.         if os.path.isdir(f):
  43.             print_dir(f,PYPIPE)
  44.         else:
  45.             print_file(f,XDG)
  46.  
  47. def print_file(fn,prg):
  48.     ''' Print a listing to open a file '''
  49.     print '<item label="%s">' % os.path.basename(fn)[:20]
  50.     print '\t<action name="Execute">'
  51.     print "\t\t<execute>%s '%s' </execute>" % (prg, fn)
  52.     print '\t</action>'
  53.     print '</item>'
  54.  
  55. def print_browse(fn,prg):
  56.     ''' Print a listing to browse current dir '''
  57.     print '<item label="Browse here...">'
  58.     print '\t<action name="Execute">'
  59.     print "\t\t<execute>%s '%s' </execute>" % (prg, fn)
  60.     print '\t</action>'
  61.     print '</item>'
  62.  
  63. def print_dir(src,pypipe):
  64.     ''' Print a listing for directory menu '''
  65.     #Escape whitespace for directory names
  66.     slashedpath = re.sub(r"\s","\ ",src)
  67.     print '<menu id="%s" label="%s..." execute="%s" />' % (src,
  68.                                 os.path.basename(src)[:20],pypipe + slashedpath)
  69.  
  70. def main():
  71.     src = os.path.abspath(sys.argv[1])
  72.     recents = recent_file_list(src)
  73.     print "<openbox_pipe_menu>"
  74.     print_browse(src,XDG)
  75.     printing(recents)
  76.     print "</openbox_pipe_menu>"
  77.  
  78. if __name__ == '__main__':
  79.     main()

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me