Skip to content

Commit

Permalink
Unify a commonly copy-pasted snippet of Config.
Browse files Browse the repository at this point in the history
  • Loading branch information
stump committed Oct 7, 2010
1 parent 2c7cfad commit 63fbe90
Showing 1 changed file with 15 additions and 44 deletions.
59 changes: 15 additions & 44 deletions src/Config.py
Expand Up @@ -36,16 +36,20 @@
logUndefinedGets = 0

class MyConfigParser(RawConfigParser):
def _writeSection(self, fp, sectionName, sectionDict):
fp.write("[%s]\n" % sectionName)
for key, value in sorted(sectionDict.iteritems()):
if key != "__name__":
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")

def write(self, fp, type = 0):
if type == 1:
return self.writeController(fp)
elif type == 2:
return self.writePlayer(fp)
if self._defaults:
fp.write("[%s]\n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
self._writeSection(fp, DEFAULTSECT, self._defaults)
sections = sorted(self._sections)
for section in sections:
if section == "theme":
Expand All @@ -54,67 +58,34 @@ def write(self, fp, type = 0):
continue
elif section == "player":
continue
fp.write("[%s]\n" % section)
sectList = self._sections[section].items()
sectList.sort()
for key, value in sectList:
if key != "__name__":
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
self._writeSection(fp, section, self._sections[section])

def writeTheme(self, fp):
if self._defaults:
fp.write("[%s]\n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
self._writeSection(fp, DEFAULTSECT, self._defaults)
sections = sorted(self._sections)
for section in sections:
if section != "theme":
continue
fp.write("[%s]\n" % section)
sectList = self._sections[section].items()
sectList.sort()
for key, value in sectList:
if key != "__name__":
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
self._writeSection(fp, section, self._sections[section])

def writeController(self, fp):
if self._defaults:
fp.write("[%s]\n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
self._writeSection(fp, DEFAULTSECT, self._defaults)
sections = sorted(self._sections)
for section in sections:
if section != "controller":
continue
fp.write("[%s]\n" % section)
sectList = self._sections[section].items()
sectList.sort()
for key, value in sectList:
if key != "__name__":
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
self._writeSection(fp, section, self._sections[section])

def writePlayer(self, fp):
if self._defaults:
fp.write("[%s]\n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
self._writeSection(fp, DEFAULTSECT, self._defaults)
sections = sorted(self._sections)
for section in sections:
if section != "player":
continue
fp.write("[%s]\n" % section)
sectList = self._sections[section].items()
sectList.sort()
for key, value in sectList:
if key != "__name__":
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
self._writeSection(fp, section, self._sections[section])

class Option:
"""A prototype configuration key."""
Expand Down

0 comments on commit 63fbe90

Please sign in to comment.