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:
@@ -1,4 +1,3 @@
|
|||||||
import optparse
|
|
||||||
import sys
|
import sys
|
||||||
import os.path
|
import os.path
|
||||||
import logging
|
import logging
|
||||||
@@ -7,11 +6,12 @@ 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
|
||||||
@@ -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.
|
||||||
@@ -85,14 +87,16 @@ class MultiWorldParser(object):
|
|||||||
|
|
||||||
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,7 +125,6 @@ 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.
|
||||||
@@ -127,7 +134,8 @@ class MultiWorldParser(object):
|
|||||||
"""
|
"""
|
||||||
# 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?
|
||||||
|
|||||||
Reference in New Issue
Block a user