def maybe_load_file(self): if self.__class__.presets is not None: return try: f = open(self.path, 'r') except IOError: alert("IOError: Preset file doesn't exist. \ A new file is written to %s." % self.path) self.__class__.presets = {} self.write_file() else: config_string = f.read() try: self.__class__.presets = eval(config_string) except: alert("There's something wrong with the file. \ A backup is made, and a new file is created") shutil.copy(self.path, self.path + "~") self.__class__.presets = {}
def init_document(self): """Create a new saving document If file is not present, not valid xml or not starting with <presets>, a new file is generated.""" try: tree = ET.parse(self.path) except IOError: alert("IOError: Preset file doesn't exist. A new file is written to %s." % self.path) if not os.path.exists(os.path.dirname(self.path)): os.makedirs(os.path.dirname(self.path)) self.create_new_document() except ExpatError: alert("ExpatError: Preset file %s is malformed. A backup is made, and a new \ file is created" % self.path) shutil.copy(self.path, self.path + "~") self.create_new_document() else: self.__class__.root = tree.getroot() if self.__class__.root.tag != "presets": alert("Preset file is well-formed, but doesn't seem to contain our presets. A backup is made, and a new file is created" % self.path) shutil.copy(self.path, self.path + "~") self.create_new_document()
def save_error(self): alert("Couldn't save preset file. Please check path, permissions and whatnot.")
def write_file(self): try: f = open(self.path, 'w') except IOError, e: alert("Couldn't write to file. %s" % e)