annotate python/tmpbuffer.py @ 542:e07b99607d27

python/tmpbuffer.py
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 28 Sep 2013 19:56:30 -0700
parents dc20e1fa93d0
children b8510769d001
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
276
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 # from http://k0s.org/hg/bitsyblog/file/5c04cf601aba/bitsyblog/blogme.py
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4
542
e07b99607d27 python/tmpbuffer.py
Jeff Hammel <jhammel@mozilla.com>
parents: 276
diff changeset
5 import optparse
276
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import os
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import subprocess
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import tempfile
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 def tmpbuffer(editor=None):
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 """open an editor and retreive the resulting editted buffer"""
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 if not editor:
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 editor = os.environ.get('EDITOR')
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 if not editor:
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 raise Exception("tmpbuffer: editor not supplied and EDITOR not defined")
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 tmpfile = tempfile.mktemp(suffix='.txt')
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 cmdline = editor.split() # XXX shlex would be more powerful
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 cmdline.append(tmpfile)
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 edit = subprocess.call(cmdline)
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 buffer = file(tmpfile).read().strip()
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 os.remove(tmpfile)
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 return buffer
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 if __name__ == '__main__':
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 # purely for testing/illustration purposes
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 contents = tmpbuffer()
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 print contents
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29