use logging to control output verbosity
Use the logging module to control what output is printed to the console. Add some additional debugging information to the rule evaluation module. Change-Id: I3b83bdd90c33cdddf36946d01a32e799d4a95e31 Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
parent
e942ec95b4
commit
e255ef4f1d
@ -19,6 +19,8 @@ from __future__ import print_function
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import io
|
import io
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
from whereto import parser
|
from whereto import parser
|
||||||
from whereto import rules
|
from whereto import rules
|
||||||
@ -124,10 +126,19 @@ argument_parser.add_argument(
|
|||||||
default=0,
|
default=0,
|
||||||
help='how many hops are allowed',
|
help='how many hops are allowed',
|
||||||
)
|
)
|
||||||
|
argument_parser.add_argument(
|
||||||
|
'-v', '--verbose',
|
||||||
|
dest='verbosity',
|
||||||
|
default=[1],
|
||||||
|
action='append_const',
|
||||||
|
const=1,
|
||||||
|
help='increase the verbosity by one level',
|
||||||
|
)
|
||||||
argument_parser.add_argument(
|
argument_parser.add_argument(
|
||||||
'-q', '--quiet',
|
'-q', '--quiet',
|
||||||
action='store_true',
|
action='store_const',
|
||||||
default=False,
|
dest='verbosity',
|
||||||
|
const=[],
|
||||||
help='run quietly',
|
help='run quietly',
|
||||||
)
|
)
|
||||||
argument_parser.add_argument(
|
argument_parser.add_argument(
|
||||||
@ -140,34 +151,43 @@ argument_parser.add_argument(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def report(msg):
|
|
||||||
print('whereto: {}'.format(msg))
|
|
||||||
|
|
||||||
|
|
||||||
def show_test_and_matches(msg, test, matches):
|
def show_test_and_matches(msg, test, matches):
|
||||||
report(
|
logging.error(
|
||||||
'{} on line {}: {} should produce {} {}'.format(
|
'{} on line {}: {} should produce {} {}'.format(
|
||||||
msg, test[0], test[1], test[2], test[3])
|
msg, test[0], test[1], test[2], test[3])
|
||||||
)
|
)
|
||||||
path = test[1]
|
path = test[1]
|
||||||
for linenum, code, new_path in matches:
|
for linenum, code, new_path in matches:
|
||||||
print('whereto: {} -> {} {} [line {}]'.format(
|
logging.error(' {} -> {} {} [line {}]'.format(
|
||||||
path, code, new_path, linenum))
|
path, code, new_path, linenum))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = argument_parser.parse_args()
|
args = argument_parser.parse_args()
|
||||||
|
|
||||||
|
verbosity = sum(args.verbosity)
|
||||||
|
if verbosity < 1:
|
||||||
|
log_level = logging.WARNING
|
||||||
|
elif verbosity == 1:
|
||||||
|
log_level = logging.INFO
|
||||||
|
else:
|
||||||
|
log_level = logging.DEBUG
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=log_level,
|
||||||
|
format='whereto %(levelname)s: %(message)s',
|
||||||
|
stream=sys.stdout,
|
||||||
|
)
|
||||||
|
log = logging.getLogger()
|
||||||
|
|
||||||
ruleset = rules.RuleSet()
|
ruleset = rules.RuleSet()
|
||||||
|
|
||||||
if not args.quiet:
|
log.debug('reading redirects from {}'.format(args.htaccess_file))
|
||||||
print('whereto: reading redirects from {}'.format(args.htaccess_file))
|
|
||||||
with io.open(args.htaccess_file, 'r', encoding='utf-8') as f:
|
with io.open(args.htaccess_file, 'r', encoding='utf-8') as f:
|
||||||
for linenum, params in parser.parse_rules(f):
|
for linenum, params in parser.parse_rules(f):
|
||||||
ruleset.add(linenum, *params)
|
ruleset.add(linenum, *params)
|
||||||
|
|
||||||
if not args.quiet:
|
log.debug('reading tests from {}'.format(args.htaccess_file))
|
||||||
print('whereto: reading tests from {}'.format(args.htaccess_file))
|
|
||||||
with io.open(args.test_file, 'r', encoding='utf-8') as f:
|
with io.open(args.test_file, 'r', encoding='utf-8') as f:
|
||||||
tests = [
|
tests = [
|
||||||
(linenum,) + tuple(params)
|
(linenum,) + tuple(params)
|
||||||
@ -210,15 +230,14 @@ def main():
|
|||||||
)
|
)
|
||||||
|
|
||||||
if untested:
|
if untested:
|
||||||
if not args.quiet:
|
log.debug('')
|
||||||
print('')
|
|
||||||
for linenum in sorted(untested):
|
for linenum in sorted(untested):
|
||||||
if not args.quiet:
|
if not args.quiet:
|
||||||
report('Untested rule: {}'.format(ruleset[linenum]))
|
logging.error('Untested rule: {}'.format(ruleset[linenum]))
|
||||||
if args.error_untested:
|
if args.error_untested:
|
||||||
failures += 1
|
failures += 1
|
||||||
|
|
||||||
if failures:
|
if failures:
|
||||||
report('{} failures'.format(failures))
|
logging.error('{} failures'.format(failures))
|
||||||
return 1
|
return 1
|
||||||
return 0
|
return 0
|
||||||
|
@ -15,9 +15,13 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
LOG = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
class Rule(object):
|
class Rule(object):
|
||||||
"Base class for rules."
|
"Base class for rules."
|
||||||
|
|
||||||
@ -118,8 +122,11 @@ class RuleSet(object):
|
|||||||
try:
|
try:
|
||||||
m = rule.match(path)
|
m = rule.match(path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Failed to evaluate {} against {}: {}'.format(
|
LOG.warning('Failed to evaluate {} against {}: {}'.format(
|
||||||
rule, path, e))
|
rule, path, e))
|
||||||
else:
|
else:
|
||||||
if m is not None:
|
if m is not None:
|
||||||
|
LOG.debug(
|
||||||
|
'Matched "{}" for path "{}" producing {}'.format(
|
||||||
|
rule, path, m))
|
||||||
return (rule.linenum,) + m
|
return (rule.linenum,) + m
|
||||||
|
Loading…
Reference in New Issue
Block a user