Commit d78fe289 authored by Devaev Maxim's avatar Devaev Maxim

Splitted opening of config for read and write

parent 0589180e
...@@ -28,7 +28,7 @@ class PlainEditor(object) : ...@@ -28,7 +28,7 @@ class PlainEditor(object) :
##### #####
self.__config_file = None self.__config_file_path = None
self.__config_file_data_list = None self.__config_file_data_list = None
### ###
...@@ -42,42 +42,44 @@ class PlainEditor(object) : ...@@ -42,42 +42,44 @@ class PlainEditor(object) :
### Public ### ### Public ###
def open(self, config_file_path, config_sample_file_path = None) : def open(self, config_file_path, sample_config_file_path = None) :
if self.__config_file != None : if self.__config_file_path != None :
raise AlreadyAssociated("This parser already associated with config \"%s\"" % self.__config_file.name) raise AlreadyAssociated("This parser already associated with config \"%s\"" % self.__config_file_path)
if not os.access(config_file_path, os.F_OK) : if not os.access(config_file_path, os.F_OK) :
if config_sample_file_path != None and os.access(config_sample_file_path, os.F_OK) : if sample_config_file_path != None and os.access(sample_config_file_path, os.F_OK) :
shutil.copy2(config_sample_file_path, config_file_path) shutil.copy2(sample_config_file_path, config_file_path)
else : else :
open(config_file_path, "w").close() open(config_file_path, "w").close()
self.__config_file = open(config_file_path, "r+") config_file = open(config_file_path, "r")
self.__config_file_data_list = self.__config_file.read().split("\n") self.__config_file_data_list = config_file.read().split("\n")
self.__config_file_path = config_file_path
try :
config_file.close()
except : pass
def save(self) : def save(self) :
if self.__config_file == None : if self.__config_file_path == None :
raise NotAssociated("This parser is not associated with config") raise NotAssociated("This parser is not associated with config")
self.__config_file.seek(0) config_file = open(self.__config_file_path, "w")
self.__config_file.truncate() config_file.write("\n".join(self.__config_file_data_list))
self.__config_file.write("\n".join(self.__config_file_data_list)) try :
self.__config_file.flush() config_file.close()
except : pass
def close(self) : def close(self) :
if self.__config_file == None : if self.__config_file_path == None :
raise NotAssociated("This parser is not associated with config") raise NotAssociated("This parser is not associated with config")
try :
self.__config_file.close()
except : pass
self.__config_file = None
self.__config_file_data_list = None self.__config_file_data_list = None
self.__config_file_path = None
### ###
def setValue(self, variable_name, values_list) : def setValue(self, variable_name, values_list) :
if self.__config_file == None : if self.__config_file_path == None :
raise NotAssociated("This parser is not associated with config") raise NotAssociated("This parser is not associated with config")
if not type(values_list).__name__ in ("list", "tuple") : if not type(values_list).__name__ in ("list", "tuple") :
...@@ -103,7 +105,7 @@ class PlainEditor(object) : ...@@ -103,7 +105,7 @@ class PlainEditor(object) :
self.__config_file_data_list.insert(last_variable_index + count, variable) self.__config_file_data_list.insert(last_variable_index + count, variable)
def value(self, variable_name) : def value(self, variable_name) :
if self.__config_file == None : if self.__config_file_path == None :
raise NotAssociated("This parser is not associated with config") raise NotAssociated("This parser is not associated with config")
values_list = [] values_list = []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment