# HG changeset patch # User Goffi # Date 1498686595 -7200 # Node ID d8e48c850ad2be206fa600c8e997c8525d9b1a07 # Parent 4af1805cc6df4b24147730e4fac098b756c8ea46 jp (input): log improvments + empty filter: - more colors in debug logs - read values are printed if verbosity is >= 2 - stdout and stderr of each command can be written in files using --log and --log-err - --empty option allow to ignore arguments if its value is empty, or skip the whole row diff -r 4af1805cc6df -r d8e48c850ad2 frontends/src/jp/cmd_input.py --- a/frontends/src/jp/cmd_input.py Wed Jun 28 20:28:58 2017 +0200 +++ b/frontends/src/jp/cmd_input.py Wed Jun 28 23:49:55 2017 +0200 @@ -20,11 +20,11 @@ import base from sat.core.i18n import _ +from sat.core import exceptions from sat_frontends.jp.constants import Const as C from sat.tools.common.ansi import ANSI as A import subprocess import argparse -import os import sys __commands__ = ["Input"] @@ -34,6 +34,9 @@ OPT_POS = 'positional' OPT_IGNORE = 'ignore' OPT_TYPES = (OPT_STDIN, OPT_SHORT, OPT_LONG, OPT_POS, OPT_IGNORE) +OPT_EMPTY_SKIP = 'skip' +OPT_EMPTY_IGNORE = 'ignore' +OPT_EMPTY_CHOICES = (OPT_EMPTY_SKIP, OPT_EMPTY_IGNORE) class InputCommon(base.CommandBase): @@ -48,6 +51,7 @@ self._stdin = [] self._opts = [] self._pos = [] + self._values_ori = [] def add_parser_options(self): self.parser.add_argument("--encoding", default='utf-8', help=_(u"encoding of the input data")) @@ -57,6 +61,8 @@ self.parser.add_argument("-p", "--positional", type=self.opt(OPT_POS), action='append', dest='arguments', help=_(u"positional argument")) self.parser.add_argument("-x", "--ignore", action='append_const', const=(OPT_IGNORE, None), dest='arguments', help=_(u"ignore value")) self.parser.add_argument("-D", "--debug", action='store_true', help=_(u"don't actually run commands but echo what would be launched")) + self.parser.add_argument("--log", type=argparse.FileType('wb'), help=_(u"log stdout to FILE")) + self.parser.add_argument("--log-err", type=argparse.FileType('wb'), help=_(u"log stderr to FILE")) self.parser.add_argument("command", nargs=argparse.REMAINDER) def opt(self, type_): @@ -64,6 +70,7 @@ def addValue(self, value): """add a parsed value according to arguments sequence""" + self._values_ori.append(value) arguments = self.args.arguments try: arg_type, arg_name = arguments[self.args_idx] @@ -74,13 +81,28 @@ while self.args_idx < len(arguments): next_arg = arguments[self.args_idx] if next_arg[0] not in OPT_TYPES: - # we have a filter - filter_type, filter_arg = arguments[self.args_idx] - value = self.filter(filter_type, filter_arg, value) + # value will not be used if False or None, so we skip filter + if value not in (False, None): + # we have a filter + filter_type, filter_arg = arguments[self.args_idx] + value = self.filter(filter_type, filter_arg, value) else: break self.args_idx += 1 + if value is None: + # we ignore this argument + return + + if value is False: + # we skip the whole row + if self.args.debug: + self.disp(A.color(C.A_SUBHEADER, _(u'values: '), A.RESET, u', '.join(self._values_ori)), 2) + self.disp(A.color(A.BOLD, _(u'**SKIPPING**\n'))) + self.reset() + self.idx += 1 + raise exceptions.CancelError + if not isinstance(value, list): value = [value] @@ -106,13 +128,15 @@ if self.args_idx != len(self.args.arguments): self.disp(_(u"arguments in input data and in arguments sequence don't match"), error=True) self.host.quit(C.EXIT_DATA_ERROR) + self.disp(A.color(C.A_HEADER, _(u'command {idx}').format(idx=self.idx)), no_lf=not self.args.debug) stdin = ''.join(self._stdin) if self.args.debug: - self.disp(_(u'command {idx}').format(idx=self.idx)) + self.disp(A.color(C.A_SUBHEADER, _(u'values: '), A.RESET, u', '.join(self._values_ori)), 2) + if stdin: - self.disp(u'--- STDIN ---') + self.disp(A.color(C.A_SUBHEADER, u'--- STDIN ---')) self.disp(stdin.decode('utf-8')) - self.disp(u'-------------') + self.disp(A.color(C.A_SUBHEADER, u'-------------')) self.disp(u'{indent}{prog} {static} {options} {positionals}'.format( indent = 4*u' ', prog=sys.argv[0], @@ -120,14 +144,21 @@ options = u' '.join([o.encode('utf-8') for o in self._opts]), positionals = u' '.join([p.encode('utf-8') for p in self._pos]) )) - self.disp(u'') + self.disp(u'\n') else: - self.disp(_(u'command {idx}').format(idx=self.idx), no_lf=True) - with open(os.devnull, 'wb') as DEVNULL: - p = subprocess.Popen([sys.argv[0]] + self.args.command + self._opts + self._pos, - stdin=subprocess.PIPE, stdout=DEVNULL, stderr=DEVNULL) - p.communicate(stdin) - ret = p.wait() + self.disp(u' (' + u', '.join(self._values_ori) + u')', 2, no_lf=True) + args = [sys.argv[0]] + self.args.command + self._opts + self._pos + p = subprocess.Popen(args, + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (stdout, stderr) = p.communicate(stdin) + log = self.args.log + log_err = self.args.log_err + log_tpl = '{command}\n{buff}\n\n' + if log: + log.write(log_tpl.format(command=' '.join(args), buff=stdout)) + if log_err: + log_err.write(log_tpl.format(command=' '.join(args), buff=stderr)) + ret = p.wait() if ret == 0: self.disp(A.color(C.A_SUCCESS, _(u'OK'))) else: @@ -137,6 +168,15 @@ self.idx += 1 def filter(self, filter_type, filter_arg, value): + """change input value + + @param filter_type(unicode): name of the filter + @param filter_arg(unicode, None): argument of the filter + @param value(unicode): value to filter + @return (unicode, False, None): modified value + False to skip the whole row + None to ignore this argument (but continue row with other ones) + """ raise NotImplementedError @@ -149,10 +189,19 @@ InputCommon.add_parser_options(self) self.parser.add_argument("-r", "--row", type=int, default=0, help=_(u"starting row (previous ones will be ignored)")) self.parser.add_argument("-S", "--split", action='append_const', const=('split', None), dest='arguments', help=_(u"split value in several options")) + self.parser.add_argument("-E", "--empty", action='append', type=self.opt('empty'), dest='arguments', + help=_(u"action to do on empty value ({choices})").format(choices=u', '.join(OPT_EMPTY_CHOICES))) - def filter(self, arg_type, filter_type, filter_arg, value): + def filter(self, filter_type, filter_arg, value): if filter_type == 'split': return value.split() + elif filter_type == 'empty': + if filter_arg == OPT_EMPTY_IGNORE: + return value if value else None + elif filter_arg == OPT_EMPTY_SKIP: + return value if value else False + else: + self.parser.error(_(u"--empty value must be one of {choices}").format(choices=u', '.join(OPT_EMPTY_CHOICES))) super(Csv, self).filter(filter_type, filter_arg, value) @@ -160,11 +209,15 @@ import csv reader = csv.reader(sys.stdin) for idx, row in enumerate(reader): - if idx < self.args.row: + try: + if idx < self.args.row: + continue + for value in row: + self.addValue(value.decode(self.args.encoding)) + self.runCommand() + except exceptions.CancelError: + # this row has been cancelled, we skip it continue - for value in row: - self.addValue(value.decode(self.args.encoding)) - self.runCommand() class Input(base.CommandBase):