Make configuration values always available

This is nice in case something tries to read a configuration value
before the configuration files have been read. This could happen for the
logging functions for example, and for tests.

The configuration is empty before the configuration files have been
read, so defaults must be provided. They must be anyway though, for the
code to be robust in case configuration files are missing.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
This commit is contained in:
Ulf Magnusson 2018-10-16 23:40:05 +02:00 committed by Marti Bolivar
parent 1967d3d47a
commit 9c027d703d
3 changed files with 19 additions and 24 deletions

View File

@ -11,7 +11,7 @@ import shutil
import subprocess
import textwrap
from west import config
from west.config import config
from west import log
from west import util
from west.commands import WestCommand
@ -593,8 +593,7 @@ def _special_project(name):
# 'revision' always exists and defaults to 'master'
return Project(name, remote, None,
revision=config.config.get(name, 'revision',
fallback='master'),
revision=config.get(name, 'revision', fallback='master'),
path=os.path.join('west', name))

View File

@ -9,6 +9,15 @@ import platform
from west.util import west_dir
# Configuration values.
#
# Initially empty, populated in read_config(). Always having this available is
# nice in case something checks configuration values before the configuration
# file has been read (e.g. the log.py functions, to check color settings, and
# tests).
config = configparser.ConfigParser()
def read_config():
'''
Reads all configuration files, making the configuration values available as
@ -42,14 +51,8 @@ def read_config():
Configuration values from later configuration files override configuration
from earlier ones. Instance-specific configuration values have the highest
precedence, and system-wide the lowest.
A convenience boolean 'colorize' is exported by this module as well, set to
True if the output should be colorized, based on configuration settings.
'''
global config
global colorize
# Gather (potential) configuration file paths
# System-wide and user-specific
@ -80,16 +83,9 @@ def read_config():
# Parse all existing configuration files
#
config = configparser.ConfigParser()
config.read(files, encoding='utf-8')
#
# Set convenience variables
#
colorize = config.getboolean('color', 'ui', fallback=True)
# Value to use before the configuration file has been read. This also fixes
# tests that run stuff without reading the configuration file first.
colorize = False
def use_colors():
# Convenience function for reading the color.ui setting
return config.getboolean('color', 'ui', fallback=True)

View File

@ -50,7 +50,7 @@ def inf(*args, colorize=False):
If True, the message is printed in bright green if stdout is a terminal.
'''
if not config.colorize:
if not config.use_colors():
colorize = False
# This approach colorizes any sep= and end= text too, as expected.
@ -69,26 +69,26 @@ def inf(*args, colorize=False):
def wrn(*args):
'''Print a warning.'''
if config.colorize:
if config.use_colors():
print(colorama.Fore.LIGHTRED_EX, end='', file=sys.stderr)
print('WARNING: ', end='', file=sys.stderr)
print(*args, file=sys.stderr)
if config.colorize:
if config.use_colors():
_reset_colors(sys.stderr)
def err(*args, fatal=False):
'''Print an error.'''
if config.colorize:
if config.use_colors():
print(colorama.Fore.LIGHTRED_EX, end='', file=sys.stderr)
print('FATAL ERROR: ' if fatal else 'ERROR: ', end='', file=sys.stderr)
print(*args, file=sys.stderr)
if config.colorize:
if config.use_colors():
_reset_colors(sys.stderr)