Package svnmailer :: Package notifier :: Module stdout

[hide private]

# Source Code for Module svnmailer.notifier.stdout

      1  # -*- coding: utf-8 -*-
      2  #
      3  # Copyright 2004-2006 André Malo or his licensors, as applicable
      4  #
      5  # Licensed under the Apache License, Version 2.0 (the "License");
      6  # you may not use this file except in compliance with the License.
      7  # You may obtain a copy of the License at
      8  #
      9  #     http://www.apache.org/licenses/LICENSE-2.0
     10  #
     11  # Unless required by applicable law or agreed to in writing, software
     12  # distributed under the License is distributed on an "AS IS" BASIS,
     13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  # See the License for the specific language governing permissions and
     15  # limitations under the License.
     16  """
     17  Stdout notifier - mostly for debugging purposes
     18  """
     19  __author__    = "André Malo"
     20  __docformat__ = "epytext en"
     21  __all__       = ['getNotifier']
     22
     23  # global imports
     24  import sys
     25  from svnmailer.notifier import _text
     26
     27
     28 -def getNotifier(config, groupset):
     29      """ Returns an initialized notifier or nothing
     30
     31          @param config: The svnmailer config
     32          @type config: C{svnmailer.settings.Settings}
     33
     34          @param groupset: The groupset to process
     35          @type groupset: C{list}
     36
     37          @return: The list of notifiers (containing 0 or 1 member)
     38          @rtype: C{list}
     39      """
     40      return [StdoutNotifier(config, groupset)]
     41
     42
     43 -class StdoutNotifier(_text.TextNotifier):
     44      """ Writes all stuff to sys.stdout
     45
     46          @cvar _fp: C{None}
     47          @ivar _fp: alternate file like object (for testing purposes)
     48          @type _fp: file like object
     49      """
     50      __implements__ = [_text.TextNotifier]
     51      _fp = None
     52
     53
     54 -    def run(self):
     55          """ Print the notification to stdout """
     56          from svnmailer import stream
     57
     58          fp = self._fp or sys.stdout
     59          fp = stream.UnicodeStream(fp, out_enc = self._getOutputEnc(fp))
     60          groups = self._groupset.groups
     61          self.fp = fp
     62
     63          self._writePreamble(groups)
     64          self._writeNotification()
     65
     66
     67 -    def _writeNotification(self):
     68          """ Writes the whole diff notification body """
     69          from svnmailer.settings import modes
     70
     71          mode = self._settings.runtime.mode
     72
     73          if mode == modes.commit:
     74              self.writeMetaData()
     75              self.writePathList()
     76              self.writeDiffList()
     77          elif mode == modes.propchange:
     78              self.writeRevPropData()
     79          elif mode in (modes.lock, modes.unlock):
     80              self.writeLockData()
     81          else:
     82              raise AssertionError("Unknown runtime.mode %r" % (mode,))
     83
     84
     85 -    def _writePreamble(self, groups):
     86          """ Writes the stdout preamble for the selected groups
     87
     88              @param groups: The groups that are notified
     89              @type groups: C{list}
     90          """
     91          self.fp.write(
     92              ">>> Notification for the following group%s:\n  %s\n\n" %
     93              (["", "s"][len(groups) > 1],
     94              ",\n  ".join(["[%s]" % group._name for group in groups]))
     95          )
     96
     97
     98 -    def _getOutputEnc(self, fp):
     99          """ Returns the "proper" output encoding
    100
    101              If the output goes to a terminal, the method tries to get
    102              the current locale encoding. UTF-8 is default and fallback
    103              if anything fails.
    104
    105              @param fp: The file object written to
    106              @type fp: file like object
    107
    108              @return: The chosen encoding
    109              @rtype: C{str}
    110          """
    111          import os
    112
    113          enc = "utf-8"
    114          try:
    115              isatty = os.isatty(fp.fileno())
    116          except AttributeError:
    117              isatty = False
    118
    119          if isatty:
    120              import locale
    121              enc = locale.getpreferredencoding() or enc
    122
    123          return enc
    124