--- /dev/null
+#!/usr/bin/env python
+# vim: set ts=4 sts=4 sw=4 textwidth=92:
+
+import os, ConfigParser
+
+class RarslaveConfig (object):
+ """A simple class to hold the default configs for the whole program"""
+
+ DEFAULT_CONFIG='~/.config/rarslave2/rarslave2.conf'
+
+ def __read_config(self, filename=DEFAULT_CONFIG):
+ """Attempt to open and read the rarslave config file"""
+
+ # Make sure the filename is corrected
+ filename = os.path.abspath(os.path.expanduser(filename))
+
+ user_config = {}
+
+ # Write the default config if it doesn't exist
+ if not os.path.isfile(filename):
+ self.write_config(default=True)
+
+ config = ConfigParser.ConfigParser()
+ config.read(filename)
+
+ for section in config.sections():
+ for option in config.options(section):
+ user_config[(section, option)] = config.get(section, option)
+
+ return user_config
+
+ def write_config(self, filename=DEFAULT_CONFIG, default=False):
+ """Write out the current config to the config file. If you set default=True, then
+ the default config file will be written."""
+
+ config = ConfigParser.ConfigParser()
+
+ # Correct filename
+ filename = os.path.abspath(os.path.expanduser(filename))
+
+ # Reset all config to make sure we write the default one, if necessary
+ if default:
+ self.__user_config = {}
+ print 'Writing default config to %s' % (filename, )
+
+ # [directories] section
+ config.add_section('directories')
+ for (s, k) in self.__defaults.keys():
+ if s == 'directories':
+ config.set(s, k, self.get_value(s, k))
+
+ # [options] section
+ config.add_section('options')
+ for (s, k) in self.__defaults.keys():
+ if s == 'options':
+ config.set(s, k, self.get_value(s, k))
+
+ # [regular_expressions] section
+ config.add_section('regular expressions')
+ for (s, k) in self.__defaults.keys():
+ if s == 'regular expressions':
+ config.set(s, k, self.get_value(s, k))
+
+ # Try to make the ~/.config/rarslave/ directory
+ if not os.path.isdir(os.path.split(filename)[0]):
+ try:
+ os.makedirs(os.path.split(filename)[0])
+ except:
+ print 'Could not make directory: %s' % (os.path.split(filename)[0], )
+ sys.exit()
+
+ # Try to write the config file to disk
+ try:
+ fsock = open(filename, 'w')
+ try:
+ config.write(fsock)
+ finally:
+ fsock.close()
+ except:
+ print 'Could not open: %s for writing' % (filename, )
+ sys.exit()
+
+ def __get_default_val(self, section, key):
+ return self.__defaults[(section, key)]
+
+ def get_value(self, section, key):
+ """Get a config value. Attempts to get the value from the user's
+ config first, and then uses the default."""
+
+ try:
+ value = self.__user_config[(section, key)]
+ except:
+ # This should work, unless you write something stupid
+ # into the code, so DON'T DO IT
+ value = self.__get_default_val(section, key)
+
+ # Convert config options to booleans for easier use
+ if value == 'True':
+ value = True
+
+ if value == 'False':
+ value = False
+
+ return value
+
+ def __init__(self):
+ self.__defaults = {
+ ('directories', 'working_directory') : '~/downloads/usenet',
+ ('options', 'recursive') : True,
+ ('options', 'check_required_programs') : False,
+ ('options', 'extract_with_full_path') : False,
+ ('options', 'interactive') : False,
+ ('options', 'output_loglevel') : 1,
+ ('regular expressions', 'par2_regex') : '.*\.par2$',
+ ('regular expressions', 'video_file_regex') : '.*\.(avi|ogm|mkv|mp4)$',
+ ('regular expressions', 'temp_repair_regex') : '.*\.1$',
+ ('regular expressions', 'remove_regex') : '^.*\.(rar|r\d\d)$' }
+
+ self.__user_config = self.__read_config()
+
+
+
+
+def main ():
+ pass
+
+if __name__ == '__main__':
+ main ()
+