Skip to content

Commit

Permalink
Config: treat undefined gets as an error
Browse files Browse the repository at this point in the history
  • Loading branch information
stump committed Jan 6, 2013
1 parent ad452cc commit 237ccb4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 31 deletions.
38 changes: 11 additions & 27 deletions src/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
config = None
prototype = {}

logUndefinedGets = 0

class MyConfigParser(RawConfigParser):
def _writeSection(self, fp, sectionName, sectionItems):
Expand Down Expand Up @@ -98,11 +97,10 @@ def define(section, option, type, default = None, text = None, options = None, p

def load(fileName = None, setAsDefault = False, type = 0):
"""Load a configuration with the default prototype"""
global config, logUndefinedGets
global config
c = Config(prototype, fileName, type)
if setAsDefault and not config:
config = c
logUndefinedGets = c.get("game", "log_undefined_gets")
return c

def _convertValue(value, type, default=False):
Expand Down Expand Up @@ -168,15 +166,12 @@ def get(self, section, option):
@return: Key value
"""

global logUndefinedGets

try:
type = self.prototype[section][option].type
default = self.prototype[section][option].default
except KeyError:
if logUndefinedGets == 1:
Log.warn("Config key %s.%s not defined while reading %s." % (section, option, self.fileName))
type, default = str, None
Log.error("Config key %s.%s not defined while reading %s." % (section, option, self.fileName))
raise

value = _convertValue(self.config.get(section, option) if self.config.has_option(section, option) else default, type, default)

Expand All @@ -190,17 +185,14 @@ def getOptions(self, section, option):
@param option: Option name
@return: Tuple of Key list and Values list
"""
global logUndefinedGets


try:
options = self.prototype[section][option].options.values()
keys = self.prototype[section][option].options.keys()
type = self.prototype[section][option].type
except KeyError:
if logUndefinedGets == 1:
Log.warn("Config key %s.%s not defined while reading %s." % (section, option, self.fileName))
type = None
Log.error("Config key %s.%s not defined while reading %s." % (section, option, self.fileName))
raise

optionList = []

Expand All @@ -220,14 +212,11 @@ def getTipText(self, section, option):
@return: Tip Text String
"""

global logUndefinedGets

try:
text = self.prototype[section][option].tipText
except KeyError:
if logUndefinedGets == 1:
Log.warn("Config key %s.%s not defined while reading %s." % (section, option, self.fileName))
text = None
Log.error("Config key %s.%s not defined while reading %s." % (section, option, self.fileName))
raise

return text

Expand All @@ -239,16 +228,13 @@ def getDefault(self, section, option):
@param option: Option name
@return: Key value
"""
global logUndefinedGets


try:
type = self.prototype[section][option].type
default = self.prototype[section][option].default
except KeyError:
if logUndefinedGets == 1:
Log.warn("Config key %s.%s not defined while reading %s." % (section, option, self.fileName))
type, default = str, None
Log.error("Config key %s.%s not defined while reading %s." % (section, option, self.fileName))
raise

value = _convertValue(default, type)

Expand All @@ -263,13 +249,11 @@ def set(self, section, option, value):
@param value: Value name
"""

global logUndefinedGets

try:
prototype[section][option]
except KeyError:
if logUndefinedGets == 1:
Log.warn("Config key %s.%s not defined while writing %s." % (section, option, self.fileName))
Log.error("Config key %s.%s not defined while writing %s." % (section, option, self.fileName))
raise

if not self.config.has_section(section):
self.config.add_section(section)
Expand Down
1 change: 0 additions & 1 deletion src/ConfigDefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ def sortOptionsByKey(dict):
Config.define("game", "log_class_inits", int, 0, text = _("Log Class Inits"), options = {0: _("No"), 1: _("Yes")}, tipText = _("Logs most class initializations in '__init__'. This is unnecessary information in bug reports; please leave it disabled unless you are certain it is relevant."))
Config.define("game", "log_loadings", int, 0, text = _("Log Loadings"), options = {0: _("No"), 1: _("Yes")}, tipText = _("Logs resource loads. This is unnecessary information in bug reports; please leave it disabled unless you are certain it is relevant."))
Config.define("game", "log_sections", int, 0, text = _("Log MIDI Sections"), options = {0: _("No"), 1: _("Yes")}, tipText = _("Logs MIDI sections. This is unnecessary information in bug reports; please leave it disabled unless you are certain it is relevant."))
Config.define("game", "log_undefined_gets", int, 0, text = _("Log Undefined GETs"), options = {0: _("No"), 1: _("Yes")}, tipText = _("Logs attempts to read an undefined config key. This is unnecessary information."))
Config.define("game", "log_marker_notes", int, 0, text = _("Log Marker Notes"), options = {0: _("No"), 1: _("Yes")}, tipText = _("Logs MIDI marker notes (solo, SP, etc). This is unnecessary information in bug reports; please leave it disabled unless you are certain it is relevant."))
Config.define("game", "log_starpower_misses", int, 0, text = _("Log SP Misses"), options = {0: _("No"), 1: _("Yes")}, tipText = _("Logs SP phrase misses. This is unnecessary information."))
Config.define("log", "log_unedited_midis", int, 0, text = _("Log Unedited MIDIs"), options = {0: _("No"), 1: _("Yes")}, tipText = _("Logs when notes-unedited.mid is used. This is unnecessary information."))
Expand Down
1 change: 0 additions & 1 deletion src/Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,6 @@ def __init__(self, engine):
ConfigChoice(engine, engine.config, "game", "log_class_inits", autoApply = True),#myfingershurt
ConfigChoice(engine, engine.config, "game", "log_loadings", autoApply = True),#myfingershurt
ConfigChoice(engine, engine.config, "game", "log_sections", autoApply = True),#myfingershurt
ConfigChoice(engine, engine.config, "game", "log_undefined_gets", autoApply = True),#myfingershurt
ConfigChoice(engine, engine.config, "game", "log_marker_notes", autoApply = True),#myfingershurt
ConfigChoice(engine, engine.config, "game", "log_starpower_misses", autoApply = True),#myfingershurt
ConfigChoice(engine, engine.config, "log", "log_unedited_midis", autoApply = True),#myfingershurt
Expand Down
3 changes: 1 addition & 2 deletions src/Theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ class Theme(Task):
def __getattr__(self, attr):
try: #getting to this function is kinda slow. Set it on the first get to keep renders from lagging.
object.__getattribute__(self, '__dict__')[attr] = defaultDict[attr]
if Config.get("game", "log_undefined_gets") == 1:
Log.debug("No theme variable for %s - Loading default..." % attr)
Log.debug("No theme variable for %s - Loading default..." % attr)
return object.__getattribute__(self, attr)
except KeyError:
if attr in classNames.keys():
Expand Down

0 comments on commit 237ccb4

Please sign in to comment.