add error message when configuration files are not valid python module names

This commit is contained in:
Arya Reais-Parsi 2020-12-08 10:43:29 -08:00
parent 5833728889
commit 9eb2f3c0e6
1 changed files with 8 additions and 0 deletions

View File

@ -295,6 +295,14 @@ def read_config(config_file, is_unit_test=True):
dir_name = os.path.dirname(config_file)
module_name = os.path.basename(config_file)
# Check that the module name adheres to Python's module naming conventions.
# This will assist the user in interpreting subsequent errors in loading
# the module. Valid Python module naming is described here:
# https://docs.python.org/3/reference/simple_stmts.html#the-import-statement
if not module_name.isidentifier():
debug.error("Configuration file name is not a valid Python module name: "
"{0}. It should be a valid identifier.".format(module_name))
# Prepend the path to avoid if we are using the example config
sys.path.insert(0, dir_name)
# Import the configuration file of which modules to use