Posted by jmeb on Fri 15th Jul 19:19 (modification of post by view diff)
download | new post
- #! /usr/bin/env python
- #
- # A obpipemenu script for viewing a dir
- # with a few options
- #
- import sys
- import os
- import re
- ###
- # Global Variables
- ###
- EXO = "exo-open" #Opens file with default (XFCE)
- XDG = "xdg-open" #Opens file with default (Openbox)
- PYPIPE = str(os.path.abspath(__file__)) + " "
- def recent_file_list(src):
- ''' Return a list of full paths for files most recent first '''
- files = []
- os.chdir(src)
- for fn in os.listdir(src):
- #Create a tuple (time modified, filename path)
- timename = os.path.getmtime(fn), os.path.abspath(fn)
- files.append(timename)
- # Sort the list by times, newest first
- # To sort by oldest first, remove set reverse to False
- recenttuples = sorted(files, reverse=True)
- # Get the names only, in a new list
- recents = []
- for t in recenttuples:
- #Replace '&'s to avoid openbox xml problems
- ampedpath = re.sub(r"&","&",t[1])
- recents.append(ampedpath)
- return recents
- def printing(recents):
- ''' Decide whether to print a file or menu listing '''
- for f in recents:
- if os.path.isdir(f):
- print_dir(f,PYPIPE)
- else:
- print_file(f,XDG)
- def print_file(fn,prg):
- ''' Print a listing to open a file '''
- print '<item label="%s">' % os.path.basename(fn)[:20]
- print '\t<action name="Execute">'
- print "\t\t<execute>%s '%s' </execute>" % (prg, fn)
- print '\t</action>'
- print '</item>'
- def print_browse(fn,prg):
- ''' Print a listing to browse current dir '''
- print '<item label="Browse here...">'
- print '\t<action name="Execute">'
- print "\t\t<execute>%s '%s' </execute>" % (prg, fn)
- print '\t</action>'
- print '</item>'
- def print_dir(src,pypipe):
- ''' Print a listing for directory menu '''
- #Escape whitespace for directory names
- slashedpath = re.sub(r"\s","\ ",src)
- print '<menu id="%s" label="%s..." execute="%s" />' % (src,
- os.path.basename(src)[:20],pypipe + slashedpath)
- def main():
- src = os.path.abspath(sys.argv[1])
- recents = recent_file_list(src)
- print "<openbox_pipe_menu>"
- print_browse(src,XDG)
- printing(recents)
- print "</openbox_pipe_menu>"
- if __name__ == '__main__':
- 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.