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


Groups > comp.lang.python > #17781

Re: Idiom for shelling out to $EDITOR/$PAGER?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'else:': 0.03; 'permissions': 0.04; 'url:py': 0.07; 'python': 0.08; 'deletion': 0.09; 'finally:': 0.09; 'mercurial': 0.09; 'presume': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.13; "'w')": 0.16; 'calculation': 0.16; 'f.close()': 0.16; 'naming,': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'temp': 0.16; 'url:hg': 0.16; 'examples': 0.16; 'wrote:': 0.18; 'written': 0.20; 'memory': 0.21; '(most': 0.21; 'commit': 0.21; 'maybe': 0.21; '(or': 0.22; 'from:addr:web.de': 0.23; 'suggests': 0.23; 'string': 0.24; 'messages.': 0.25; 'code': 0.25; 'asking': 0.28; 'compare': 0.28; 'example': 0.29; 'posts,': 0.30; 'subject:?': 0.31; "i've": 0.31; 'quite': 0.32; 'does': 0.32; 'pretty': 0.32; 'actually': 0.33; 'rather': 0.33; 'header:X -Complaints-To:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'things': 0.34; 'calling': 0.34; 'anything': 0.34; 'conventions': 0.34; 'try:': 0.34; 'something': 0.35; 'external': 0.35; 'file': 0.36; 'data.': 0.36; 'uses': 0.36; 'none': 0.37; 'could': 0.37; 'using': 0.38; 'received:org': 0.38; 'some': 0.38; 'skip:o 20': 0.38; "i'd": 0.39; 'to:addr:python.org': 0.40; 'unique': 0.61; 'quick': 0.61; 'your': 0.61; 'efficient': 0.62; 'back': 0.62; 'results': 0.63; 'watch': 0.65; 'are)': 0.84; 'method)': 0.84; 'worthwhile.': 0.84; 'canonical': 0.91; 'conditions,': 0.93; 'subject: $': 0.97
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Idiom for shelling out to $EDITOR/$PAGER?
Followup-To gmane.comp.python.general
Date Fri, 23 Dec 2011 09:28:52 +0100
Organization None
References <4EF4009E.8010109@tim.thechases.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a50e.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4021.1324628949.27778.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1324628949 news.xs4all.nl 6843 [2001:888:2000:d::a6]:36347
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17781

Followups directed to: gmane.comp.python.general

Show key headers only | View raw


Tim Chase wrote:

> 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

Just read the whole thing back into memory and compare the string to the 
original data. The file has to be quite long for the checksum calculation to 
be worthwhile.

> -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?

You could look into mercurial to see what it does to let you edit its commit 
messages. A quick look into mercurial/ui.py 
(http://selenic.com/hg/file/9cf1620e1e75/mercurial/ui.py, start with the 
ui.edit() method) suggests that it uses the same approach as your 
pseudocode.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Idiom for shelling out to $EDITOR/$PAGER? Peter Otten <__peter__@web.de> - 2011-12-23 09:28 +0100
  Re: Idiom for shelling out to $EDITOR/$PAGER? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 09:44 +0000
    Re: Idiom for shelling out to $EDITOR/$PAGER? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-23 09:47 +0000

csiph-web