annotate python/tmpbuffer.py @ 924:fc88a12acf48 default tip

add lfs filter
author Jeff Hammel <k0scist@gmail.com>
date Wed, 04 Dec 2024 15:50:53 -0800
parents b8510769d001
children
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)
859
b8510769d001 py3 compat
Jeff Hammel <k0scist@gmail.com>
parents: 542
diff changeset
21 buffer = open(tmpfile).read().strip()
276
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()
859
b8510769d001 py3 compat
Jeff Hammel <k0scist@gmail.com>
parents: 542
diff changeset
28 print (contents)
276
dc20e1fa93d0 add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29