Commit 1000fa2a authored by Cyril Roelandt's avatar Cyril Roelandt
Browse files

upt.log: New module.

Frontends and backends will now be able to use a unified logger utility.
parent 90bc7bbc
......@@ -6,6 +6,7 @@ Based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Added
- upt package: Add the '--output' option
- upt.log: New module that provides frontends/backends with a logger
## [0.3] - 2018-03-13
### Added
......
......@@ -313,6 +313,21 @@ class MyBackend(upt.Backend):
```
### Logging
Whether you are writing a frontend or a backend, you can use a logger provided
by upt by running:
```
import logging
...
logger = logging.getLogger('upt-backend')
```
See the [logging module
documentation](https://docs.python.org/3/library/logging.html) for more info on
how to use this logger.
## Known frontends and backends
The following frontends are supported:
......
# Copyright 2018 Cyril Roelandt
#
# Licensed under the 3-clause BSD license. See the LICENSE file.
from io import StringIO
import logging
import unittest
from unittest import mock
import upt
class TestLog(unittest.TestCase):
@mock.patch('sys.stderr', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=StringIO)
def test_quiet(self, m_stdout, m_stderr):
logger = upt.log.create_logger(logging.CRITICAL+1)
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')
self.assertEqual(m_stdout.getvalue(), '')
self.assertEqual(m_stderr.getvalue(), '')
@mock.patch('sys.stderr', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=StringIO)
def test_debug(self, m_stdout, m_stderr):
logger = upt.log.create_logger(logging.DEBUG)
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')
self.assertEqual(m_stdout.getvalue(), 'debug\ninfo\nwarning\n')
self.assertEqual(m_stderr.getvalue(), 'error\ncritical\n')
@mock.patch('sys.stderr', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=StringIO)
def test_set_formatter(self, m_stdout, m_stderr):
logger = upt.log.create_logger(logging.DEBUG)
upt.log.logger_set_formatter(logger, 'foo')
logger.debug('debug')
logger.error('error')
self.assertEqual(m_stdout.getvalue(), '[DEBUG ] [foo] debug\n')
self.assertEqual(m_stderr.getvalue(), '[ERROR ] [foo] error\n')
if __name__ == '__main__':
unittest.main()
......@@ -39,3 +39,8 @@ class TestCommandLine(unittest.TestCase):
args = 'package -f pypi -b guix'.split()
with self.assertRaises(SystemExit):
self.parser.parse_args(args)
def test_package_exclusive_logging_options(self):
args = 'package -f pypi -b guix --debug --quiet requests'.split()
with self.assertRaises(SystemExit):
self.parser.parse_args(args)
......@@ -2,11 +2,13 @@
#
# Licensed under the 3-clause BSD license. See the LICENSE file.
import argparse
import logging
import sys
import pkg_resources
import upt.exceptions
import upt.log
class Backend(object):
......@@ -102,10 +104,17 @@ def create_parser():
help='Frontend to use')
required_args.add_argument('-b', '--backend', required=True,
help='Backend to use')
logger_group = parser_package.add_mutually_exclusive_group()
logger_group.add_argument('--debug', action='store_const',
const=logging.DEBUG, dest='log_level',
help='Print debug messages.')
parser_package.add_argument('-o', '--output',
help='Output file/directory. Defaults to '
'stdout/current directory. This may be '
'ignored by the backend.')
logger_group.add_argument('-q', '--quiet', action='store_const',
const=logging.CRITICAL+1, dest='log_level',
help='Suppress all logging output')
parser_package.add_argument('package', help='Name of the package')
return parser
......@@ -162,9 +171,13 @@ def main():
print(f'No backend named {args.backend}.')
sys.exit(1)
logger = upt.log.create_logger(args.log_level)
try:
upt.log.logger_set_formatter(logger, 'Frontend')
upt_pkg = frontend.parse(args.package)
upt_pkg.frontend = args.frontend
upt.log.logger_set_formatter(logger, 'Backend')
backend.create_package(upt_pkg, output=args.output)
except upt.exceptions.UnhandledFrontendError as e:
print(e, file=sys.stderr)
......
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