Eigenstate / vmd-python

Installable VMD as a python module
Other
132 stars 25 forks source link

Squelching vmd output to stdout #12

Closed RasmusFonseca closed 6 years ago

RasmusFonseca commented 6 years ago

When calling molecule.load and molecule.read VMD prints info to stdout:

netcdfplugin) conventions: 'AMBER'
netcdfplugin) trajectory follows AMBER conventions version '1.0'
netcdfplugin) AMBER: program 'cpptraj'
netcdfplugin) AMBER: program version 'V15.0'
netcdfplugin) AMBER: title 'Cpptraj Generated trajectory'
netcdfplugin) AMBER: application 'AMBER'
netcdfplugin) AMBER: spatial dimension: 3
netcdfplugin) AMBER: atom dimension: 54849
netcdfplugin) AMBER: frame dimension: 421
netcdfplugin) AMBER: coordinates units: 'angstrom'
netcdfplugin) AMBER: no coordinates scalefactor attribute, 1.0 assumed
netcdfplugin) AMBER: coordinates scalefactor: 1.000000
netcdfplugin) AMBER trajectory contains periodic cell information
netcdfplugin) AMBER: cell lengths units: 'angstrom'
netcdfplugin) AMBER: no cell lengths scalefactor attribute, 1.0 assumed
netcdfplugin) AMBER: cell lengths scalefactor: 1.000000
netcdfplugin) AMBER: cell angles units: 'degree'
netcdfplugin) AMBER: no cell angles scalefactor attribute, 1.0 assumed
netcdfplugin) AMBER: cell angles scalefactor: 1.000000

Is there a way to squelch this output?

jvermaas commented 6 years ago

Not really. If there are things I want to see printed in the middle of a bunch of file loads, I'll prepend my print with PY, and grep for it in the log afterward. You could of course also just pipe the output to /dev/null, but this has knock-on effects if you are printing something yourself.

RasmusFonseca commented 6 years ago

Used a mix of the answer from here: https://stackoverflow.com/questions/4178614/suppressing-output-of-module-calling-outside-library:

import os
from contextlib import contextmanager

@contextmanager
def suppress_stdout():
    """
    Temporarily suppresses stdout.

    Example
    =======
        with suppress_stdout():
            print "You cannot see this"
    """
    with open('/dev/null', "w") as devnull:
        old_stdout = os.dup(sys.stdout.fileno())
        os.dup2(devnull.fileno(), 1)
        try:
            yield
        finally:
            os.dup2(old_stdout, 1)

.. and enclosed calls to molecule. Theres some molecule deloading as the program terminates so I also had to add

devnull = open('/dev/null', "w")
os.dup2(devnull.fileno(), 1)

just before the program terminates. My problem is fixed, but consider "quiet vmd" a feature request.