Idiom for shelling out to $EDITOR/$PAGER?

Discussion in 'Python' started by Tim Chase, Dec 23, 2011.

  1. Tim Chase

    Tim Chase Guest

    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
    Tim Chase, Dec 23, 2011
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Dec 23, 2011
    #2
    1. Advertising

  3. On 2011-12-23 06:02:18 +0000, Cameron Simpson said:

    > MacOSX has "open", though it won't be running a blocking editor, alas.


    But it can be. From the man page:

    -t Causes the file to be opened with the default text editor, as deter-
    mined via LaunchServices

    and

    -W Causes open to wait until the applications it opens (or that were
    already open) have exited. Use with the -n flag to allow open to
    function as an appropriate app for the $EDITOR environment variable.

    and finally

    -n Open a new instance of the application(s) even if one is already run-
    ning.

    can be combined to make 'open' (the shell interface to OS X's
    LaunchServices system) into a workable $EDITOR.

    -o
    Owen Jacobson, Dec 24, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam

    shelling from .aspx

    sam, Nov 30, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    371
    Steve C. Orr [MVP, MCSD]
    Dec 1, 2003
  2. Stephen Miller
    Replies:
    1
    Views:
    475
    Stephen Miller
    May 13, 2004
  3. MirkoGeest
    Replies:
    0
    Views:
    472
    MirkoGeest
    May 29, 2006
  4. Rich

    shelling an app on server from asp?

    Rich, Nov 4, 2003, in forum: ASP General
    Replies:
    12
    Views:
    202
  5. Caio Chassot

    piping input when shelling out

    Caio Chassot, Sep 20, 2006, in forum: Ruby
    Replies:
    12
    Views:
    189
    Rick DeNatale
    Sep 21, 2006
Loading...

Share This Page