Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17772 > unrolled thread

Idiom for shelling out to $EDITOR/$PAGER?

Started byTim Chase <python.list@tim.thechases.com>
First post2011-12-22 22:16 -0600
Last post2011-12-23 09:59 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Idiom for shelling out to $EDITOR/$PAGER? Tim Chase <python.list@tim.thechases.com> - 2011-12-22 22:16 -0600
    Re: Idiom for shelling out to $EDITOR/$PAGER? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 09:59 +0000

#17772 — Idiom for shelling out to $EDITOR/$PAGER?

FromTim Chase <python.list@tim.thechases.com>
Date2011-12-22 22:16 -0600
SubjectIdiom for shelling out to $EDITOR/$PAGER?
Message-ID<mailman.4014.1324613800.27778.python-list@python.org>
After a little searching, I've not been able to come up with what 
I'd consider canonical examples of consider calling an external 
editor/pager on a file and reading the results back in.  (most of 
my results are swamped by people asking about editors written in 
Python, or what the best editors for Python code are)

The pseudocode would be something like

   def edit_text(data):
     temp_fname = generate_temp_name()
     try:
       f = file(temp_fname, 'w')
       f.write(data)
       f.close()
       before = info(temp_fname) # maybe stat+checksum?
       editor = find_sensible_editor()
       subprocess.call([editor, temp_fname])
       if before == info(temp_fname):
         return None
       else:
         return file(temp_fname).read()
     finally:
       delete_if_exists(temp_fname)

However there are things to watch out for in this lousy code:

-race conditions, unique naming, and permissions on the temp file

-proper & efficient detection of file-change, to know whether the 
user actually did anything

-cross-platform determination of a sensible editor (that blocks 
rather than spawns), using platform conventions like 
os.environ['EDITOR']

-cleanup deletion of the temp-file

I presume the code for spawning $PAGER on some content would look 
pretty similar.

Any good example code (or blog posts, or other links) that has 
been battle-tested?

Thanks,

-tkc






[toc] | [next] | [standalone]


#17790

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-12-23 09:59 +0000
Message-ID<4ef45117$0$29973$c3e8da3$5496439d@news.astraweb.com>
In reply to#17772
On Thu, 22 Dec 2011 22:16:30 -0600, Tim Chase wrote:

> I presume the code for spawning $PAGER on some content would look pretty
> similar.

Have a look at pydoc in the standard library, which implements pager-like 
functionality.


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web