-#!/usr/bin/env python
-# vim: set ts=4 sts=4 sw=4 textwidth=92:
-
-"""
-Module holding the config class, to be used by the rarslave project.
-"""
-
-__author__ = "Ira W. Snyder (devel@irasnyder.com)"
-__copyright__ = "Copyright (c) 2006,2007 Ira W. Snyder (devel@irasnyder.com)"
-__license__ = "GNU GPL v2 (or, at your option, any later version)"
-
-# config.py -- configuration class for rarslave
-#
-# Copyright (C) 2006,2007 Ira W. Snyder (devel@irasnyder.com)
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
-import os, ConfigParser
-
-class config (object):
- """A simple class to hold the default configs for the whole program"""
-
- DEFAULT_CONFIG=os.path.join ('~','.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))
-
- # [commands] section
- config.add_section('commands')
- for (s, k) in self.__defaults.keys():
- if s == 'commands':
- 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 native types for easier use
- SAFE_EVAL = ['None', 'True', 'False', '-1', '0', '1', '2']
-
- if value in SAFE_EVAL:
- value = eval (value)
-
- # Absolute-ize directories for easier use
- if section == 'directories' and value != None:
- value = os.path.abspath (os.path.expanduser (value))
-
- return value
-
- def __init__(self):
- self.__defaults = {
- ('directories', 'working_directory') : os.path.join ('~','downloads','usenet'),
- ('directories', 'extract_directory') : None,
- ('options', 'recursive') : True,
- ('options', 'interactive') : False,
- ('options', 'output_loglevel') : 0,
- ('regular expressions', 'par2_regex') : '^.*\.par2$',
- ('regular expressions', 'delete_regex') :
- '^.*\.(par2|\d|\d\d\d|rar|r\d\d|zip)$',
- ('regular expressions', 'basename_regex') :
- '^(.+)\.(par2|vol\d+\+\d+|\d\d\d|part\d+|rar|zip|avi|mp4|mkv|ogm)$',
- ('regular expressions', 'extractable_regex') :
- '^.+\.(rar|r\d\d|\d\d\d|zip)$',
- ('commands', 'unrar') : 'unrar x -o+ -- ',
- ('commands', 'unzip') : 'unzip \"%s\" -d \"%s\" ',
- ('commands', 'noextract') : 'mv \"%s\" \"%s\" ',
- ('commands', 'par2repair') : 'par2repair -- ',
- }
-
- self.__user_config = self.__read_config()
-
-
-
-
-def main ():
- pass
-
-if __name__ == '__main__':
- main ()
-