Start working on a new implementation.
[laditools.git] / Laditools / config.py
blobed256639e79eeb8432d65193cb23b488bdf299dc
1 #!/usr/bin/python
2 # LADITools - Linux Audio Desktop Integration Tools
3 # Copyright (C) 2011-2012 Alessio Treglia <quadrispro@ubuntu.com>
4 # Copyright (C) 2007-2010, Marc-Olivier Barre <marco@marcochapeau.org>
5 # Copyright (C) 2007-2009, Nedko Arnaudov <nedko@arnaudov.name>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # Let's make sure we'll place the file in an existing dir
21 from os import environ, sep, mkdir, path
22 from os.path import exists
23 from xdg import BaseDirectory as basedir
24 import sys
25 import yaml
27 config_dir = path.join(basedir.xdg_config_home, 'laditools')
28 config_filename = path.join(config_dir, 'laditools.conf')
29 if not exists (config_dir):
30 mkdir (config_dir, 0755)
32 # Note to users of the config class. Only applications should create an instance
33 # of the config object. The ladimenu is *NOT* an application...
34 class Configuration(object):
35 def __init__ (self, args = None):
36 self.appdict = {}
37 try:
38 with open (config_filename, 'r') as config_file:
39 self.appdict = yaml.load (config_file)
40 except:
41 sys.stderr.write("Config file doesn't exist, creating a new one...\n")
42 sys.stderr.flush()
44 # Returns the section named <app_name> from the global config
45 def get_config_section (self, app_name):
46 try:
47 if app_name in self.appdict:
48 return self.appdict[app_name]
49 else:
50 return {}
51 except:
52 return {}
54 # Saves the section named <app_name> into the global config
55 def set_config_section (self, app_name, param_dict):
56 self.appdict[app_name] = param_dict
58 # This writes the config file to the disk
59 def save (self):
60 config_file = open (config_filename, 'w')
61 yaml.dump (self.appdict, config_file, default_flow_style=False)
62 config_file.close ()