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

cli: make resetLogger a context manager restoring the state

parent 127ff568
...@@ -86,9 +86,9 @@ def do_lookup(logger, cache_dir, cache_url, signature_certificate_list, ...@@ -86,9 +86,9 @@ def do_lookup(logger, cache_dir, cache_url, signature_certificate_list,
logger.info('Python egg %s version %s', name, version) logger.info('Python egg %s version %s', name, version)
logger.info('SHADIR URL: %s/%s\n', cache_dir, key) 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'): for line in pt.get_string(border=True, padding_width=0, vrules=prettytable.NONE).split('\n'):
logger.info(line) logger.info(line)
except HTTPError as e: except HTTPError as e:
if e.code == 404: if e.code == 404:
logger.info('Object not found in cache.') logger.info('Object not found in cache.')
......
...@@ -84,9 +84,9 @@ def do_lookup(logger, cache_dir, cache_url, signature_certificate_list, ...@@ -84,9 +84,9 @@ def do_lookup(logger, cache_dir, cache_url, signature_certificate_list,
logger.info('Software source URL: %s', url) logger.info('Software source URL: %s', url)
logger.info('SHADIR URL: %s/%s\n', cache_dir, key) 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'): for line in pt.get_string(border=True, padding_width=0, vrules=prettytable.NONE).split('\n'):
logger.info(line) logger.info(line)
except HTTPError as e: except HTTPError as e:
if e.code == 404: if e.code == 404:
logger.info('Object not found in cache.') logger.info('Object not found in cache.')
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
############################################################################## ##############################################################################
import argparse import argparse
import contextlib
import functools import functools
import logging import logging
import os import os
...@@ -36,9 +37,20 @@ import sys ...@@ -36,9 +37,20 @@ import sys
from cliff import command from cliff import command
@contextlib.contextmanager
def resetLogger(logger): def resetLogger(logger):
logger.propagate = False has_propagate = hasattr(logger, 'propagate') # XXX mocked Loggers do not have this
logger.addHandler(logging.StreamHandler(sys.stdout)) if has_propagate:
propagate = logger.propagate
logger.propagate = False
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): class Command(command.Command):
......
...@@ -58,16 +58,15 @@ class InfoCommand(ClientConfigCommand): ...@@ -58,16 +58,15 @@ class InfoCommand(ClientConfigCommand):
def do_info(logger, conf, local): def do_info(logger, conf, local):
resetLogger(logger) with resetLogger(logger):
try: try:
computer = local['slap'].registerComputer(conf.reference).getInformation() computer = local['slap'].registerComputer(conf.reference).getInformation()
except ResourceNotReady: except ResourceNotReady:
logger.warning('Computer does not exist or is not ready yet.') logger.warning('Computer does not exist or is not ready yet.')
return(2) return(2)
except NotFoundError: except NotFoundError:
logger.warning('Computer %s does not exist.', conf.reference) logger.warning('Computer %s does not exist.', conf.reference)
return(2) return(2)
logger.info('Computer Reference: %s', computer._reference)
logger.info('Computer Title : %s', computer._title)
logger.info('Computer Reference: %s', computer._reference)
logger.info('Computer Title : %s', computer._title)
...@@ -51,11 +51,11 @@ class ListCommand(ClientConfigCommand): ...@@ -51,11 +51,11 @@ class ListCommand(ClientConfigCommand):
def do_list(logger, conf, local): def do_list(logger, conf, local):
resetLogger(logger) with resetLogger(logger):
computer_dict = local['slap'].getComputerDict() computer_dict = local['slap'].getComputerDict()
if computer_dict == {}: if computer_dict == {}:
logger.info('No existing computer.') logger.info('No existing computer.')
return return
logger.info('List of Computers:') logger.info('List of Computers:')
for title, computer in six.iteritems(computer_dict): for title, computer in six.iteritems(computer_dict):
logger.info('%s %s', computer._reference, title) logger.info('%s %s', computer._reference, title)
...@@ -54,15 +54,14 @@ class TokenCommand(ClientConfigCommand): ...@@ -54,15 +54,14 @@ class TokenCommand(ClientConfigCommand):
def do_token(logger, conf, local): def do_token(logger, conf, local):
resetLogger(logger) with resetLogger(logger):
try: try:
token = local['slap'].registerToken().request() token = local['slap'].registerToken().request()
except ResourceNotReady: except ResourceNotReady:
logger.warning('Computer does not exist or is not ready yet.') logger.warning('Computer does not exist or is not ready yet.')
return(2) return(2)
except NotFoundError: except NotFoundError:
logger.warning('Computer %s does not exist.', conf.reference) logger.warning('Computer %s does not exist.', conf.reference)
return(2) return(2)
logger.info('Computer token: %s', token)
logger.info('Computer token: %s', token)
...@@ -70,7 +70,6 @@ class InfoCommand(ClientConfigCommand): ...@@ -70,7 +70,6 @@ class InfoCommand(ClientConfigCommand):
def do_info(logger, conf, local): def do_info(logger, conf, local):
resetLogger(logger)
try: try:
instance = local['slap'].registerOpenOrder().getInformation( instance = local['slap'].registerOpenOrder().getInformation(
partition_reference=conf.reference, partition_reference=conf.reference,
...@@ -126,5 +125,6 @@ def do_info(logger, conf, local): ...@@ -126,5 +125,6 @@ def do_info(logger, conf, local):
if conf.news: if conf.news:
info['news'] = news info['news'] = news
logger.info(json.dumps(info, indent=2)) with resetLogger(logger):
logger.info(json.dumps(info, indent=2))
...@@ -52,12 +52,12 @@ class ListCommand(ClientConfigCommand): ...@@ -52,12 +52,12 @@ class ListCommand(ClientConfigCommand):
def do_list(logger, conf, local): def do_list(logger, conf, local):
resetLogger(logger)
# XXX catch exception # XXX catch exception
instance_dict = local['slap'].getOpenOrderDict() instance_dict = local['slap'].getOpenOrderDict()
logger.info( with resetLogger(logger):
json.dumps( logger.info(
{title: instance._software_release_url json.dumps(
for (title, instance) in six.iteritems(instance_dict)}, {title: instance._software_release_url
sort_keys=True, for (title, instance) in six.iteritems(instance_dict)},
indent=2)) sort_keys=True,
indent=2))
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