instant.output

Package instant :: Module output |
[hide private]
[frames] | no frames]

# Source Code for Module instant.output

     1  """This module contains internal logging utilities."""
     2
     3  import logging
     4
     5  # Logging wrappers
     6
     7  _log = logging.getLogger("instant")
     8  _loghandler = logging.StreamHandler()
     9  _log.addHandler(_loghandler)
    10  _log.setLevel(logging.WARNING)
    11  _log.setLevel(logging.INFO)
    12  #_log.setLevel(logging.DEBUG)
    13
    14 -def get_log_handler():
    15      return _loghandler
    16
    17 -def get_logger():
    18      return _log
    19
    20 -def set_log_handler(handler):
    21      global _loghandler
    22      _log.removeHandler(_loghandler)
    23      _loghandler = handler
    24      _log.addHandler(_loghandler)
    25
    26 -def set_logging_level(level):
    27      if isinstance(level, str):
    28          level = level.upper()
    29          assert level in ("INFO", "WARNING", "ERROR", "DEBUG")
    30          level = getattr(logging, level)
    31      else:
    32          assert isinstance(level, int)
    33      _log.setLevel(level)
    34
    35  # Aliases for calling log consistently:
    36
    37 -def instant_debug(*message):
    38      _log.debug(*message)
    39
    40 -def instant_info(*message):
    41      _log.info(*message)
    42
    43 -def instant_warning(*message):
    44      _log.warning(*message)
    45
    46 -def instant_error(*message):
    47      _log.error(*message)
    48      text = message[0] % message[1:]
    49      raise RuntimeError(text)
    50
    51 -def instant_assert(condition, *message):
    52      if not condition:
    53          _log.error(*message)
    54          text = message[0] % message[1:]
    55          raise AssertionError(text)
    56
    57  # Utility functions for file handling:
    58
    59 -def write_file(filename, text):
    60      "Write text to a file and close it."
    61      try:
    62          f = open(filename, "w")
    63          f.write(text)
    64          f.close()
    65      except IOError, e:
    66          instant_error("Can't open '%s': %s" % (filename, e))
    67
    68
    69  # Taken from http://ivory.idyll.org/blog/mar-07/replacing-commands-with-subprocess
    70  from subprocess import Popen, PIPE, STDOUT
    71 -def get_status_output(cmd, input=None, cwd=None, env=None):
    72      pipe = Popen(cmd, shell=True, cwd=cwd, env=env, stdout=PIPE, stderr=STDOUT)
    73
    74      (output, errout) = pipe.communicate(input=input)
    75      assert not errout
    76
    77      status = pipe.returncode
    78
    79      return (status, output)
    80