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 sys
import os.path import os.path
import logging import logging
@@ -7,13 +6,14 @@ import traceback
from . import settingsDefinition from . import settingsDefinition
from . import settingsValidators 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. """A class that is used to parse a settings.py file.
This class's job is to compile and validate the configuration settings for This class's job is to compile and validate the configuration settings for
a set of renders. It can read in configuration from the given file with the a set of renders. It can read in configuration from the given file with the
parse() method, and one can set configuration options directly with the parse() method, and one can set configuration options directly with the
@@ -39,7 +39,7 @@ class MultiWorldParser(object):
continue continue
self._settings[settingname] = setting self._settings[settingname] = setting
# Set top level defaults. This is intended to be for container # Set top level defaults. This is intended to be for container
# types, so they can initialize a config file with an empty # types, so they can initialize a config file with an empty
# container (like a dict) # container (like a dict)
@@ -72,7 +72,9 @@ class MultiWorldParser(object):
""" """
if not os.path.exists(settings_file) and not os.path.isfile(settings_file): 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 # The global environment should be the rendermode module, so the config
# file has access to those resources. # file has access to those resources.
@@ -82,17 +84,19 @@ class MultiWorldParser(object):
with open(settings_file, "rb") as settings_file_handle: with open(settings_file, "rb") as settings_file_handle:
exec(compile(settings_file_handle.read(), settings_file, 'exec'), exec(compile(settings_file_handle.read(), settings_file, 'exec'),
rendermodes.__dict__, self._config_state) rendermodes.__dict__, self._config_state)
except Exception as ex: except Exception as ex:
if isinstance(ex, SyntaxError): if isinstance(ex, SyntaxError):
logging.error("Syntax error parsing %s" % settings_file) 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("The traceback below will tell you which line triggered the "
"syntax error.\n")
elif isinstance(ex, NameError): elif isinstance(ex, NameError):
logging.error("NameError parsing %s" % settings_file) 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("The traceback below will tell you which line referenced the "
"non-existent variable.\n")
else: else:
logging.error("Error parsing %s" % settings_file) logging.error("Error parsing '%s'.", settings_file)
logging.error("The traceback below will tell you which line triggered the error\n") logging.error("The traceback below will tell you which line triggered the error.\n")
# skip the execfile part of the traceback # skip the execfile part of the traceback
exc_type, exc_value, exc_traceback = sys.exc_info() exc_type, exc_value, exc_traceback = sys.exc_info()
@@ -100,12 +104,16 @@ class MultiWorldParser(object):
print_rest = False print_rest = False
lines = [] lines = []
for l in formatted_lines: for l in formatted_lines:
if print_rest: lines.append(l) if print_rest:
lines.append(l)
else: 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 # on windows, our traceback as no 'execfile'. in this case, print everything
if print_rest: logging.error("Partial traceback:\n" + "\n".join(lines)) if print_rest:
else: logging.error("Partial traceback:\n" + "\n".join(formatted_lines)) logging.error("Partial traceback:\n" + "\n".join(lines))
else:
logging.error("Partial traceback:\n" + "\n".join(formatted_lines))
sys.exit(1) sys.exit(1)
# At this point, make a pass through the file to possibly set global # At this point, make a pass through the file to possibly set global
@@ -117,17 +125,17 @@ class MultiWorldParser(object):
setting = render_settings[key] setting = render_settings[key]
setting.default = self._config_state[key] setting.default = self._config_state[key]
def get_validated_config(self): def get_validated_config(self):
"""Validate and return the configuration. Raises a ValidationException """Validate and return the configuration. Raises a ValidationException
if there was a problem validating the config. if there was a problem validating the config.
Could also raise a ValueError Could also raise a ValueError
""" """
# Okay, this is okay, isn't it? We're going to create the validation # Okay, this is okay, isn't it? We're going to create the validation
# routine right here, right now. I hope this works! # 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... # Woah. What just happened? No. WAIT, WHAT ARE YOU...
validated_config = validator(self._config_state) validated_config = validator(self._config_state)
# WHAT HAVE YOU DONE? # WHAT HAVE YOU DONE?