This commit converts the whole config file management to yaml.

Everything still works like before except ladimenu, ladimenu does not *ever* get to create a config object !
Ladimenu's config is passed uppon manager object creation as a parameter, and saved by the parrent app.
This commit is contained in:
Marc-Olivier Barre 2009-12-28 20:52:35 +01:00
parent 3a971c8f29
commit c955d77564
5 changed files with 68 additions and 77 deletions

25
ladilog
View File

@ -34,7 +34,7 @@ try:
import vte import vte
import laditools import laditools
except Exception, e: except Exception, e:
error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Wow dude...\n\nYou really need to get you dependencies right before you run this program. Ask your package maintainer why he didn't do his job properly\n%s" % repr(e)) error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "You need to get you dependencies right before you run this program. Ask your package maintainer why this is happening to you\n%s" % repr(e))
error.run () error.run ()
exit (1) exit (1)
@ -85,18 +85,22 @@ class ladilog (gtk.glade.XML):
] ]
# Handle the configuration # Handle the configuration
self.jacklog_config = laditools.config () self.global_config = laditools.config ()
self.param_dict = self.jacklog_config.get_as_dict ('ladilog') self.param_dict = self.global_config.get_config_section ('ladilog')
for log in self.log_files: for log in self.log_files:
if log['config_name'] not in self.param_dict: if self.param_dict != None:
self.param_dict[log['config_name']] = log['config_default'], {} if log['config_name'] not in self.param_dict:
self.param_dict[log['config_name']] = log['config_default']
else:
self.param_dict = {}
self.param_dict[log['config_name']] = log['config_default']
if 'max_lines' not in self.param_dict: if 'max_lines' not in self.param_dict:
self.param_dict['max_lines'] = max_lines_default, {} self.param_dict['max_lines'] = max_lines_default
for log in self.log_files: for log in self.log_files:
log['logfile_path'], devnull = self.param_dict[log['config_name']] log['logfile_path'] = self.param_dict[log['config_name']]
# skip logfiles that dont exist # skip logfiles that dont exist
if not os.access(log['logfile_path'], os.R_OK): if not os.access(log['logfile_path'], os.R_OK):
self.log_files.remove(log) self.log_files.remove(log)
@ -104,7 +108,7 @@ class ladilog (gtk.glade.XML):
else: else:
print "Watching '%s'" % log['logfile_path'] print "Watching '%s'" % log['logfile_path']
max_lines_text, devnull = self.param_dict['max_lines'] max_lines_text = self.param_dict['max_lines']
self.max_lines = int (max_lines_text) self.max_lines = int (max_lines_text)
# Load the glade file # Load the glade file
gtk.glade.XML.__init__(self, laditools.find_data_file("ladilog_ui.glade")) gtk.glade.XML.__init__(self, laditools.find_data_file("ladilog_ui.glade"))
@ -171,7 +175,8 @@ class ladilog (gtk.glade.XML):
def run (self): def run (self):
gtk.main () gtk.main ()
self.jacklog_config.set_as_dict ("ladilog", self.param_dict) self.global_config.set_config_section ("ladilog", self.param_dict)
self.global_config.save ()
return 0 return 0
try: try:

View File

@ -29,13 +29,13 @@ autostart_default = False
class laditray (gtk.StatusIcon, laditools.manager): class laditray (gtk.StatusIcon, laditools.manager):
def __init__ (self): def __init__ (self):
# Handle the configuration # Handle the configuration
self.laditray_config = laditools.config () self.global_config = laditools.config ()
self.param_dict = self.laditray_config.get_as_dict ('laditray') self.laditray_param_dict = self.global_config.get_config_section ('laditray')
if 'autostart' not in self.param_dict: if 'autostart' not in self.laditray_param_dict:
self.param_dict['autostart'] = autostart_default self.laditray_param_dict['autostart'] = autostart_default
autostart = self.param_dict['autostart'] autostart = self.laditray_param_dict['autostart']
# Build the UI # Build the UI
laditools.manager.__init__(self, autostart) laditools.manager.__init__(self, self.global_config.get_config_section ('ladimenu'), autostart)
gtk.StatusIcon.__init__ (self) gtk.StatusIcon.__init__ (self)
self.icon_state = "" self.icon_state = ""
self.last_status_text = "" self.last_status_text = ""
@ -102,12 +102,16 @@ class laditray (gtk.StatusIcon, laditools.manager):
def run(self): def run(self):
gtk.main () gtk.main ()
self.laditray_config.set_as_dict ('laditray', self.param_dict) # Some default config might need to be injected in the config file,
# we handle all that before we quit.
self.global_config.set_config_section ('ladimenu', self.menu_array)
self.global_config.set_config_section ('laditray', self.laditray_param_dict)
self.global_config.save ()
return 0 return 0
#try: try:
laditray().run() laditray().run()
#except Exception, e: except Exception, e:
# error = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Unexpected error\n\n" + repr(e)) error = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, "Unexpected error\n\n" + repr(e))
# error.run() error.run()
# exit(1) exit(1)

