2017-04-12 06:14:09 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
|
|
|
2017-04-12 06:39:48 +04:00
|
|
|
u"""Afficher les lignes d'un fichier en mettant en surbrillance certains patterns"""
|
2017-04-12 06:14:09 +04:00
|
|
|
|
|
|
|
import sys, subprocess, re
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
def bluef(line, *ignored): return '\x1B[34m%s\x1B[0m' % line
|
|
|
|
def greenf(line, *ignored): return '\x1B[32m%s\x1B[0m' % line
|
|
|
|
def yellowf(line, *ignored): return '\x1B[33m%s\x1B[0m' % line
|
|
|
|
def redf(line, *ignored): return '\x1B[31m%s\x1B[0m' % line
|
|
|
|
def nonef(line, *ignored): return re.sub('\x1B\[.*?m', '', line)
|
|
|
|
|
|
|
|
DEFAULT_PATTERNS = OrderedDict([
|
|
|
|
(r'(?i)error', redf),
|
|
|
|
(r'(?i)warn(ing)?', yellowf),
|
|
|
|
(r'(?i)info', bluef),
|
|
|
|
(None, nonef),
|
|
|
|
])
|
|
|
|
FORMATS = OrderedDict([
|
|
|
|
('blue', bluef),
|
|
|
|
('green', greenf),
|
|
|
|
('yellow', yellowf),
|
|
|
|
('red', redf),
|
|
|
|
('none', nonef),
|
|
|
|
])
|
|
|
|
FORMAT_ALIASES = {
|
|
|
|
'b': 'blue',
|
|
|
|
'g': 'green',
|
|
|
|
'y': 'yellow',
|
|
|
|
'r': 'red',
|
|
|
|
'': 'none',
|
|
|
|
}
|
|
|
|
|
|
|
|
def strip_nl(s):
|
|
|
|
if s is None: return None
|
|
|
|
elif s.endswith("\r\n"): s = s[:-2]
|
|
|
|
elif s.endswith("\n"): s = s[:-1]
|
|
|
|
elif s.endswith("\r"): s = s[:-1]
|
|
|
|
return s
|
|
|
|
|
2017-04-12 06:39:48 +04:00
|
|
|
def run_tailor(inputfile=None, follow=False, patterns=None):
|
|
|
|
if inputfile is None or not follow:
|
|
|
|
if inputfile is None:
|
|
|
|
inf = sys.stdin
|
|
|
|
close = False
|
|
|
|
else:
|
|
|
|
inf = open(inputfile, 'rb')
|
|
|
|
close = True
|
2017-04-12 06:14:09 +04:00
|
|
|
def next_line():
|
2017-04-12 06:39:48 +04:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
try: line = inf.readline()
|
|
|
|
except: break
|
|
|
|
if line == '': break
|
|
|
|
yield line
|
|
|
|
finally:
|
|
|
|
if close: inf.close()
|
2017-04-12 06:14:09 +04:00
|
|
|
else:
|
|
|
|
def next_line():
|
|
|
|
p = subprocess.Popen(
|
|
|
|
['tail', '-f', inputfile],
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
|
|
while True:
|
|
|
|
try: line = p.stdout.readline()
|
|
|
|
except: break
|
|
|
|
if line == '': break
|
|
|
|
yield line
|
|
|
|
if patterns is None: patterns = DEFAULT_PATTERNS
|
|
|
|
|
|
|
|
for line in next_line():
|
|
|
|
line = nonef(strip_nl(line))
|
|
|
|
func = False
|
|
|
|
mo = None
|
|
|
|
for p, f in patterns.items():
|
|
|
|
if p is None:
|
|
|
|
func = f
|
|
|
|
mo = None
|
|
|
|
break
|
|
|
|
mo = re.search(p, line)
|
|
|
|
if mo is not None:
|
|
|
|
func = f
|
|
|
|
break
|
|
|
|
if func is not False:
|
|
|
|
line = func(line, mo)
|
|
|
|
sys.stdout.write(line)
|
|
|
|
sys.stdout.write('\n')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-04-12 06:39:48 +04:00
|
|
|
from argparse import ArgumentParser
|
2017-04-12 06:14:09 +04:00
|
|
|
AP = ArgumentParser(
|
2017-04-12 06:39:48 +04:00
|
|
|
usage=u"%(prog)s [-f] [INPUTFILE]",
|
2017-04-12 06:14:09 +04:00
|
|
|
description=__doc__,
|
|
|
|
)
|
2017-04-12 06:39:48 +04:00
|
|
|
AP.set_defaults(inputfile=None, follow=False, patterns=None, defaults=True)
|
|
|
|
pattern_help = u"""\
|
|
|
|
Ajouter une spécification de pattern et le format dans lequel il doit être affiché.
|
|
|
|
Le format par défaut est red.
|
|
|
|
Les formats valides sont: %(formats)s""" % {
|
|
|
|
'formats': ', '.join(FORMATS.keys()),
|
|
|
|
}
|
2017-04-12 06:14:09 +04:00
|
|
|
AP.add_argument('-e', '--pattern', action='append', dest='patterns', metavar='PATTERN:FORMAT',
|
2017-04-12 06:39:48 +04:00
|
|
|
help=pattern_help)
|
|
|
|
AP.add_argument('-z', '--no-defaults', action='store_false', dest='defaults',
|
2017-04-12 06:14:09 +04:00
|
|
|
help=u"Ne pas ajouter les patterns par défaut")
|
2017-04-12 06:39:48 +04:00
|
|
|
AP.add_argument('-f', '--follow', action='store_true', dest='follow',
|
|
|
|
help=u"Suivre le contenu du fichier spécifié")
|
|
|
|
AP.add_argument('inputfile', metavar='INPUTFILE', nargs='?',
|
|
|
|
help=u"Fichier qu'il faut afficher ou dont il faut suivre le contenu")
|
2017-04-12 06:14:09 +04:00
|
|
|
o = AP.parse_args()
|
|
|
|
|
|
|
|
if o.patterns is None:
|
|
|
|
patterns = DEFAULT_PATTERNS
|
|
|
|
else:
|
|
|
|
patterns = OrderedDict()
|
|
|
|
for pf in o.patterns:
|
|
|
|
mo = re.match('(.*):([a-zA-Z]*)$', pf)
|
|
|
|
if mo is not None:
|
|
|
|
p = mo.group(1)
|
|
|
|
of = mo.group(2)
|
|
|
|
else:
|
|
|
|
p = pf
|
|
|
|
of = 'red'
|
2017-04-12 06:39:48 +04:00
|
|
|
if p == '': p = None
|
2017-04-12 06:14:09 +04:00
|
|
|
f = of.lower()
|
|
|
|
f = FORMAT_ALIASES.get(f, f)
|
|
|
|
if f not in FORMATS:
|
|
|
|
raise ValueError("%s: format invalide" % of)
|
|
|
|
patterns[p] = FORMATS[f]
|
2017-04-12 06:39:48 +04:00
|
|
|
if o.defaults:
|
|
|
|
for p, f in DEFAULT_PATTERNS.items():
|
|
|
|
patterns.setdefault(p, f)
|
2017-04-12 06:14:09 +04:00
|
|
|
|
2017-04-12 06:39:48 +04:00
|
|
|
run_tailor(o.inputfile, o.follow, patterns)
|