2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
4 import os, ConfigParser
6 class RarslaveConfig (object):
7 """A simple class to hold the default configs for the whole program"""
9 DEFAULT_CONFIG='~/.config/rarslave2/rarslave2.conf'
11 def __read_config(self, filename=DEFAULT_CONFIG):
12 """Attempt to open and read the rarslave config file"""
14 # Make sure the filename is corrected
15 filename = os.path.abspath(os.path.expanduser(filename))
19 # Write the default config if it doesn't exist
20 if not os.path.isfile(filename):
21 self.write_config(default=True)
23 config = ConfigParser.ConfigParser()
26 for section in config.sections():
27 for option in config.options(section):
28 user_config[(section, option)] = config.get(section, option)
32 def write_config(self, filename=DEFAULT_CONFIG, default=False):
33 """Write out the current config to the config file. If you set default=True, then
34 the default config file will be written."""
36 config = ConfigParser.ConfigParser()
39 filename = os.path.abspath(os.path.expanduser(filename))
41 # Reset all config to make sure we write the default one, if necessary
43 self.__user_config = {}
44 print 'Writing default config to %s' % (filename, )
46 # [directories] section
47 config.add_section('directories')
48 for (s, k) in self.__defaults.keys():
49 if s == 'directories':
50 config.set(s, k, self.get_value(s, k))
53 config.add_section('options')
54 for (s, k) in self.__defaults.keys():
56 config.set(s, k, self.get_value(s, k))
58 # [regular_expressions] section
59 config.add_section('regular expressions')
60 for (s, k) in self.__defaults.keys():
61 if s == 'regular expressions':
62 config.set(s, k, self.get_value(s, k))
64 # Try to make the ~/.config/rarslave/ directory
65 if not os.path.isdir(os.path.split(filename)[0]):
67 os.makedirs(os.path.split(filename)[0])
69 print 'Could not make directory: %s' % (os.path.split(filename)[0], )
72 # Try to write the config file to disk
74 fsock = open(filename, 'w')
80 print 'Could not open: %s for writing' % (filename, )
83 def __get_default_val(self, section, key):
84 return self.__defaults[(section, key)]
86 def get_value(self, section, key):
87 """Get a config value. Attempts to get the value from the user's
88 config first, and then uses the default."""
91 value = self.__user_config[(section, key)]
93 # This should work, unless you write something stupid
94 # into the code, so DON'T DO IT
95 value = self.__get_default_val(section, key)
97 # Convert config options to booleans for easier use
108 ('directories', 'working_directory') : '~/downloads/usenet',
109 ('options', 'recursive') : True,
110 ('options', 'check_required_programs') : False,
111 ('options', 'extract_with_full_path') : False,
112 ('options', 'interactive') : False,
113 ('options', 'output_loglevel') : 1,
114 ('regular expressions', 'par2_regex') : '.*\.par2$',
115 ('regular expressions', 'video_file_regex') : '.*\.(avi|ogm|mkv|mp4)$',
116 ('regular expressions', 'temp_repair_regex') : '.*\.1$',
117 ('regular expressions', 'remove_regex') : '^.*\.(rar|r\d\d)$' }
119 self.__user_config = self.__read_config()
127 if __name__ == '__main__':