View File

@ -21,44 +21,34 @@ from os import environ, sep, mkdir
from os.path import exists from os.path import exists
config_dir = environ['HOME'] + sep + ".config" + sep + "laditools" + sep config_dir = environ['HOME'] + sep + ".config" + sep + "laditools" + sep
config_filename = config_dir + "laditools.conf" config_filename = config_dir + "laditools.conf"
if not exists(config_dir): if not exists (config_dir):
mkdir(config_dir, 0755) mkdir (config_dir, 0755)
# Note to users of the config class. Only applications should create an instance
# of the config object. The ladimenu is *NOT* an application...
class config: class config:
def __init__(self): def __init__ (self):
try: try:
self.appdict = yaml.load(config_filename) config_file = open (config_filename)
self.appdict = yaml.load (config_file)
config_file.close ()
except: except:
self.appdict = {} print "Config file doesn't exist, creating a new one..."
self.appdict = dict ()
# Use this to create the dictionary that you'll use in your application # Returns the section named <app_name> from the global config
# You can add remove any parameters you wish from it, it'll get saved magically def get_config_section (self, app_name):
def get_as_dict(self, app_name):
if app_name in self.appdict: if app_name in self.appdict:
return self.appdict[app_name] return self.appdict[app_name]
else: else:
return {} return None
# Use this to create the array that you'll use in your application # Saves the section named <app_name> into the global config
# The array is used when you want to take into account the order the items are listed in the file def set_config_section (self, app_name, param_dict):
# You can add remove any parameters you wish from it, it'll get saved magically
def get_as_array(self, app_name):
if app_name in self.appdict:
return self.appdict[app_name]
else:
return []
# Use this when you want to update the yaml config with the content of the dictionary
def set_as_dict(self, app_name, param_dict):
self.appdict[app_name] = param_dict self.appdict[app_name] = param_dict
self.save()
# Use this when you want to update the yaml config with the content of the array # This writes the config file to the disk
def set_as_array(self, app_name, param_array): def save (self):
self.appdict[app_name] = param_array config_file = open (config_filename, 'w')
self.save() yaml.dump (self.appdict, config_file)
config_file.close ()
# Use this when you want to write the config file to disk
def save(self):
config_file = open(config_filename, 'w')
yaml.dump(self.appdict, config_file)

View File

