Mercurial > hg > config
annotate python/tmpbuffer.py @ 895:8d3df8c0c730
wordstream is a requirement
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 13 Aug 2021 15:16:21 -0700 |
parents | b8510769d001 |
children |
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 | 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 | 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 | 28 print (contents) |
276
dc20e1fa93d0
add a thing to make a temporary buffer
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 |