0

configParser: code style and string changes

It's now PEP8 compliant! Also we can remove an unused optparse
import (wtf?) and let logging.error do the string formatting lazily.
This commit is contained in:
Nicolas F
2019-07-24 05:35:40 +02:00
parent 7e2cb56c1b
commit f314de1403

View File

@@ -1,4 +1,3 @@
import optparse
import sys
import os.path
import logging
@@ -7,11 +6,12 @@ import traceback
from . import settingsDefinition
from . import settingsValidators
class MissingConfigException(Exception):
"To be thrown when the config file can't be found"
pass
class MultiWorldParser(object):
class MissingConfigException(Exception):
""""To be thrown when the config file can't be found"""
class MultiWorldParser:
"""A class that is used to parse a settings.py file.
This class's job is to compile and validate the configuration settings for
@@ -72,7 +72,9 @@ class MultiWorldParser(object):
"""
if not os.path.exists(settings_file) and not os.path.isfile(settings_file):
raise MissingConfigException("The settings file you specified (%r) does not exist, or is not a file" % settings_file)
raise MissingConfigException(
"The settings file you specified (%r) does not exist, or is not a file."
% settings_file)
# The global environment should be the rendermode module, so the config
# file has access to those resources.
@@ -85,14 +87,16 @@ class MultiWorldParser(object):
except Exception as ex:
if isinstance(ex, SyntaxError):
logging.error("Syntax error parsing %s" % settings_file)
logging.error("The traceback below will tell you which line triggered the syntax error\n")
logging.error("Syntax error parsing '%s'.", settings_file)
logging.error("The traceback below will tell you which line triggered the "
"syntax error.\n")
elif isinstance(ex, NameError):
logging.error("NameError parsing %s" % settings_file)
logging.error("The traceback below will tell you which line referenced the non-existent variable\n")
logging.error("NameError parsing '%s'.", settings_file)
logging.error("The traceback below will tell you which line referenced the "
"non-existent variable.\n")
else:
logging.error("Error parsing %s" % settings_file)
logging.error("The traceback below will tell you which line triggered the error\n")
logging.error("Error parsing '%s'.", settings_file)
logging.error("The traceback below will tell you which line triggered the error.\n")
# skip the execfile part of the traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
@@ -100,12 +104,16 @@ class MultiWorldParser(object):
print_rest = False
lines = []
for l in formatted_lines:
if print_rest: lines.append(l)
if print_rest:
lines.append(l)
else:
if "execfile" in l: print_rest = True
if "execfile" in l:
print_rest = True
# on windows, our traceback as no 'execfile'. in this case, print everything
if print_rest: logging.error("Partial traceback:\n" + "\n".join(lines))
else: logging.error("Partial traceback:\n" + "\n".join(formatted_lines))
if print_rest:
logging.error("Partial traceback:\n" + "\n".join(lines))
else:
logging.error("Partial traceback:\n" + "\n".join(formatted_lines))
sys.exit(1)
# At this point, make a pass through the file to possibly set global
@@ -117,7 +125,6 @@ class MultiWorldParser(object):
setting = render_settings[key]
setting.default = self._config_state[key]
def get_validated_config(self):
"""Validate and return the configuration. Raises a ValidationException
if there was a problem validating the config.
@@ -127,7 +134,8 @@ class MultiWorldParser(object):
"""
# Okay, this is okay, isn't it? We're going to create the validation
# routine right here, right now. I hope this works!
validator = settingsValidators.make_configDictValidator(self._settings, ignore_undefined=True)
validator = settingsValidators.make_configDictValidator(self._settings,
ignore_undefined=True)
# Woah. What just happened? No. WAIT, WHAT ARE YOU...
validated_config = validator(self._config_state)
# WHAT HAVE YOU DONE?