@ -34,25 +34,24 @@ menu_default = [("Configure...", "ladiconf"),
("Logs...", "ladilog")] ("Logs...", "ladilog")]
class manager: class manager:
def __init__(self, jack_autostart = False): def __init__(self, menu_config_array, jack_autostart = False):
self.proxy_jack_controller = None self.proxy_jack_controller = None
self.proxy_jack_configure = None self.proxy_jack_configure = None
self.proxy_a2j_controller = None self.proxy_a2j_controller = None
self.proxy_ladish_controller = None self.proxy_ladish_controller = None
self.diagnose_text = "" self.diagnose_text = ""
# Handle the configuration and grab custom menu items # Handle the configuration and grab custom menu items
self.ladimenu_config = config() self.menu_array = menu_config_array
self.menu_array = self.ladimenu_config.get_as_array('ladimenu')
# Add some defaults if we don't already have a menu # Add some defaults if we don't already have a menu
if self.menu_array == []: if self.menu_array == None:
self.menu_array = []
for element in menu_default: for element in menu_default:
self.menu_array.append(element) self.menu_array.append(element)
self.ladimenu_config.set_as_array('ladimenu', self.menu_array, 'menuitem')
self.proc_list = [] self.proc_list = []
if jack_autostart: if jack_autostart:
self.jack_start() self.jack_start ()
def set_diagnose_text(self, text): def set_diagnose_text(self, text):
self.diagnose_text = text self.diagnose_text = text
@ -62,7 +61,6 @@ class manager:
def get_jack_controller(self): def get_jack_controller(self):
if not self.proxy_jack_controller: if not self.proxy_jack_controller:
#print "creating jack proxy"
self.proxy_jack_controller = jack_controller() self.proxy_jack_controller = jack_controller()
return self.proxy_jack_controller return self.proxy_jack_controller
@ -75,7 +73,6 @@ class manager:
return self.proxy_jack_configure return self.proxy_jack_configure
def clear_jack_proxies(self): def clear_jack_proxies(self):
#print "clearing jack proxies"
self.proxy_jack_controller = None self.proxy_jack_controller = None
self.proxy_jack_configure = None self.proxy_jack_configure = None
@ -107,27 +104,21 @@ class manager:
self.get_jack_controller().reset_xruns() self.get_jack_controller().reset_xruns()
def jack_is_started(self): def jack_is_started(self):
#print "jack_is_started"
return self.get_jack_controller().is_started() return self.get_jack_controller().is_started()
def jack_is_realtime(self): def jack_is_realtime(self):
#print "jack_is_realtime"
return self.get_jack_controller().is_realtime() return self.get_jack_controller().is_realtime()
def jack_get_load(self): def jack_get_load(self):
#print "jack_get_load"
return self.get_jack_controller().get_load() return self.get_jack_controller().get_load()
def jack_get_xruns(self): def jack_get_xruns(self):
#print "jack_get_xruns"
return self.get_jack_controller().get_xruns() return self.get_jack_controller().get_xruns()
def jack_get_sample_rate(self): def jack_get_sample_rate(self):
#print "jack_get_sample_rate"
return self.get_jack_controller().get_sample_rate() return self.get_jack_controller().get_sample_rate()
def jack_get_latency(self): def jack_get_latency(self):
#print "jack_get_latency"
return self.get_jack_controller().get_latency() return self.get_jack_controller().get_latency()
def get_a2j_controller(self): def get_a2j_controller(self):
@ -297,8 +288,8 @@ class manager:
menu_items.append((gtk.ImageMenuItem("Start gladish"), self.on_menu_launcher, "gladish")) menu_items.append((gtk.ImageMenuItem("Start gladish"), self.on_menu_launcher, "gladish"))
# Add the laucher entries at the beginning of the menu # Add the laucher entries at the beginning of the menu
for path, attrib_dict in self.menu_array: for menu_label, path in self.menu_array:
menu_items.append((gtk.ImageMenuItem(attrib_dict['name']), self.on_menu_launcher, path)) menu_items.append((gtk.ImageMenuItem(menu_label), self.on_menu_launcher, path))
menu = gtk.Menu() menu = gtk.Menu()
menu_items.append((gtk.SeparatorMenuItem(),)) menu_items.append((gtk.SeparatorMenuItem(),))

17
wmladi
View File

@ -32,21 +32,21 @@ import gobject
autostart_default = 0 autostart_default = 0
debug = False debug = False
class wmjackctl (wmoo.Application, laditools.manager): class wmladi (wmoo.Application, laditools.manager):
def __init__ (self): def __init__ (self):
# Handle the configuration # Handle the configuration
self.wmjackctl_config = laditools.config () self.global_config = laditools.config ()
self.param_dict = self.wmjackctl_config.get_as_dict ('wmjackctl') self.param_dict = self.global_config.get_config_setion ('wmladi')
if 'autostart' not in self.param_dict: if 'autostart' not in self.param_dict:
self.param_dict['autostart'] = str (autostart_default), {} self.param_dict['autostart'] = str (autostart_default)
autostart, devnull = self.param_dict['autostart'] autostart = self.param_dict['autostart']
wmoo.Application.__init__ ( wmoo.Application.__init__ (
self, self,
#background = os.path.dirname(sys.argv[0]) + os.sep + "wmjackctl.xpm", #background = os.path.dirname(sys.argv[0]) + os.sep + "wmjackctl.xpm",
margin = 2, margin = 2,
debug = False) debug = False)
laditools.manager.__init__(self, int(autostart)) laditools.manager.__init__(self, self.global_config.get_config_setion ('ladimenu'), int(autostart))
self.addCallback (self.on_button_release, 'buttonrelease', area=(0,0,64,64)) self.addCallback (self.on_button_release, 'buttonrelease', area=(0,0,64,64))
@ -197,6 +197,7 @@ class wmjackctl (wmoo.Application, laditools.manager):
def run (self): def run (self):
self.run_gtk () self.run_gtk ()
self.wmjackctl_config.set_as_dict ('wmjackctl', self.param_dict) self.global_config.set_config_section ('wmladi', self.param_dict)
self.global_config.save ()
wmjackctl ().run () wmladi ().run ()