Commit 699603e5 authored by Jérome Perrin's avatar Jérome Perrin

cli: make resetLogger a context manager restoring the state

parent 127ff568
......@@ -86,7 +86,7 @@ def do_lookup(logger, cache_dir, cache_url, signature_certificate_list,
logger.info('Python egg %s version %s', name, version)
logger.info('SHADIR URL: %s/%s\n', cache_dir, key)
resetLogger(logger)
with resetLogger(logger):
for line in pt.get_string(border=True, padding_width=0, vrules=prettytable.NONE).split('\n'):
logger.info(line)
except HTTPError as e:
......
......@@ -84,7 +84,7 @@ def do_lookup(logger, cache_dir, cache_url, signature_certificate_list,
logger.info('Software source URL: %s', url)
logger.info('SHADIR URL: %s/%s\n', cache_dir, key)
resetLogger(logger)
with resetLogger(logger):
for line in pt.get_string(border=True, padding_width=0, vrules=prettytable.NONE).split('\n'):
logger.info(line)
except HTTPError as e:
......
......@@ -28,6 +28,7 @@
##############################################################################
import argparse
import contextlib
import functools
import logging
import os
......@@ -36,9 +37,20 @@ import sys
from cliff import command
@contextlib.contextmanager
def resetLogger(logger):
has_propagate = hasattr(logger, 'propagate') # XXX mocked Loggers do not have this
if has_propagate:
propagate = logger.propagate
logger.propagate = False
logger.addHandler(logging.StreamHandler(sys.stdout))
stdout_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stdout_handler)
try:
yield
finally:
if has_propagate:
logger.propagate = propagate
logger.removeHandler(stdout_handler)
class Command(command.Command):
......
......@@ -58,7 +58,7 @@ class InfoCommand(ClientConfigCommand):
def do_info(logger, conf, local):
resetLogger(logger)
with resetLogger(logger):
try:
computer = local['slap'].registerComputer(conf.reference).getInformation()
except ResourceNotReady:
......@@ -70,4 +70,3 @@ def do_info(logger, conf, local):
logger.info('Computer Reference: %s', computer._reference)
logger.info('Computer Title : %s', computer._title)
......@@ -51,7 +51,7 @@ class ListCommand(ClientConfigCommand):
def do_list(logger, conf, local):
resetLogger(logger)
with resetLogger(logger):
computer_dict = local['slap'].getComputerDict()
if computer_dict == {}:
logger.info('No existing computer.')
......
......@@ -54,7 +54,7 @@ class TokenCommand(ClientConfigCommand):
def do_token(logger, conf, local):
resetLogger(logger)
with resetLogger(logger):
try:
token = local['slap'].registerToken().request()
except ResourceNotReady:
......@@ -65,4 +65,3 @@ def do_token(logger, conf, local):
return(2)
logger.info('Computer token: %s', token)
......@@ -70,7 +70,6 @@ class InfoCommand(ClientConfigCommand):
def do_info(logger, conf, local):
resetLogger(logger)
try:
instance = local['slap'].registerOpenOrder().getInformation(
partition_reference=conf.reference,
......@@ -126,5 +125,6 @@ def do_info(logger, conf, local):
if conf.news:
info['news'] = news
with resetLogger(logger):
logger.info(json.dumps(info, indent=2))
......@@ -52,9 +52,9 @@ class ListCommand(ClientConfigCommand):
def do_list(logger, conf, local):
resetLogger(logger)
# XXX catch exception
instance_dict = local['slap'].getOpenOrderDict()
with resetLogger(logger):
logger.info(
json.dumps(
{title: instance._software_release_url
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment