def write_option(self, name, option, value): try: cfg = ini_namespace(file(self.file_name)) except: self.specto.logger.log( _("There was an error initializing config file %s") % self.file_name, "critical", "specto") return False if cfg: name = self.hide_brackets(name) if not name in cfg._sections: return 0 else: if option == "password": # and self.check_old_version(self.cfg[name]['type']): #don't use encoding for old watches.list value = self.encode_password(name, value) cfg[name][option] = value try: f = open(self.file_name, "w") f.write( str(cfg).strip()) #write the new configuration file except IOError: self.specto.logger.log( _("There was an error writing to the file %s") % self.file_name, "critical", "specto") return False finally: f.close()
def write_watch(self, values): """ Write or change the watch options in a configuration file. Values has to be a dictionary with the name from the options and the value. example: { 'name':'value', 'interval':'value' } If the name is not found, a new watch will be added, else the excisting watch will be changed. """ try: cfg = ini_namespace(file(self.file_name)) except: self.specto.logger.log( _("There was an error initializing config file %s") % self.file_name, "critical", "specto") return False if cfg: name = self.hide_brackets(values['name']) if not name in cfg._sections: cfg.new_namespace(name) #add a new watch try: f = open(self.file_name, "w") f.write( str(cfg).strip()) #write the new configuration file except IOError: self.specto.logger.log( _("There was an error writing to the file %s") % self.file_name, "critical", "specto") return False finally: f.close() del values['name'] for option, value in values.iteritems(): self.write_option(name, option, value)
def read_watch(self, name, startup=False): """ Read the watch options from one watch. """ watch_options = {} if startup == False: try: self.cfg = ini_namespace(file(self.file_name)) except: self.specto.logger.log( _("There was an error initializing config file %s") % self.file_name, "critical", "specto") return False name = self.hide_brackets(name) options = self.cfg._sections[name]._options.keys() for option_ in options: if option_ == "password" and not self.check_old_version( self.cfg[name] ['type']): #don't use decoding for old watches.list option = self.read_option(name, option_, startup) option = self.decode_password(name, option) else: option = self.read_option(name, option_, startup) watch_options_ = {option_: option} watch_options.update(watch_options_) name = self.show_brackets(name) watch_options.update({'name': name}) return watch_options
def __init__(self, specto, file_name): # Read the watch from file using the iniparser module self.specto = specto self.file_name = file_name self.valid = True if not os.path.exists(self.file_name): try: f = open(self.file_name, "w") except: self.valid = False self.specto.logger.log( _("There was an error writing to the file %s") % self.file_name, "critical", "specto") finally: f.close() os.chmod( self.file_name, 0600 ) # This is important for security purposes, we make the file read-write to the owner only, otherwise everyone can read passwords. try: self.cfg = ini_namespace(file(self.file_name)) except: self.valid = False self.specto.logger.log( _("There was an error initializing config file %s") % self.file_name, "critical", "specto")
def read_option(self, name, option, startup=False): """ Read one option from a watch """ if startup == False: try: self.cfg = ini_namespace(file(self.file_name)) except: self.specto.logger.log( _("There was an error initializing config file %s") % self.file_name, "critical", "specto") return False try: return self.cfg[name][option] except: return 0
def already_exists(self, name): """ Returns True if the watch is found in the watches.list file. """ try: self.cfg = ini_namespace(file(self.file_name)) if not self.hide_brackets(name) in self.cfg._sections: return False else: return True except IOError: self.specto.logger.log( _("There was an error initializing config file %s") % self.file_name, "critical", "specto") return False #this has to be an error
def read_all_watches(self, startup=False): """ Read the watch options from the config file and return a dictionary containing the info needed to start the watches. """ watch_value_db = {} if startup == False: try: self.cfg = ini_namespace(file(self.file_name)) except: self.specto.logger.log( _("There was an error initializing config file %s") % self.file_name, "critical", "specto") return False names = self.cfg._sections.keys() i = 0 for name_ in names: watch_value_db[i] = self.read_watch(name_, startup) i += 1 return watch_value_db