#! /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 '' % os.path.basename(fn)[:20] print '\t' print "\t\t%s '%s' " % (prg, fn) print '\t' print '' def print_browse(fn,prg): ''' Print a listing to browse current dir ''' print '' print '\t' print "\t\t%s '%s' " % (prg, fn) print '\t' print '' def print_dir(src,pypipe): ''' Print a listing for directory menu ''' #Escape whitespace for directory names slashedpath = re.sub(r"\s","\ ",src) print '' % (src, os.path.basename(src)[:20],pypipe + slashedpath) def main(): src = os.path.abspath(sys.argv[1]) recents = recent_file_list(src) print "" print_browse(src,XDG) printing(recents) print "" if __name__ == '__main__': main()