annotate python/dmenu.py @ 289:e1a861bbb559

sample file picker
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 10 May 2013 14:25:29 -0700
parents
children 03d62a2cbe62
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
289
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 import optparse
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 import os
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 import subprocess
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import sys
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 def choose_file(directory, dmenu='dmenu'):
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 """choose a file in the directory with dmenu"""
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 directory = os.path.abspath(directory)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 files = os.listdir(directory)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 string = '\n'.join(files)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 process = subprocess.Popen([dmenu, '-i'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 stdout, _ = process.communicate(input=string)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 if process.returncode:
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 return
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 chosen = os.path.join(directory, stdout)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 if os.path.isdir(chosen):
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 return choose_file(chosen)
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 return chosen
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 def main(args=sys.argv[1:]):
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 parser = optparse.OptionParser()
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 print choose_file(os.getcwd())
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 if __name__ == '__main__':
e1a861bbb559 sample file picker
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 main()