view python/dmenu.py @ 333:998d3c276b25

http://askubuntu.com/questions/168117/how-to-automatically-add-meta-tags-to-screenshot?rq=1
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 18 Jun 2013 14:44:14 -0700
parents 618db1b1f34b
children
line wrap: on
line source

#!/usr/bin/env python

import optparse
import os
import shlex
import subprocess
import sys

def choose_file(directory, dmenu='dmenu',
                args=('-i', '-nb', 'black', '-nf', 'white',
                      '-fn', '-*-lucidatypewriter-medium-r-*-*-*-120-*-*-*-*-*-*')):
    """choose a file in the directory with dmenu"""
    directory = os.path.abspath(directory)
    files = os.listdir(directory)
    string = '\n'.join(files)

    if isinstance(dmenu, basestring):
        dmenu = [dmenu]
    dmenu = list(dmenu)
    dmenu.extend(args)

    process = subprocess.Popen(dmenu, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout, _ = process.communicate(input=string)
    if process.returncode:
        return
    chosen = os.path.join(directory, stdout)
    if os.path.isdir(chosen):
        return choose_file(chosen)
    return chosen

def main(args=sys.argv[1:]):
    parser = optparse.OptionParser()
    parser.add_option('-d', '--directory', dest='directory',
                      default=os.getcwd(),
                      help="call on this directory [Default: current directory]")
    parser.add_option('-e', '--exec', dest='executable',
                      help="call this proram with the result")
    options, args = parser.parse_args(args)
    chosen =  choose_file(options.directory)
    if chosen:
        if options.executable:
            # TODO: extract this pattern (see ims journal.txt)
            command = shlex.split(options.executable)
            executable = command[0]
            command.append(chosen)
            os.execlp(executable, *command)
        else:
            print chosen
    else:
        sys.exit(1)

if __name__ == '__main__':
    main()