Mercurial > hg > config
comparison python/tmpbuffer.py @ 276:dc20e1fa93d0
add a thing to make a temporary buffer
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Thu, 18 Apr 2013 16:14:09 -0700 |
| parents | |
| children | e07b99607d27 |
comparison
equal
deleted
inserted
replaced
| 275:7ffc6b1821f8 | 276:dc20e1fa93d0 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 # from http://k0s.org/hg/bitsyblog/file/5c04cf601aba/bitsyblog/blogme.py | |
| 4 | |
| 5 import os | |
| 6 import subprocess | |
| 7 import tempfile | |
| 8 | |
| 9 def tmpbuffer(editor=None): | |
| 10 """open an editor and retreive the resulting editted buffer""" | |
| 11 | |
| 12 if not editor: | |
| 13 editor = os.environ.get('EDITOR') | |
| 14 if not editor: | |
| 15 raise Exception("tmpbuffer: editor not supplied and EDITOR not defined") | |
| 16 tmpfile = tempfile.mktemp(suffix='.txt') | |
| 17 cmdline = editor.split() # XXX shlex would be more powerful | |
| 18 cmdline.append(tmpfile) | |
| 19 edit = subprocess.call(cmdline) | |
| 20 buffer = file(tmpfile).read().strip() | |
| 21 os.remove(tmpfile) | |
| 22 return buffer | |
| 23 | |
| 24 if __name__ == '__main__': | |
| 25 # purely for testing/illustration purposes | |
| 26 contents = tmpbuffer() | |
| 27 print contents | |
| 28 |
