From 92a4fea8a9d63eab82a8217762bcd6bd1af262b6 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Tue, 26 Apr 2005 23:45:25 +0000 Subject: [PATCH] new simple optparser. *break* old configuration file compatibility. --- src/common/optparser.py | 234 +++++++++------------------------------- src/gajim.py | 7 +- 2 files changed, 52 insertions(+), 189 deletions(-) diff --git a/src/common/optparser.py b/src/common/optparser.py index e6141c67d..816c94162 100644 --- a/src/common/optparser.py +++ b/src/common/optparser.py @@ -1,8 +1,8 @@ -## common/optparser.py +#!/usr/bin/python ## ## Gajim Team: -## - Yann Le Boulanger -## - Vincent Hanquez +## - Yann Le Boulanger +## - Vincent Hanquez ## ## Copyright (C) 2003-2005 Gajim Team ## @@ -17,194 +17,58 @@ ## import os -from common import connection from common import gajim class OptionsParser: - def __init__(self, fname): - self.__fname = os.path.expanduser(fname) - self.tab = {} - # END __init__ + def __init__(self, filename): + self.__filename = os.path.expanduser(filename) - def parseCfgFile(self): - try: - fd = open(self.__fname) - except: - print 'error cannot open file %s\n' % (self.__fname); - return - - section = '' - for line in fd.readlines(): - if line[0] in "#;": - continue - if line[0] == '[': - section = line[1:line.find(']')] - self.tab[section] = {} - continue - index = line.find('=') - if index == -1: - continue - option = line[0:index] - option = option.strip() - value = line[index+2:-1] - if option.find('password') == -1: - if value == 'False': - self.tab[section][option] = False - elif value == 'True': - self.tab[section][option] = True - else: - try: - i = int(value) - except ValueError: - self.tab[section][option] = value - else: - self.tab[section][option] = i - else: - self.tab[section][option] = value - fd.close() - # END parseCfgFile - - def _fill_config_key(self, section, option, key): - if not self.tab.has_key(section): - return - if not self.tab[section].has_key(option): - return - gajim.config.set(key, self.tab[section][option]) - - def fill_config(self): - self._fill_config_key('Profile', 'log', 'log') - self._fill_config_key('Core', 'delauth', 'delauth') - self._fill_config_key('Core', 'delroster', 'delroster') - self._fill_config_key('Core', 'alwaysauth', 'alwaysauth') - self._fill_config_key('Logger', 'lognotsep', 'lognotsep') - self._fill_config_key('Logger', 'lognotusr', 'lognotusr') - - if self.tab.has_key('GtkGui'): - for k in self.tab['GtkGui']: - self._fill_config_key('GtkGui', k, k) - # status messages - for msg in gajim.config.get_per('statusmsg'): - gajim.config.del_per('statusmsg', msg) - i = 0 - while self.tab['GtkGui'].has_key('msg%s_name' % i): - gajim.config.add_per('statusmsg', self.tab['GtkGui']['msg%s_name' \ - % i]) - gajim.config.set_per('statusmsg', self.tab['GtkGui']['msg%s_name' \ - % i], 'message', self.tab['GtkGui']['msg%s' % i]) - i += 1 - # emoticons - if self.tab['GtkGui'].has_key('emoticons'): - for emot in gajim.config.get_per('emoticons'): - gajim.config.del_per('emoticons', emot) - emots = self.tab['GtkGui']['emoticons'].split('\t') - for i in range(0, len(emots)/2): - gajim.config.add_per('emoticons', emots[2*i]) - gajim.config.set_per('emoticons', emots[2*i], 'path', \ - emots[2*i+1]) - # sound events - for event in gajim.config.get_per('soundevents'): - gajim.config.del_per('soundevents', event) - for key in self.tab['GtkGui']: - if key.find('sound_'): - continue - if not self.tab['GtkGui'].has_key(key + '_file'): - continue - event = key[6:] - gajim.config.add_per('soundevents', event) - gajim.config.set_per('soundevents', event, 'enabled', \ - self.tab['GtkGui'][key]) - gajim.config.set_per('soundevents', event, 'path', \ - self.tab['GtkGui'][key + '_file']) - - # accounts - if self.tab.has_key('Profile'): - if self.tab['Profile'].has_key('accounts'): - accounts = self.tab['Profile']['accounts'].split() + def read_line(self, line): + index = line.find(" = ") + var_str = line[0:index] + value_str = line[index + 3:-1] + + i_start = var_str.find('.') + i_end = var_str.rfind('.') + + if i_start == -1: + gajim.config.set(var_str, value_str) else: - return - for account in accounts: - if not self.tab.has_key(account): - continue - gajim.connections[account] = connection.Connection(account) - gajim.config.add_per('accounts', account) - for key in self.tab[account]: - gajim.config.set_per('accounts', account, key, \ - self.tab[account][key]) - if gajim.config.get_per('accounts', account, 'savepass'): - gajim.connections[account].password = gajim.config.get_per( \ - 'accounts', account, 'password') - - def read_config(self): - self.tab = {} - self.tab['Profile'] = {} - self.tab['Profile']['log'] = gajim.config.get('log') + optname = var_str[0:i_start] + key = var_str[i_start + 1:i_end] + subname = var_str[i_end + 1:] + gajim.config.add_per(optname, key) + gajim.config.set_per(optname, key, subname, value_str) - self.tab['Core'] = {} - self.tab['Core']['delauth'] = gajim.config.get('delauth') - self.tab['Core']['delroster'] = gajim.config.get('delroster') - self.tab['Core']['alwaysauth'] = gajim.config.get('alwaysauth') - - self.tab['Logger'] = {} - self.tab['Logger']['lognotsep'] = gajim.config.get('lognotsep') - self.tab['Logger']['lognotusr'] = gajim.config.get('lognotusr') - - self.tab['GtkGui'] = {} - for key in gajim.config.get(None): - if key in self.tab['Profile']: - continue - if key in self.tab['Core']: - continue - if key in self.tab['Logger']: - continue - self.tab['GtkGui'][key] = gajim.config.get(key) - - # status messages - i = 0 - for msg in gajim.config.get_per('statusmsg'): - self.tab['GtkGui']['msg%s_name' % i] = msg - self.tab['GtkGui']['msg%s' % i] = gajim.config.get_per('statusmsg', \ - msg, 'message') - i += 1 - - # sounds - for event in gajim.config.get_per('soundevents'): - self.tab['GtkGui']['sound_' + event] = gajim.config.get_per( \ - 'soundevents', event, 'enabled') - self.tab['GtkGui']['sound_' + event + '_file'] = gajim.config.get_per(\ - 'soundevents', event, 'path') - - # emoticons - emots = [] - for emot in gajim.config.get_per('emoticons'): - emots.append(emot) - emots.append(gajim.config.get_per('emoticons', emot, 'path')) - self.tab['GtkGui']['emoticons'] = '\t'.join(emots) - - # accounts - accounts = gajim.config.get_per('accounts') - self.tab['Profile']['accounts'] = ' '.join(accounts) - for account in accounts: - self.tab[account] = {} - for key in gajim.config.get_per('accounts', account): - self.tab[account][key] = gajim.config.get_per('accounts', account, \ - key) - - def writeCfgFile(self): + def read(self): try: - fd = open(self.__fname, 'w') + fd = open(self.__filename) except: - log.debug('Can\'t write config %s' % self.__fname) - return 0 - index = 0 - for s in self.tab.keys(): - if index == 0: - fd.write('[' + s + ']\n') - else: - fd.write('\n[' + s + ']\n') - for o in self.tab[s].keys(): - fd.write(o + ' = ' + str(self.tab[s][o]) + '\n') - index += 1 - return 1 - # END writeCfgFile + print "error: cannot open %s\n" % (self.__filename) + return -# END OptionsParser + for line in fd.readlines(): + self.read_line(line) + + fd.close() + + def write_line(self, fd, opt, parents, value): + s = "" + if parents: + if len(parents) == 1: + return + for p in parents: + s += p + "." + if value == None: + return + s += opt + fd.write(s + " = " + str(value[1]) + "\n") + + def write(self): + try: + fd = open(self.__filename, 'w') + except: + print "error: cannot open %s\n" % (self.__filename) + return + gajim.config.foreach(self.write_line, fd) + fd.close() diff --git a/src/gajim.py b/src/gajim.py index 2639c3d7a..769269eff 100755 --- a/src/gajim.py +++ b/src/gajim.py @@ -634,8 +634,8 @@ class Interface: sys.exit() def save_config(self): - parser.read_config() - parser.writeCfgFile() + parser.read() + parser.write() def __init__(self): if gtk.pygtk_version >= (2, 6, 0): @@ -712,7 +712,6 @@ if __name__ == '__main__': except ImportError: pass - parser.parseCfgFile() - parser.fill_config() + parser.read() Interface() gtk.main()