Mercurial > hg > config
annotate python/tmpbuffer.py @ 288:498210b6e1a9
add branches alias
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 10 May 2013 12:36:13 -0700 |
parents | dc20e1fa93d0 |
children | e07b99607d27 |
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 |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 import os |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 import subprocess |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 import tempfile |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 def tmpbuffer(editor=None): |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 """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
|
11 |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 if not editor: |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 editor = os.environ.get('EDITOR') |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 if not editor: |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 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
|
16 tmpfile = tempfile.mktemp(suffix='.txt') |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 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
|
18 cmdline.append(tmpfile) |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 edit = subprocess.call(cmdline) |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 buffer = file(tmpfile).read().strip() |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 os.remove(tmpfile) |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 return buffer |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 if __name__ == '__main__': |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 # purely for testing/illustration purposes |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 contents = tmpbuffer() |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 print contents |
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 |