Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'received:edu.au': 0.07; 'python': 0.08; 'deletion': 0.09; 'finally:': 0.09; 'presume': 0.09; 'to:addr:python.list': 0.09; 'to:addr:tim.thechases.com': 0.09; 'to:name:tim chase': 0.09; 'def': 0.13; 'blocking': 0.13; "'w')": 0.16; 'editor,': 0.16; 'f.close()': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'had,': 0.16; 'hmm.': 0.16; 'macosx': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'name).': 0.16; 'naming,': 0.16; 'received:202.125.174': 0.16; 'received:202.125.174.133': 0.16; 'received:boardofstudies.nsw.edu.au': 0.16; 'received:cskk.homeip.net': 0.16; 'received:harvey.boardofstudies.nsw.edu.au': 0.16; 'received:homeip.net': 0.16; 'received:nsw.edu.au': 0.16; 'solves': 0.16; 'temp': 0.16; 'cc:addr:python-list': 0.16; 'examples': 0.16; 'wrote:': 0.18; 'written': 0.20; 'cheers,': 0.20; '(most': 0.21; 'maybe': 0.21; 'stuff': 0.22; '(or': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'mode': 0.25; 'code': 0.25; 'asking': 0.28; 'exit': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'example': 0.29; 'posts,': 0.30; '(the': 0.30; 'subject:?': 0.31; "i've": 0.31; 'like.': 0.32; 'pretty': 0.32; "won't": 0.33; 'header:User-Agent:1': 0.33; 'actually': 0.33; 'rather': 0.33; 'there': 0.33; '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; 'external': 0.35; 'file': 0.36; 'received:au': 0.36; '...': 0.36; 'none': 0.37; 'charset:us-ascii': 0.37; 'but': 0.37; 'using': 0.38; 'some': 0.38; 'skip:o 20': 0.38; "i'd": 0.39; 'called': 0.40; 'platforms': 0.40; 'unique': 0.61; 'your': 0.61; 'efficient': 0.62; 'back': 0.62; 'results': 0.63; 'watch': 0.65; 'received:202': 0.66; 'cameron': 0.73; 'are)': 0.84; 'up;': 0.84; 'canonical': 0.91; 'superb': 0.91; 'conditions,': 0.93; 'scenario': 0.93; 'subject: $': 0.97 Date: Fri, 23 Dec 2011 17:02:18 +1100 From: Cameron Simpson To: Tim Chase Subject: Re: Idiom for shelling out to $EDITOR/$PAGER? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4EF4009E.8010109@tim.thechases.com> User-Agent: Mutt/1.5.21 (2010-09-15) References: <4EF4009E.8010109@tim.thechases.com> Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324620143 news.xs4all.nl 6880 [2001:888:2000:d::a6]:58451 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17777 On 22Dec2011 22:16, 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 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 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