annotate python/tmpbuffer.py @ 694:ebca6d85213a

File "/usr/lib/python3/dist-packages/IPython/config/__init__.py", line 16, in <module> from .application import * File "/usr/lib/python3/dist-packages/IPython/config/application.py", line 31, in <module> from IPython.config.configurable import SingletonConfigurable File "/usr/lib/python3/dist-packages/IPython/config/configurable.py", line 33, in <module> from IPython.utils.text import indent, wrap_paragraphs File "/usr/lib/python3/dist-packages/IPython/utils/text.py", line 28, in <module> from IPython.external.path import path File "/usr/lib/python3/dist-packages/IPython/external/path/__init__.py", line 2, in <module> from path import * File "/home/jhammel/python/path.py", line 25 print root(path) ^
author Jeff Hammel <k0scist@gmail.com>
date Wed, 09 Jul 2014 16:26:49 -0700
parents e07b99607d27
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