Fix indentation, properly open and close the configuration file (thanks to the "with" statement)

This commit is contained in:
Alessio Treglia 2011-12-07 21:43:54 +01:00
parent aea03a75cf
commit a47e9f4725
1 changed files with 5 additions and 6 deletions

View File

@ -30,11 +30,10 @@ if not exists (config_dir):
class config:
def __init__ (self):
try:
config_file = open (config_filename)
self.appdict = yaml.load (config_file)
config_file.close ()
with open (config_filename) as config_file:
self.appdict = yaml.load (config_file)
except:
print "Config file doesn't exist, creating a new one..."
print "Config file doesn't exist, creating a new one..."
self.appdict = dict ()
# Returns the section named <app_name> from the global config
@ -46,10 +45,10 @@ class config:
# Saves the section named <app_name> into the global config
def set_config_section (self, app_name, param_dict):
self.appdict[app_name] = param_dict
self.appdict[app_name] = param_dict
# This writes the config file to the disk
def save (self):
config_file = open (config_filename, 'w')
yaml.dump (self.appdict, config_file, default_flow_style=False)
config_file.close ()
config_file.close ()