Source

repoman / repoman / ui.py

Full commit
# Copyright 2009-2012 Edlund A/S
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
'''
UI handling.
'''

import os
import re
import sys

import mercurial.ui

class RepoUi(mercurial.ui.ui):

    def __init__(self, src=None, **options):
        mercurial.ui.ui.__init__(self, src)
        self.debugflag = bool(options.get('debug'))
        self.verbose = self.debugflag or bool(options.get('verbose'))
        self.quiet = not self.debugflag and bool(options.get('quiet'))
        self.silent = not self.debugflag and bool(options.get('silent'))
        self._traceback = bool(options.get('traceback'))
        self.filter = options.get('filter')
        self._buffered = options.get('buffered')
        self.buffer = []
        if self.verbose and self.quiet:
            self.verbose = self.quiet = False

    @property
    def buffered(self):
        return self._buffered or self._buffers
    @buffered.setter
    def buffered(self, value):
        self._buffered = value
        if not value and self.buffer:
            self._write(''.join(self.buffer))
            self.buffer = []

    def copy(self):
        return self.__class__(src=self, debug=self.debugflag, verbose=self.verbose,
                              quiet=self.quiet, filter=self.filter, silent=self.silent)

    def _filter(self, text):
        f = self.filter

        if f is None:
            return text

        txt = text.split('\n')

        ret = []
        for t in txt:
            if isinstance(f, str) and f not in t:
                ret.append(t)
            if hasattr(f, 'match') and not f.match(t):
                ret.append(t)

        return '\n'.join(ret)

    def _write(self, text, stderr=False):
        if self.silent and not stderr:
            return
        text = self._filter(text)
        if not text:
            return
        if self.buffered:
            self.buffer.append(text)
            if self._buffers:
                self._buffers[-1].append(text)
            return
        if stderr:
            mercurial.ui.ui.write_err(self, text)
        else:
            self.fout.write(text)

    def write(self, *args, **opts):
        for arg in args:
            self._write(str(arg))

    def write_stderr(self, args):
        for arg in args:
            self._write(str(arg), stderr=True)

    def write_header(self, *msg):
        self.write(*msg, label='repoman.header')

    def write_err(self, *msg, **opts):
        self.write_stderr(msg)

    def emph_str(self, string):
        return self.label(string, 'repoman.emph')

    def write_module(self, module):
        self.write(self.emph_str(module))

    def write_module_header(self, module):
        self.write('~~~ %s ~~~\n' % (module,), label='repoman.module_header')

class SimpleUi(RepoUi):
    def __init__(self, **options):
        RepoUi.__init__(self, **options)
        self.setconfig('color', 'mode', 'never')

ui = RepoUi

if not os.isatty(sys.stdout.fileno()) or not os.isatty(sys.stderr.fileno()):
    ui = SimpleUi