Config() now loads in defaults if no file is specified

This commit is contained in:
2010-01-30 10:00:12 +00:00
parent f79b771a38
commit d6a3a2878e

View File

@@ -6,16 +6,16 @@ def defaults():
Creates a ConfigParser instance and fills it with the default settings Creates a ConfigParser instance and fills it with the default settings
""" """
config = ConfigParser.ConfigParser() config = ConfigParser.RawConfigParser()
config.add_section('main') config.add_section('main')
config.set('main', 'target_path', '/media/%(showname)s/Season %(seasonnum)s/') config.set('main', 'target_path', '/media/%(showname)s/Season %(seasonnum)s/')
config.add_section('regex') config.add_section('regex')
config.set('regex', 'valid_in_names', "[\\w\\(\\).,\\[\\]'\\ \\-?!#:]") config.set('regex', 'valid_in_names', "[\\w\\(\\).,\\[\\]'\\ \\-?!#:]")
return config return config
class Config(dict): class Config(dict):
""" """
@@ -24,12 +24,14 @@ class Config(dict):
""" """
def __init__(self, cfile=None): def __init__(self, cfile=None):
super(Config, self).__init__() super(Config, self).__init__()
if cfile: if cfile:
self.load(cfile) self.load(cfile)
else:
self.defaults()
def load(self, cfile): def load(self, cfile=None, cparser=None):
""" """
Parses the TVOrganiser style config file and produces a dict Parses the TVOrganiser style config file and produces a dict
with all the elements contained within. with all the elements contained within.
@@ -37,8 +39,11 @@ class Config(dict):
Also, all regex specified in the file are compiled Also, all regex specified in the file are compiled
""" """
configpsr = ConfigParser.RawConfigParser() if cparser:
configpsr.read(cfile) configpsr = cparser
else:
configpsr = ConfigParser.RawConfigParser()
configpsr.read(cfile)
if configpsr.has_section('main'): if configpsr.has_section('main'):
for key, value in configpsr.items('main'): for key, value in configpsr.items('main'):
@@ -60,3 +65,8 @@ class Config(dict):
self['regex'] = regex self['regex'] = regex
def defaults(self):
"""
Load default settings
"""
self.load(cparser=defaults())