Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17780
| 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!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <jeanpierreda@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'else:': 0.03; 'permissions': 0.04; 'option,': 0.07; 'python': 0.08; 'context.': 0.09; 'deletion': 0.09; 'finally:': 0.09; 'presume': 0.09; 'am,': 0.12; 'def': 0.13; 'blocking': 0.13; "'w')": 0.16; 'editor,': 0.16; 'f.close()': 0.16; 'had,': 0.16; 'hmm.': 0.16; 'macosx': 0.16; 'name).': 0.16; 'naming,': 0.16; 'solves': 0.16; 'temp': 0.16; '\xc2\xa0-': 0.16; 'cc:addr:python-list': 0.16; 'examples': 0.16; 'linux': 0.17; 'wrote:': 0.18; 'file,': 0.19; 'written': 0.20; 'cheers,': 0.20; 'maybe': 0.21; 'dec': 0.22; 'stuff': 0.22; '(or': 0.22; 'header:In-Reply-To:1': 0.22; 'figure': 0.23; 'mode': 0.25; 'code': 0.25; 'asking': 0.28; 'url:mailman': 0.28; 'message-id:@mail.gmail.com': 0.28; 'exit': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'example': 0.29; 'closing': 0.30; 'posts,': 0.30; 'skip:\xc2 20': 0.30; '(the': 0.30; 'least': 0.30; '(as': 0.31; 'subject:?': 0.31; "i've": 0.31; 'like.': 0.32; 'url:listinfo': 0.32; 'pretty': 0.32; "isn't": 0.33; "won't": 0.33; 'actually': 0.33; 'rather': 0.33; 'there': 0.33; 'received:209.85.160': 0.33; 'fri,': 0.34; 'things': 0.34; 'calling': 0.34; 'anything': 0.34; 'conventions': 0.34; 'executable': 0.34; 'parse': 0.34; 'try:': 0.34; 'running': 0.35; 'something': 0.35; 'received:209.85.160.46': 0.35; 'received :mail-pw0-f46.google.com': 0.35; 'external': 0.35; 'url:python': 0.36; 'file': 0.36; 'cc:2**1': 0.36; 'none': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'useful': 0.38; 'skip:o 20': 0.38; "i'd": 0.39; 'url:org': 0.39; 'being': 0.39; "couldn't": 0.39; 'called': 0.40; 'received:209': 0.40; 'platforms': 0.40; 'unique': 0.61; '2011': 0.61; 'your': 0.61; 'efficient': 0.62; 'back': 0.62; 'results': 0.63; 'opened': 0.64; 'watch': 0.65; 'cameron': 0.73; 'race': 0.77; 'are)': 0.84; 'up;': 0.84; 'canonical': 0.91; 'superb': 0.91; 'conditions,': 0.93; 'scenario': 0.93; 'subject: $': 0.97 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=J+vbiXoq2EwHYC86eoFQGzPI/UfxaID0gs/yaGM/dsE=; b=LtWjJsIpjtHkQgcnIUDVnpnQk/C7Bi76BqUCC4IxAhJcMdUewQaoq+WcaRjySIAfms n9CsVPideR8sUC/g3KJCBwnq93P/4UYDm1RekhKmSzffHmRbOrN6Hf2mOH0QpYlcO08/ i2ycqi1wCDNbIS0mZRiakmFo1aXjO//LVYFqc= |
| MIME-Version | 1.0 |
| In-Reply-To | <20111223060218.GA28472@cskk.homeip.net> |
| References | <4EF4009E.8010109@tim.thechases.com> <20111223060218.GA28472@cskk.homeip.net> |
| From | Devin Jeanpierre <jeanpierreda@gmail.com> |
| Date | Fri, 23 Dec 2011 02:21:01 -0500 |
| Subject | Re: Idiom for shelling out to $EDITOR/$PAGER? |
| To | Cameron Simpson <cs@zip.com.au> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | Python <python-list@python.org> |
| 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.4019.1324624912.27778.python-list@python.org> (permalink) |
| Lines | 101 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1324624912 news.xs4all.nl 6906 [2001:888:2000:d::a6]:39983 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:17780 |
Show key headers only | View raw
> Anyway, look it up; it has an autodelete mode etc.
The autodelete isn't useful in this context. The file needs to be
closed and then reopened again; with the autodelete option, the
closing would delete the file, preventing it from being opened by the
text editor or reopened to check contents.
(As far as I know, the race condition inherent in this is unavoidable.
At least on windows, but I couldn't figure out any other way on Linux
either.)
-- Devin
On Fri, Dec 23, 2011 at 1:02 AM, Cameron Simpson <cs@zip.com.au> wrote:
> On 22Dec2011 22:16, Tim Chase <python.list@tim.thechases.com> 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
>
> NamedTemporaryFile is your friend.
>
> | -proper & efficient detection of file-change, to know whether the
> | user actually did anything
>
> Wait for the editor to exit?
> In that scenario I go for:
> - wait for edit to exit
> - if exit status 0 and file non-empty, trust it
> (subject to parse issues afterwards of course)
>
> | -cross-platform determination of a sensible editor (that blocks
> | rather than spawns), using platform conventions like
> | os.environ['EDITOR']
>
> os.environment.get('EDITOR', 'vi')?
>
> Some platforms have an executable called "editor" that solves that
> problem (absent $EDITOR); MacOSX has "open", though it won't be running
> a blocking editor, alas.
>
> You may need some knowledge of the local system. On a terminal? Easy,
> use $EDITOR. No terminal, but $DISPLAY? xterm -e "$EDITOR temp_file",
> or the like. No terminal, no $DISPLAY, macosx? I have an incantation
> somewhere...
>
> | -cleanup deletion of the temp-file
>
> NamedTemporaryFile is your friend.
>
> | I presume the code for spawning $PAGER on some content would look
> | pretty similar.
>
> Yep.
>
> | Any good example code (or blog posts, or other links) that has been
> | battle-tested?
>
> Hmm. Nothing directly to had, but regarding the temp file:
>
> # write contents of the file `fp` into a temp file
> with NamedTemporaryFile('w', dir=os.path.join(self.dir, 'tmp')) as T:
> T.write(fp.read())
> ... do stuff with T.name (the temp file name).
>
> Anyway, look it up; it has an autodelete mode etc.
>
> Cheers,
> --
> Cameron Simpson <cs@zip.com.au> DoD#743
> http://www.cskk.ezoshosting.com/cs/
>
> I have always been a welly man myself. They are superb in wet grass, let
> alone lagoons of pig shit. - Julian Macassey
> --
> http://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Idiom for shelling out to $EDITOR/$PAGER? Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-23 02:21 -0500
csiph-web